Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: add Example, Usefull Links

...

  • "@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

// TODO: TEST EXAMPLEThis simple example show how to test single method in the InventoryService class.

InventoryService class

This method clear fields after copy.

Code Block
languagejava
themeEclipse
linenumberstrue

    
@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

Code Block
languagejava
themeEclipse
linenumberstrue

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