Import statement
import org.junit.*

– behavior testing: test if methods are called with the correct input parameters
– state testing: validate the result

– regression test: identify software regressions introduced by changes in the code
– integration test (also functional test): test the behavior of a component or the integration between a set of components. Test resembles user interaction with the application.
– performance test: test application’s performance under high load

– code under test, application under test
– test fixture: test inputs
– test coverage
– asserts or assert statements: assert methods checking an expected result versus the actual result

src/main/java – for Java classes
src/test/java – for test classes

Naming convention
– “Test” suffix in class names
– test method name should explain what the test does
— use “should” in the test method name
— “Given[ExplainYourInput]When[WhatIsDone]Then[ExpectedResult]”

Execute tests from the command line
– add the JUnit library JAR file to the classpath
– in main class write:

Result result = JUnitCore.runClasses(MyClassTest.class);
for (Failure failure : result.getFailures()) {

Order of execution
Well-written test code should not assume any order, i.e., tests should not depend on other tests.
@FixMethodOrder(MethodSorters.NAME_ASCENDING) (also MethodSorters.DEFAULT, MethodSorters.JVM)
@Before – executed before testes (place to set up variables,…)
@Test – testing
@After – executed after tests (place to close connections, delete temporary data, restore defaults,…)
When multiple methods have same annotations, order of execution of methods with same annotation is not guaranteed.

public void setup(){}
public void testPattern() {}
public void testContainsNumbers() {}


Annotation Description Example of use
@Test mark testing method use assert methods to test your code
@Before executed before each test set variables, initialize class,…
@After executed after each test delete temporary data, restore defaults,…
@BeforeClass Executed once, before the start of all tests. Method marked with this annotation must be static! for more intensive tasks: connect to a database,…
@AfterClass Executed once, after all tests finnished. Method marked with this annotation must be static! for clean-up tasks: close connections,…
@Ignore/@Ignore(“reason why”) disable test test hasn’t been adapted to the recent code change, test execution is too long,…
@Test(expected = Exception.class) expect this test to throw an exception
@Test(timeout=100) fails if this test runs longer than 100 milliseconds


Method Description Example of use
fail method will always fail – used e.g. before certain code in implemented. fail(“optional message”);
assertTrue check if condition is true assertTrue(“optional message”, condition);
assertFalse check if condition is false assertFalse(“optional message”, condition)
assertEquals check if two values are same (in case of array, the reference is checked not the content of the array) assertEquals(“optional message”, expected, actual);/td>
assertEquals with tolerance check if float and double match, where tolerance is the number decimals which must be the same assertEquals(“optional message”, expected, actual, tolerance);
assertNull check if the object is null assertNull(“optional message”, object)
assertNotNull check if the object is not null assertNotNull(“optional message”, object)
assertSame check if both variables refer to the same object assertSame(“optional message”, expected, actual)
assertNotSame check if both variables refer to different objects assertNotSame(“optional message”, expected, actual)

fail(“optional message”);

Test suite = several test classes combined in one suite

        MySecondClassTest.class })

Parameters in class
Define a class with one method which will use specified parameters.
Mark class with @RunWith(Parameterized.class).
This class must contain one static method marked with @Parameters.