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/02/23 10:50] – Fixed the description of assertThrows() bernstdh
Line 155: Line 155:
    */    */
   @Test   @Test
-  public void constructor_IllegalArguments()  +  public void constructor_IllegalArguments()
-          throws IllegalArgumentException+
   {   {
     assertThrows(IllegalArgumentException.class, () -> {new Atom("O", -8, -16);});     assertThrows(IllegalArgumentException.class, () -> {new Atom("O", -8, -16);});
Line 163: Line 162:
  
 This approach uses a //Lambda expression//, a representation of a class with a single-method.  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.) 
  
  
Line 180: Line 178:
  
       // 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 224: Line 222:
  
  
-==== The @Before Annotation ====+==== The @BeforeEach and @BeforeAll Annotations ====
  
  
Line 230: Line 228:
  
  
-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 238:
  
 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 260:
  
 <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 274:
  
 <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 300:
 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 308:
 <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 324:
  
  
-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 332:
 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 367:
 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 374:
  
 <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 385:
  
  
-==== Integrating JUnit into IDE ====+==== Integrating JUnit into an IDE ====