Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
student:junit:v5 [2018/08/14 10:45] – created bernstdhstudent:junit:v5 [2020/04/23 11:25] – [Testing Methods that Throw Exceptions] changing order, add sub-headings for convenient deep linking stewarmc
Line 147: Line 147:
  
  
-There are several ways to test for thrown exceptions in JUnit. The most common is to use the ''%%assertThrows()%%'' method. For examplesuppose   the constructor of the ''%%Atom()%%'' class is required to throw an   ''%%IllegalArgumentException%%'' when the numeric parameters   are negativeOne might test this is follows.+There are several ways to test for thrown exceptions in JUnit. The most common is to use the ''%%assertThrows()%%'' method, but this approach depends on a feature of Java (and many languages called [[https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html|"lambda expressions"]], so we will demonstrate a less commonly used approach first.
  
 +=== Testing for exceptions ===
  
-<code java> +One can invoke the methods that is supposed to throw an expression in a ''%%try-catch%%'' block, and use the ''%%fail()%%'' method. For example:
-  /** +
-   * Test that the constructor validates properly. +
-   */ +
-  @Test +
-  public void constructor_IllegalArguments()  +
-          throws IllegalArgumentException +
-  { +
-    assertThrows(IllegalArgumentException.class, () -> {new Atom("O", -8, -16);}); +
-  } +
-</code> +
- +
-This approach uses a //Lambda expression//, a representation of a class with a single-method.  +
-(Note that ''%%IllegalArgumentException%%'' is an unchecked   exception. Hence, this code will compile even if the test method does   not specify that it re-throws the exception. If you are testing for   a checked exception then the test method must specify that it re-throws   the exception.) +
- +
- +
-Alternatively, one can invoke the methods that is supposed to throw an expression in a ''%%try-catch%%'' block, and use the ''%%fail()%%'' method. For example:+
  
 <code java> <code java>
Line 180: Line 165:
  
       // Shouldn’t get here       // Shouldn’t get here
-      fail("Constrcutor should have thrown an IllegalArgumentException");+      fail("Constructor should have thrown an IllegalArgumentException");
     }     }
     catch (IllegalArgumentException iae)     catch (IllegalArgumentException iae)
Line 189: Line 174:
 </code> </code>
  
 +=== Testing for exceptions canonically (with lambda expressions) ===
  
 +Suppose the constructor of the ''%%Atom()%%'' class is required to throw an   ''%%IllegalArgumentException%%'' when the numeric parameters   are negative. One might test this is follows.
  
 +
 +<code java>
 +  /**
 +   * Test that the constructor validates properly.
 +   */
 +  @Test
 +  public void constructor_IllegalArguments()
 +  {
 +    assertThrows(IllegalArgumentException.class, () -> {new Atom("O", -8, -16);});
 +  }
 +</code>
 +
 +This approach uses a //Lambda expression//, a representation of a class with a single-method. 
 ==== Other Useful Methods in the Assert Class ==== ==== Other Useful Methods in the Assert Class ====
  
Line 224: Line 224:
  
  
-==== The @Before Annotation ====+==== The @BeforeEach and @BeforeAll Annotations ====
  
  
Line 230: Line 230:
  
  
-The ''%%@Before%%'' annotation is very useful when   an individual test changes the state of an object since, in general,   one wants test to be independent. (Note that, in general, you should   not assume that the tests themselves will be run in a particular order.)+The ''%%@BeforeEach%%'' and ''%%@BeforeAll%%'' annotations are very useful when   an individual test changes the state of an object since, in general,   one wants test to be independent. (Note that, in general, you should   not assume that the tests themselves will be run in a particular order.)
  
 To use these annotation you must import ''%%org.junit.jupiter.api.BeforeEach%%'' and/or To use these annotation you must import ''%%org.junit.jupiter.api.BeforeEach%%'' and/or
