Need to write JUnit Test case

I am a younger java developer. But I don't have much knowledge about writing Junit test cases. I will do the work soon. Why do they want me to write a program

  • To read HTML from any site, say "http://www.google.com" (you can use any API of built-in APIs in Java, for example URLConnection)
  • Print HTML from the URL above on the console and save it in a file (web-content.txt) on the local machine.
  • JUnit test examples for the above program.

I followed the first two steps as shown below:

import java.io.*; import java.net.*; public class JavaSourceViewer{ public static void main (String[] args) throws IOException{ System.out.print("Enter url of local for viewing html source code: "); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String url = br.readLine(); try { URL u = new URL(url); HttpURLConnection uc = (HttpURLConnection) u.openConnection(); int code = uc.getResponseCode(); String response = uc.getResponseMessage(); System.out.println("HTTP/1.x " + code + " " + response); InputStream in = new BufferedInputStream(uc.getInputStream()); Reader r = new InputStreamReader(in); int c; FileOutputStream fout=new FileOutputStream("D://web-content.txt"); while((c = r.read()) != -1){ System.out.print((char)c); fout.write(c); } fout.close(); } catch(MalformedURLException ex) { System.err.println(url + " is not a valid URL."); } catch (IOException ie) { System.out.println("Input/Output Error: " + ie.getMessage()); } } } 

Now I need help with the third step.

+6
source share
4 answers

You need to extract the method, therefore

 package abc.def; import java.io.*; import java.net.*; public class JavaSourceViewer { public static void main(String[] args) throws IOException { System.out.print("Enter url of local for viewing html source code: "); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String url = br.readLine(); try { FileOutputStream fout = new FileOutputStream("D://web-content.txt"); writeURL2Stream(url, fout); fout.close(); } catch (MalformedURLException ex) { System.err.println(url + " is not a valid URL."); } catch (IOException ie) { System.out.println("Input/Output Error: " + ie.getMessage()); } } private static void writeURL2Stream(String url, OutputStream fout) throws MalformedURLException, IOException { URL u = new URL(url); HttpURLConnection uc = (HttpURLConnection) u.openConnection(); int code = uc.getResponseCode(); String response = uc.getResponseMessage(); System.out.println("HTTP/1.x " + code + " " + response); InputStream in = new BufferedInputStream(uc.getInputStream()); Reader r = new InputStreamReader(in); int c; while ((c = r.read()) != -1) { System.out.print((char) c); fout.write(c); } } } 

Ok, now you can write JUnit-Test.

  package abc.def; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.MalformedURLException; import org.junit.Test; import junit.framework.TestCase; public class MainTestCase extends TestCase { @Test public static void test() throws MalformedURLException, IOException{ ByteArrayOutputStream baos = new ByteArrayOutputStream(); JavaSourceViewer.writeURL2Stream("http://www.google.de", baos); assertTrue(baos.toString().contains("google")); } } 
+4
source
  • First move your code from the main method to other methods in the JavaSourceViewer.
  • Secondly, these methods return something that you can check. for example, the file name of the output file or Reader.

Then create a class for unit test

 import org.junit.* ; import static org.junit.Assert.* ; public class JavaSourceViewerTest { @Test public void testJavaSourceViewer() { String url = "..."; JavaSourceViewer jsv = new JavaSourceViewer(); // call you methods here to parse the site jsv.xxxMethod(...) .... // call you checks here like: // <file name to save to output data from your code, actual filename> - eg assertEquals(jsv.getOutputFile(), "D://web-content.txt"); .... } } 

To run Junit, use an IDE (e.g. eclipse) or put the junit.jar file in the classpath and from the console:

java org.junit.runner.JUnitCore JavaSourceViewerTest

+2
source

Your steps are wrong. Doing this this way will definitely help, 3, then 1, and then 2. Doing this way will make you think about functionality and units. And the result code will be tested without doing anything special. The test also first guides your system design beyond security.

PS Never try to write code before a test. It is simply not natural, and it does not attach much importance, as you can see.

Now, to check the 1st block, you can compare string , html with google.com with some existing string . But this test case will be broken if Google changes its page. Another way is to simply check the HTTP code from the header, if it's 200, you're fine. Just an idea.

For the second, you can compare the string that you are reading from a web page to the string that you wrote in the file by reading the file.

+1
source
  String str = ""; URL oracle = new URL("http://www.google.com/"); BufferedReader in = new BufferedReader( new InputStreamReader(oracle.openStream())); File file = new File("C:/Users/rohit/Desktop/rr1.txt"); String inputLine; FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); bw.write(inputLine); } bw.close(); in.close(); 
-1
source

Source: https://habr.com/ru/post/925906/


All Articles