JUnit testing Tutorial Example, from the DevX tutorial linked from the class web page
Code under test: (simplified)
package calc;
public class Calculator {
private static int result; // Static variable where the result is stored
public void add(int n) {
result = result + n;
}
public void substract(int n)
{
result = result - 1; //Bug : should be result = result - n
}
public void divide(int n) {
result = result / n;
}
public void clear() { // Cleans the result
result = 0;
}
int getResult() {
return result;
}
}
JUnit4 test code: mark tests with annotation @Test, test setup with @Before:
package junit4; <--also can put the test in the same package
import calc.Calculator;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
public class CalculatorTest {
private static Calculator calculator = new Calculator();
@Before
public void clearCalculator() {
calculator.clear();
}
@Test
public void add() {
calculator.add(1);
calculator.add(1);
assertEquals(calculator.getResult(), 2);
}
@Test
public void subtract() {
calculator.add(10);
calculator.subtract(2);
assertEquals(calculator.getResult(), 8);
}
@Test
public void divide() {
calculator.add(8);
calculator.divide(2);
assert calculator.getResult() == 5;
}
@Test(expected = ArithmeticException.class)
public void divideByZero() {
calculator.divide(0);
}
@Ignore("not ready yet")
@Test
public void multiply() {
calculator.add(10);
calculator.multiply(10);
assertEquals(calculator.getResult(), 100);
}
}
Important idea: tests are independent, and all start from a known program state.
That's why above @Before clears the calculator, so nothing is left over from the last test.
Running from the command line: need to enable Java asserts with -ea:
java –ea org.junit.runner.JUnitCore junit4.CalculatorTest
JUnit version 4.1
...E.EI
There were 2 failures:
1) subtract(junit4.CalculatorTest)
java.lang.AssertionError: expected:<9> but was:<8>
at org.junit.Assert.fail(Assert.java:69)
2) divide(junit4.CalculatorTest)
java.lang.AssertionError
at junit4.CalculatorTest.divide(CalculatorTest.java:40)
FAILURES!!!
Tests run: 4, Failures: 2
Ant integration:
Since the DevX tutorial was written, the ant task has been upgraded to
JUnit4. Here is the ant task in pizza1. It also works for JUnit3 tests.
It just does the above command for you.
<target name="testDAO1" depends="build">
<junit fork="yes">
<formatter type="brief" usefile="false"/>
<test name="cs636.pizza.dao.PizzaOrderDAOTest1"/>
<classpath refid="project.classpath"/>
</junit>
</target>
Output from this:
testDAO1:
[junit] Testsuite: cs636.pizza.dao.PizzaOrderDAOTest1
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.063 sec
[junit]
[junit] ------------- Standard Output ---------------
[junit] Connecting using driver org.hsqldb.jdbcDriver, connection string jdbc:hsqldb:hsql://localhost/
[junit] Connecting using driver org.hsqldb.jdbcDriver, connection string jdbc:hsqldb:hsql://localhost/
[junit] ------------- ---------------- ---------------
Eclipse integration
As shown in the demo last week, all you need to do is right-click on
the project and select Run as JUnit test, and all tests of the project
will be run, showing green bar for success and red bar for failure.
@BeforeClass, @AfterClass: To
save time, parts of the setup that need to be done only once can be put
in the @BeforeClass. For us, the original database connection and
setup should be done here.
Note that @BeforeClass, @AfterClass must be public and static, unlike @Before, @After, which must be public and non-static.