JUnit
JUnit
JUnit
James Brucker
What to Test?
In unit testing, we test functions or methods in classes.
Repeatable
import org.junit.*;
import static org.junit.Assert.* ; // import names of all static methods
public StackTest {
@Test
public void testStackConstructor( ) {
Stack stack = new Stack(5);
assertEquals("Stack should be empty", 0, stack.size() );
assertEquals("Capacity should be 5", 5, stack.capacity() );
assertFalse( stack.isFull() );
assertTrue( stack.isEmpty() );
}
What can you Assert ?
JUnit Assert class provides many assert methods
Assert.assertTrue( 2*2 == 4 );
Assert.assertFalse( "Stupid Slogan", 1+1 == 3 );
Assert.assertEquals( new Double(2), new Double(2));
Assert.assertNotEquals( 1, 2 );
Assert.assertSame( "Yes", "Yes" ); // same object
Assert.assertNotSame("Yes", new String("Yes") );
double[] a = { 1, 2, 3 };
double[] b = Arrays.copyOf( a, 3 );
Assert.assertArrayEquals( a, b );
Assert.assertThat( patternMatcher, actualValue );
Use import static Assert.*
Tests almost always use static Assert methods:
@Test
public void testInsert( ) {
Assert.assertTrue( 1+1 == 2 );
// assertSame(a,b) tests a == b
setUp( ) setUp( )
tearDown( ) tearDown( )
Using @Before and @After
You want a clean test environment for each test.
This is called a "test fixture". Use @Before to initialize a test
fixture. Use @After to clean up.
private File file; // fixture for tests writing a local file
@Before
public void setUp( ) {
file = new File( "/tmp/tempfile" );
}
@After
public void tearDown( ) {
if ( file.exists() ) file.delete();
}
Testing for an Exception
you can indicate that a test should throw an exception.
@Test( expected=StackException.class )
public void testPopEmptyStack() {
Stack stack = new Stack(3);
Object x = stack.pop( );
}
Limit the Execution Time
specify a time limit (milliseconds) for a test
if time limit is exceeded, the test fails
@Test
public void testWithdrawStrategy() {
//TODO write this test
fail( "Test not implemented yet" );
}
What to Test?
Test BEHAVIOR not just methods.
// ...continued
Method defines parameter values
@Parameterized.Parameters
public static Collection makeTestValues() {
// collection of (input,result) pairs
return Arrays.asList( new Object[][] {
{2, true},
{3, true},
{4, false},
{19, true},
{21, false},
...
});
}
Tutorial: https://www.baeldung.com/junit-params
Using JUnit in BlueJ
1. From "Tools" menu select "Preferences..."
2. Select "Miscellaneous" tab.
3. Select "Show unit testing tools".
Using JUnit in Eclipse
Eclipse includes JUnit 3.8 and 4.x libraries
you should use Junit 4 on your projects
eclipse will manage running of tests.
but, you can write your own test running in the main method
Select a source file to test and then...
Using JUnit in Eclipse (2)
Select test options and methods to test.
Using JUnit in Eclipse (3)
/** Test of the Purse class
* @author James Brucker
*/
Write your test cases.
public class PurseTest {
Eclipse can't help much
private Purse purse; with this.
private static final int CAPACITY = 10;
/** create a new purse before each test */
@Before
public void setUp() throws Exception {
purse = new Purse( CAPACITY );
}
@Test
public void testCapacity() {
assertEquals("capacity wrong",
CAPACITY, purse.capacity());
}
Run JUnit in Eclipse (4)
JUnit 4 in 10 Minutes
on JUnit web site
Other Software for Testing
JUnit 5 - The new version of JUnit