Next revision | Previous revisionNext revisionBoth sides next revision |
student:junit:v5 [2018/08/14 10:45] – created bernstdh | student:junit:v5 [2020/02/23 10:44] – Fixed example of assertThrows() bernstdh |
---|
*/ | */ |
@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);}); |
| |
// 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) |
| |
| |
==== The @Before Annotation ==== | ==== The @BeforeEach and @BeforeAll Annotations ==== |
| |
| |
| |
| |
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 |
| |
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 ==== |
| |
<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> |
| |
| |
<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> |
| |
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. |
<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> |
| |
| |
| |
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%%''. |
| |
| |
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> |
| |
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. |
| |
<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> |
| |
| |
| |
==== Integrating JUnit into a IDE ==== | ==== Integrating JUnit into an IDE ==== |
| |
| |