More about JUnit

Download Report

Transcript More about JUnit

More About JUnit
26-Jul-16
Test suites


A test suite is a group of JUnit tests
You can create a test suite in Eclipse as follows:






File  New  Other...  Java/JUnit  TestSuite
Next >
Change the name if you like (default is AllTests)
Select the JUnit tests you want included (default is all)
Finish
You can run your test suite by selecting AllTests.java
(or whatever you named it) and choosing Run  Run
as  JUnit Test
Sample test suite

Here is the code Eclipse generates for a project that I’m currently
working on:

public class AllTests {
public static Test suite() {
TestSuite suite = new TestSuite("Test for default package");
suite.addTest(new TestSuite(TokenizerTest.class));
suite.addTest(new TestSuite(NodeTest.class));
suite.addTest(new TestSuite(ParserTest.class));
suite.addTest(new TestSuite(TreeTest.class));
}


}
return suite;
As you can see, this is ordinary Java code
You can easily add more tests by writing similar lines of code
Testing exceptions




If your code should sometimes throw Exceptions, you
need to test that it does throw them
There are special tests in JUnit for Exceptions
However, I think it’s easier just to put code like the
following into your test case:
try {
// call to a method that should throw an Exception
fail("Did not throw an ExpectedException");
}
catch (ExpectedException e) { }
The End