Line 240: Line 240:
  
 Information about how to download and install JUnit is available   from   [[ https://github.com/junit-team/junit/wiki/Download-and-Install | junit.org ]]. Information about how to download and install JUnit is available   from   [[ https://github.com/junit-team/junit/wiki/Download-and-Install | junit.org ]].
 +The .jar file will be named something like ''%%junit-platform-console-standalone.jar%%'' (which is referred to below as ''%%junit.jar%%'' for simplicity).
  
 ==== Compiling and Running JUnit Tests from a Command Shell ==== ==== Compiling and Running JUnit Tests from a Command Shell ====
Line 262: Line 262:
  
 <code bash> <code bash>
-export CLASSPATH=.://directory//junit.jar://directory//hamcrest-core.jar:$CLASSPATH+export CLASSPATH=.://directory//junit.jar:$CLASSPATH
 </code> </code>
  
-where //directory// denotes the name of the directory/folder that contains the files ''%%junit.jar%%'' and ''%%hamcrest-core.jar%%''.+where //directory// denotes the name of the directory/folder that contains the file ''%%junit.jar%%''.
  
-For example, assuming that the two ''%%.jar%%'' fiels are in the current working directory, you can set the ''%%CLASSPATH%%'' as follows:+For example, assuming that the ''%%.jar%%'' file is in the current working directory, you can set the ''%%CLASSPATH%%'' as follows:
  
 <code bash> <code bash>
-export CLASSPATH=.:junit.jar:hamcrest-core.jar:$CLASSPATH+export CLASSPATH=.:junit.jar:$CLASSPATH
 </code> </code>
  
Line 276: Line 276:
  
 <code bash> <code bash>
-java org.junit.runner.JUnitCore //ClassName//Test+java org.junit.platform.console.ConsoleLauncher --select-class  //ClassName//Test
 </code>  </code> 
 +
  
 where //ClassName// is the name of the class being tested. For example, to run ''%%AtomTest%%'': where //ClassName// is the name of the class being tested. For example, to run ''%%AtomTest%%'':
  
 <code> <code>
-java org.junit.runner.JUnitCore AtomTest+java org.junit.platform.console.ConsoleLauncher --select-class  AtomTest
 </code> </code>
  
Line 301: Line 302:
 To run: To run:
  
-''java -cp .:junit.jar:hamcrest-core.jarorg.junit.runner.JUnitCore //ClassName//Test''+''java -cp .;junit.jar org.junit.platform.console.ConsoleLauncher --select-class  //ClassName//Test''
  
 where //Name// represents the name of the class being tested. where //Name// represents the name of the class being tested.
Line 309: Line 310:
 <code bash> <code bash>
 javac -cp .:junit.jar AtomTest.java javac -cp .:junit.jar AtomTest.java
-java -cp .:junit.jar hamcrest-core.jarorg.junit.runner.JUnitCore AtomTest+java -cp .:junit.jar org.junit.platform.console.ConsoleLauncher --select-class   AtomTest
 </code> </code>
  
Line 325: Line 326:
  
  
-The Java compiler needs to be able to "find" the JUnit classes and the Java intepreter needs to be able to find both the JUnit classes and the Hamcrest classes. For this discussion, we will assume that the JUnit classes are in ''%%junit.jar%%'' and the Hamcrest classes are in ''%%hamcrest-core.jar%%'' (though the actual names will vary). You can either identify the location of these files each time you use the compiler/interpreter or you can set an operating system environment variable named ''%%CLASSPATH%%''.+The Java compiler needs to be able to "find" the JUnit classes and the Java intepreter needs to be able to find the JUnit classes. For this discussion, we will assume that the JUnit classes are in ''%%junit.jar%%'' (though the actual names will vary). You can either identify the location of these files each time you use the compiler/interpreter or you can set an operating system environment variable named ''%%CLASSPATH%%''.
  
  
Line 333: Line 334:
 You can set the ''%%CLASSPATH%%'' as follows: You can set the ''%%CLASSPATH%%'' as follows:
  
-''set CLASSPATH=.;//directory//junit.jar;//directory//hamcrest.jar;%CLASSPATH%''+''set CLASSPATH=.;//directory//junit.jar;%CLASSPATH%''
  
-where //directory// denotes the name of the directory/folder that contains the files ''%%junit.jar%%'' and ''%%hamcrest-core.jar%%''.+where //directory// denotes the name of the directory/folder that contains the file ''%%junit.jar%%''.
  
-For example, assuming that the two ''%%.jar%%'' files are in the current working directory, you can set the ''%%CLASSPATH%%'' as follows:+For example, assuming that the ''%%.jar%%'' file is in the current working directory, you can set the ''%%CLASSPATH%%'' as follows:
  
 <code> <code>
-set CLASSPATH=.;junit.jar;hamcrest-core.jar;%CLASSPATH%+set CLASSPATH=.;junit.jar;%CLASSPATH%
 </code> </code>
  
 After you have set the ''%%CLASSPATH%%'' you can compile your classes (including the test classes) and the usual way, and run a JUnit test as follows: After you have set the ''%%CLASSPATH%%'' you can compile your classes (including the test classes) and the usual way, and run a JUnit test as follows:
  
-java org.junit.runner.JUnitCore //ClassName//Test+java  org.junit.platform.console.ConsoleLauncher --select-class   //ClassName//Test
    
 where //ClassName// is the name of the class being tested. For example, to run ''%%AtomTest%%'': where //ClassName// is the name of the class being tested. For example, to run ''%%AtomTest%%'':
  
 <code> <code>
-java org.junit.runner.JUnitCore AtomTest+java  org.junit.platform.console.ConsoleLauncher --select-class   AtomTest
 </code> </code>
  
Line 368: Line 369:
 To run: To run:
  
-java -cp .;junit.jar;hamcrest-core.jarorg.junit.runner.JUnitCore //ClassName//Test+java -cp .;junit.jar org.junit.platform.console.ConsoleLauncher --select-class   //ClassName//Test
  
 where //Name// represents the name of the class being tested. where //Name// represents the name of the class being tested.
Line 375: Line 376:
  
 <code> <code>
-javac -cp .;junit.jar;hamcrest-core.jar AtomTest.java +javac -cp .;junit.jar AtomTest.java 
-java -cp .;junit.jar;hamcrest-core.jarorg.junit.runner.JUnitCore AtomTest+java -cp .;junit.jar org.junit.platform.console.ConsoleLauncher --select-class   AtomTest
 </code> </code>
  
Line 386: Line 387:
  
  
-==== Integrating JUnit into IDE ====+==== Integrating JUnit into an IDE ====