Overview
Unit testing is a method by which individual units of source code are tested to determine if they are fit for use. A unit is the smallest testable part of an application.
Test Template
Below is a template which you can use to create a new test. All tests should be put inside "src/test/java" source directory of your project.
import static org.mockito.Mockito.mock; // (...) import org.junit.Test; // (...) public class <ClassBeingTestedName>Test { // TESTED CLASS DECLARATION // MOCKED CLASS DECLARATIONS @Before public void init() { // THINGS TO DO BEFORE EACH TEST } @Test public void should<TestDescription> throws Exception { // given // METHOD MOCKS // when // CALLING TESTED CLASS METHOD WHICH YOU WANT TO TEST // then // ASSERTIONS } @After public void flush() { // THINGS TO DO AFTER EACH TEST (optional) } }
Guidelines
- In method with "@Before" annotation you should:
- Initialize tested class. It's always done by using "new ClassName();"
- Mock all classes used by a tested class. It's done by initializing mocked class variables with "mock(ClassName.class);"
- Replace classes used by a tested class with the mocked ones. It's done with "setField(testedClassVariable, "variableInsideTestedClass", mockedClassVariable);"
- During test you need to have full control over mocked class methods used by tested class so you also have to mock them in "given" section
- It's done with "when(mockedClassVariable.methodBeingMocked(arguments)).thenSomething(argument);"
- "thenSomething" mentioned above can be for example "thenReturn"
- Assertions are used to check if method returns value equal to the expected one or if it was called X times
- Checking values: "assertXXX(expectedValue, actualValue);" where "XXX" can be for example replaced by "Equals"
- Cheking amount of method calls: "verify(mockedClassVariable, times(amount)).mockedMethod(arguments);"
- "@Test" annotation has optional parameter "expected" that takes as values subclasses of Throwable
- It's used to verify if class throws the correct exception and annotation above test method looks like that: "@Test(expected=ExceptionName.class)"
Example
This simple example show how to test single method in the InventoryService class.
InventoryService class
This method clear fields after copy.
@Service public class InventoryService { ... public boolean clearGeneratedOnCopy(final DataDefinition dataDefinition, final Entity entity) { entity.setField("fileName", null); entity.setField("generated", "0"); entity.setField("date", null); return true; } ... }
InventoryServiceTest class
We check in this Test method return value and how many times we call setField() method. We mock only Entity beacase we only use it in method
public class InventoryServiceTest { private InventoryService inventoryService; private Entity entity; @Autowired private DataDefinition dataDefinition; @Before public void init() { inventoryService = new InventoryService(); entity = mock(Entity.class); } @Test public void shouldClearGeneratedOnCopy() throws Exception { // given // when boolean bool = inventoryService.clearGeneratedOnCopy(dataDefinition, entity); // then assertTrue(bool); verify(entity, Mockito.times(3)).setField(Mockito.anyString(), Mockito.any()); } }
Usefull links
- eCobertura - Tool to show how many code is covered by tests
- Mockito Documentation