I am trying to test assertThrows but it’s not working when I am instantiating the class with @Mock Annotation.
<code>@ExtendWith(MockitoExtension.class)
class EmployeeMapperTest {
@Mock
private EmployeeMapper employeeMapper;
@Test
@DisplayName("This test will throw NullPointer Exception when DTO is null")
public void should_throw_npe_when_dto_is_null() {
//given
CreateEmployeeRequestDTO createEmployeeRequestDTO = null;
//then
Exception exp = assertThrows(NullPointerException.class, () -> employeeMapper.dtoToEmployeeEntity(createEmployeeRequestDTO));
assertEquals("Employee DTO cannot be null", exp.getMessage());
}
</code>
<code>@ExtendWith(MockitoExtension.class)
class EmployeeMapperTest {
@Mock
private EmployeeMapper employeeMapper;
@Test
@DisplayName("This test will throw NullPointer Exception when DTO is null")
public void should_throw_npe_when_dto_is_null() {
//given
CreateEmployeeRequestDTO createEmployeeRequestDTO = null;
//then
Exception exp = assertThrows(NullPointerException.class, () -> employeeMapper.dtoToEmployeeEntity(createEmployeeRequestDTO));
assertEquals("Employee DTO cannot be null", exp.getMessage());
}
</code>
@ExtendWith(MockitoExtension.class)
class EmployeeMapperTest {
@Mock
private EmployeeMapper employeeMapper;
@Test
@DisplayName("This test will throw NullPointer Exception when DTO is null")
public void should_throw_npe_when_dto_is_null() {
//given
CreateEmployeeRequestDTO createEmployeeRequestDTO = null;
//then
Exception exp = assertThrows(NullPointerException.class, () -> employeeMapper.dtoToEmployeeEntity(createEmployeeRequestDTO));
assertEquals("Employee DTO cannot be null", exp.getMessage());
}
}
However, if I instantiate the mapper class with new EmployeeMapper instead of Mock Annotation. Test is working fine
<code>private EmployeeMapper employeeMapper = new EmployeeMapper();
</code>
<code>private EmployeeMapper employeeMapper = new EmployeeMapper();
</code>
private EmployeeMapper employeeMapper = new EmployeeMapper();
Below EmployMapper Class where I was throwing the NullPointerException
<code>@Component
public class EmployeeMapper {
public EmployeeEntity dtoToEmployeeEntity(CreateEmployeeRequestDTO employeeDTO) {
if(employeeDTO == null) {
throw new NullPointerException("Employee DTO cannot be null");
}
EmployeeEntity entity = new EmployeeEntity();
entity.setName(employeeDTO.getName());
entity.setEmail(employeeDTO.getEmail());
return entity;
}
}
</code>
<code>@Component
public class EmployeeMapper {
public EmployeeEntity dtoToEmployeeEntity(CreateEmployeeRequestDTO employeeDTO) {
if(employeeDTO == null) {
throw new NullPointerException("Employee DTO cannot be null");
}
EmployeeEntity entity = new EmployeeEntity();
entity.setName(employeeDTO.getName());
entity.setEmail(employeeDTO.getEmail());
return entity;
}
}
</code>
@Component
public class EmployeeMapper {
public EmployeeEntity dtoToEmployeeEntity(CreateEmployeeRequestDTO employeeDTO) {
if(employeeDTO == null) {
throw new NullPointerException("Employee DTO cannot be null");
}
EmployeeEntity entity = new EmployeeEntity();
entity.setName(employeeDTO.getName());
entity.setEmail(employeeDTO.getEmail());
return entity;
}
}