I have a class, which I want to unit test using mockito:
public class ServiceImpl {
@Autowired
private TestDao testDao;
@Transactional
public void setData(Long id) {
TestClass testClass = testDao.findOne(id);
if (testClass != null) {
testClass.setStatus(true);
}
}
}
I need to test this class. So I have created the Test class as below:
@RunWith(MockitoJUnitRunner.class)
public class ServiceImplTest{
@Mock
private TestDao testDao;
@InjectMocks
private ServiceImpl service;
@Test
public void setData_success() {
TestClass testClass = new TestClass();
when(testDao.findOne(1L)).thenReturn(testClass);
service.setData(1L);
assertTrue(testClass.getStatus());
}
}
The above scenario is when the testClass object is present. Similarly i want to test, the scenario when the testClass object is null. Something like:
@Test
public void setData_testClass_null() {
TestClass testClass = null;
when(testDao.findOne(1L)).thenReturn(testClass);
service.setData(1L);
//how to I verify here that testClass.setStatus(true) is never called????
//or should I just ignore the test case?
}