i wanted to write some tests for the method shown in the code below. I'm mocking dependencies with JMockit. For whatever reason i am getting a NullPointerException all the time and i really don't understand why that happens. I'm new to JMockit and in mocking dependencies in general. Stack Trace just shows NullPointerException thrown at line new NonStrictExpectations()
.
Method to test:
#Override
public boolean addSubject(User user, Schedule schedule, SchoolSubject subject) {
final boolean result = schedule.addSubject(subject.getHourTime(), subject);
scheduleDAO.update(schedule);
if (subject.getTeacher() != null && !subject.getTeacher().trim().isEmpty()) {
for (final TeacherEntry teacher : user.getTeachers()) {
if (subject.getTeacher().equals(teacher.getName())) {
teacher.getSubjects().add(subject.getName());
teacherDAO.update(teacher);
}
}
}
try {
userDAO.update(user);
} catch (final DuplicateUniqueFieldException e) {
throw new UnexpectedUniqueViolationException(e);
}
}
Test method:
//imports not copied
public class ScheduleManagerTest {
#Tested
ScheduleManager manager;
#Injectable
UserDAO userDAO;
#Injectable
ScheduleDAO scheduleDAO;
#Injectable
TeacherEntryDAO teacherDAO;
#Injectable
SchoolSubjectDAO schoolSubjectDAO;
#Mocked
Schedule schedule;
#Mocked
SchoolSubject subject;
#Mocked
User user;
#Test
public void testAddSubject() throws DuplicateUsernameException, DuplicateEmailException {
new NonStrictExpectations() {
{
schedule.addSubject(anyInt, (SchoolSubject) any);
result = true;
scheduleDAO.update((Schedule) any);
subject.getTeacher();
result = anyString;
subject.getTeacher().trim().isEmpty();
result = false;
user.getTeachers();
result = (ArrayList<TeacherEntry>) any;
teacherDAO.update(((TeacherEntry) any));
userDAO.update((User) any);
};
};
assertTrue(manager.addSubject(user, schedule, subject));
}
}
I think i'm doing something pretty wrong :(
It's hard to know what is going wrong based on the description, but here's a guess:
Mocked objects will return null in their methods by default. You'll get a null pointer here:
subject.getTeacher().trim()
because getTeacher() will return null. To return a teacher, you'll have to either use a real subject or do further mocking with
Mockito.when(subject.getTeacher()).thenReturn(new Teacher());
You need to add #RunWith(JMockit.class) to your class
#RunWith(JMockit.class)
public class ScheduleManagerTest {
...
Related
I am getting a NPE when the first when(creditApplicationCache.getObject("")).thenRetun(response) is called. creditApplicationCache is null.
I am using mockito 2.17.jar.
I have added the #RunWith(MockitoJUnitRunner.class).
I have also tried with MockitoAnnotations.initMocks(this).
But nothing has helped. CreditApplicationCache is not getting initialized.
#RunWith(MockitoJUnitRunner.class)
public class WebServiceClientTest {
#InjectMocks
WebServiceClientImpl webServiceClientImpl;
#Mock
private ApplicationCacheImpl creditApplicationCache;
/*#Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}*/
#Test
public void testWebServiceClient() throws BusinessException, SystemException {
WebServiceClientImpl webServiceClientImpl = new WebServiceClientImpl();
CreditCardDetailsResponse creditCardDetailsResponse = new CreditCardDetailsResponse();
creditCardDetailsResponse.setAccountNumber("1234567");
when(creditApplicationCache.getObject("XXXXXXX")).thenReturn(creditCardDetailsResponse);
when(webServiceClientImpl.getCreditCardDetails("1234")).thenReturn(creditCardDetailsResponse);
CreditCardDetailsResponse mockResponse = webServiceClientImpl.getCreditCardDetails("1234");
assertEquals(creditCardDetailsResponse.toString(),mockResponse.toString());
}
}
---------------------------------------------------------------------------------------------------------
WebServiceClientImpl.java
public class WebServiceClientImpl implements WebServiceClient {
#Autowired
private ApplicationCache creditApplicationCache;
public CreditCardDetailsResponse getCreditCardDetails(String id)
throws BusinessException, SystemException {
// Some Code
CreditCardDetailsResponse response = null;
response = (CreditCardDetailsResponse) creditApplicationCache.getObject(cacheKey);
if (response == null) {
try {
// More Code }
return response ;
}
}
Your problem is that you are creating the client again on the first line of the test: WebServiceClientImpl webServiceClientImpl = new WebServiceClientImpl();
#InjectMocks already constructs the annotated class, and injects all the relevant mocks, but what you do is re-create it without it, hence the NPE.
Simply remove that line and it should work
This issue might occur, when you import #Test from org.junit.jupiter.api.Test instead of org.junit.Test. Can you ensure, that you are using the correct #Test annotation?
I am trying to write unit tests for Repository layer classes with Junit and Mockito.
I have mocked the base class that supplies NamedParameterJdbcOperations and tried to inject into the repo class.
In the repo class, we are loading sql queries from files on classpath. This is done in a method that is annotated with #PostConstruct.
When trying to test a method of the repo, it is not able to find or load the query and thus throwing NullPointerException.
Need help / suggestion on how to deal with such scenario.
PS: I am not allowed to change the repo class implementation.
Attaching the code of repo and test class for reference.
RepositoryImpl.java
#Repository
public class RepositoryImpl extends AppJdbcImpl implements
Repository {
private static final StudentMapper STUDENT_ROW_MAPPER = new StudentMapper();
private static final CourseMapper COURSE_ROW_MAPPER = new CourseMapper();
#Value("classpath:sql/sql1.sql")
private Resource sql1;
private String query1;
#Value("classpath:sql/sql2.sql")
private Resource sql2;
private String query2;
public RepositoryImpl() { }
public RepositoryImpl(NamedParameterJdbcOperations jdbc) {
super(jdbc);
}
#PostConstruct
public void setUp() {
query1 = loadSql(sql1);
query2 = loadSql(sql2);
}
public Iterable<Course> findCoursesByStudentId(int studentId) throws
DataAccessException {
try {
return jdbc().queryForObject(query1,
ImmutableMap.of("studentId", studentId),
COURSE_ROW_MAPPER);
} catch (EmptyResultDataAccessException emptyResult) {
return null;
} catch (DataAccessException e) {
// Need to create exception classes and throw specific exceptions
throw e;
}
}
public Iterable<Student> findStudentsByCourseId(int courseId) throws DataAccessException {
try {
return jdbc().query(query2,
ImmutableMap.of("courseId", courseId),
STUDENT_ROW_MAPPER);
} catch (DataAccessException e) {
// Need to create exception classes and throw specific exceptions
throw e;
}
}
private String loadSql(Resource resource) {
try {
return CharStreams.toString(new InputStreamReader(resource.getInputStream()));
} catch (IOException e) {
return null;
}
}
}
RespositoryImplTest.java
#RunWith(MockitoJUnitRunner.class)
public class RepositoryImplTest {
#Mock
private NamedParameterJdbcOperations jdbc;
#Mock
private ResultSet resultSet;
#Mock
private StudentMapper studentMapper;
#Mock
private CourseMapper CourseMapper;
#InjectMocks
private RepositoryImpl repository;
private Student student1;
private Student student2;
private Course course1;
private Course course2;
#Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
course1 = new Course(1, "Karate");
course2 = new Course(2, "Riding");
course8 = new Course(8, "Swimming");
List<Course> courseList = Arrays.asList(course1, course2, course8);
student1 = new Student(1, "Chuck", "Norris", 27, new Arrays.asList(course1, course2));
student2 = new Student(2, "Bruce", "Lee", 54, new Arrays.asList(course1, course8));
List<Student> studentList = Arrays.asList(student1, student2);
when(jdbc.queryForObject(Matchers.anyString(), anyMap(),
isA(StudentMapper.class)))
.thenAnswer(new Answer() {
#Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
Object[] args = invocationOnMock.getArguments();
int queryParam = Integer.parseInt(args[0].toString());
Iterable<Credentials> result = studentList.stream()
.filter(d -> d.getId() == queryParam)
.collect(Collectors.toList());
return result;
}
});
}
#Test
public void findCoursesByStudentId() {
Iterable<Course> result = repository.findCoursesByStudentId(1);
assertNotNull(result);
}
}
In repo class, exception is thrown as query1 is null.
Need help to properly solving the issue.
Thanks, Baru
#RunWith(MockitoJUnitRunner.class)
you start test with mockito starter, not spring starter. It's mean that spring not provided you beans. Mockito starter nothing know about PostConstruct annotation.
You may call PostConstruct method youself in sturUp junit method or in test method.
I have a class which I would like to test with a public static method that contains some chained method calls. Assuming that an exception occurs during the chained method calls, how do I handle this effectively and make it return some specific value?
Following is the code sample of the test class.
#RunWith(PowerMockRunner.class)
#PrepareForTest({CodeWithPrivateMethod.class,CodeWithAnotherPrivateMethod.class,CodeWithYetAnotherPrivateMethod.class})
public class CodeWithPrivateMethodTest {
#Test
public void when_gambling_is_true_then_always_explode() throws Exception {
CodeWithYetAnotherPrivateMethod codeWithYetAnotherPrivateMethod = PowerMockito.spy(new CodeWithYetAnotherPrivateMethod());
PowerMockito.whenNew(CodeWithYetAnotherPrivateMethod.class).withAnyArguments().thenReturn(codeWithYetAnotherPrivateMethod);
CodeWithAnotherPrivateMethod codeWithAnotherPrivateMethod = PowerMockito.spy(new CodeWithAnotherPrivateMethod());
PowerMockito.whenNew(CodeWithAnotherPrivateMethod.class).withAnyArguments().thenReturn(codeWithAnotherPrivateMethod);
PowerMockito.doReturn(true).when(codeWithYetAnotherPrivateMethod, "getGambling");
//PowerMockito.doReturn(codeWithYetAnotherPrivateMethod).when(codeWithAnotherPrivateMethod, "getGambleValue");
PowerMockito.spy(CodeWithPrivateMethod.class);
CodeWithPrivateMethod.startGamble();
}
}
Following is the code sample for the class under test
public class CodeWithPrivateMethod {
public static void startGamble() {
Boolean gamble = CodeWithAnotherPrivateMethod.getGambleValue()
.getGambling();
if (gamble) {
System.out.println("kaboom");
}else{
System.out.println("boom boom");
}
}
}
Following is the code sample for the class that gets called from the class under test
public class CodeWithAnotherPrivateMethod {
static CodeWithYetAnotherPrivateMethod codeWithYetAnotherPrivateMethod = new CodeWithYetAnotherPrivateMethod();
public static CodeWithYetAnotherPrivateMethod getGambleValue() {
return codeWithYetAnotherPrivateMethod; //works fine
return null; // fails
}
}
Following is the code sample for the other class that gets called from the class under test
public class CodeWithYetAnotherPrivateMethod {
public Boolean getGambling() {
return false;
}
}
So Assuming I return a null value from getGambleValue() method of CodeWithAnotherPrivateMethod class, how do I handle this null value effectively in my testclass?
This is how to specify expected exceptions using Mockito:
#Test(expected = NullPointerException.class)
public void when_gambling_is_true_then_always_explode() throws Exception {
...
Before I found out about this I would do:
#Test
public void when_gambling_is_true_then_always_explode() throws Exception {
// setup omitted
try {
CodeWithPrivateMethod.startGamble();
}
catch(NullPointerException e) {
// expected
return;
}
fail("Expected NullPointerException");
}
EDIT: Testing multiple classes that call each other statically like this is a severe code smell. Unit tests should test a single class and inline static calls should be limited to utility classes.
Another comment: your example class names are very confusing. Next time please stick with Foo, Bar, Baz or Appple, Pear, Banana.
If you are not getting an NPE then I expect your mocking/spying is interfering. If you call the code under test without mocking/spying the call chain would be:
CodeWithPrivateMethod.startGamble();
->
CodeWithYetAnotherPrivateMethod value = CodeWithAnotherPrivateMethod.getGambleValue();
->
return null;
<-
value.getGambling();
<- throws NullPointerException
What exactly are you trying to find out or achieve?
EDIT: Here's how it should work with PowerMock
#RunWith(PowerMockRunner.class)
#PrepareForTest(CodeWithAnotherPrivateMethod.class)
public class CodeWithPrivateMethodTest {
#Mock
private CodeWithYetAnotherPrivateMethod yetAnotherInstance;
#Test
public final void testStartGamble() {
// SETUP
mockStatic(CodeWithAnotherPrivateMethod.class);
expect(CodeWithAnotherPrivateMethod.getGambleValue())
.andReturn(yetAnotherInstance);
Boolean gamblingValue = true;
expect(yetAnotherInstance.getGambling()).andReturn(gamblingValue);
replayAll();
// CALL
CodeWithPrivateMethod.startGamble();
// VERIFY
verifyAll();
}
I'm trying to understand JMockit but still I'm running into walls.
This is a class I want to test:
#Stateless
public class VerfahrensArchivService {
#PersistenceContext
private EntityManager em;
public void storeAndUpdateVerfahren(List<Verfahren> pVerfahrenToStore) {
if (pVerfahrenToStore == null){
throw new IllegalArgumentException("pVerfahrenToStore darf nicht null sein!");
}
for (Verfahren verfahren : pVerfahrenToStore) {
Verfahren storedCopy = getVerfahrenByExterneID(verfahren.getFremdsystem(), verfahren.getExterneId());
if (storedCopy == null){
//Ein neues Verfahren wurde gefunden!
em.persist(verfahren);
}else{
}
}
}
}
This is how my test looks like:
public class VerfahrensArchivServiceTest {
#Tested
private VerfahrensArchivService archiveService;
#NonStrict //Also tried simple #Mocked
private EntityManager em;
#Test
public void should_store_new_verfahren_to_persistence_layer(){
List<Verfahren> listeMitEinemNeuenVerfahren = new ArrayList<Verfahren>();
Verfahren v = new Verfahren();
v.setId(0);
v.setExterneId("Neu");
v.setFremdsystem(Verfahren.FREMDSYSTEM_P);
listeMitEinemNeuenVerfahren.add(v);
new NonStrictExpectations(archiveService) {
{
//simulate that nothing was found in the db
archiveService.getVerfahrenByExterneID(anyString, anyString);result = null;
}
};
new Expectations() {
{
em.persist(any);
}
};
archiveService.storeAndUpdateVerfahren(listeMitEinemNeuenVerfahren);
}
}
The test fails because EntityManager is null in the moment of calling em.persist().
Why?
Is the test structured the right way? If not, could you show me how I do better?
I really believe that JMockit will help me be more productive in a TDD way. But I need to understand how to use it correctly.
I read the #Tested Javadoc little bit more carefully. There it states, that you could use test method parameters to setup the #Tested/class under test. Just declare a #Incectable parameter of the type you need. Read the #Tested javadoc to find out how the params are matched to the uninitialized fields.
so, my test works with this:
#Test
public void should_store_new_verfahren_to_persistence_layer(#Injectable final EntityManager em){
List<Verfahren> listeMitEinemNeuenVerfahren = new ArrayList<Verfahren>();
Verfahren v = new Verfahren();
v.setId(0);
v.setExterneId("Neu");
v.setFremdsystem(Verfahren.FREMDSYSTEM_P);
listeMitEinemNeuenVerfahren.add(v);
new NonStrictExpectations(archiveService) {
{
archiveService.getVerfahrenByExterneID(anyString, anyString);result = null;
}
};
new Expectations() {
{
em.persist(any);
}
};
archiveService.storeAndUpdateVerfahren(listeMitEinemNeuenVerfahren);
}
I have been trying to run the following test using mockito and junit and I keep on getting "java.lang.NullPointerException: name must not be null"
Can anyone tell me why this is happening?
On debugging, I found out that this exception is thrown when the test executes the following statement in isStopValid(String) method:
FacilityValidationUtil facUtil = new FacilityValidationUtil();
#RunWith(MockitoJUnitRunner.class)
public class MyFormTest{
#InjectMocks MyForm form = new MyForm();
#Mock FacilityValidationUtil facUtil;
#Test
public void testIsStopValid() throws FinderException{
when(facUtil.isFacilityValid("")).thenReturn(false);
form.setOrigin("");
assertEquals(false, form.isStopValid(form.getOrigin()));
}
}
Class with method to be tested:
public class MyForm{
FacilityValidationUtil facUtil = new FacilityValidationUtil();
public boolean isStopValid(String stop){
try {
return facUtil.isFacilityValid(stop);
} catch (FinderException e) {
log.error("Error finding the stop. "+e.getCause());
return false;
}
}
}
public class FacilityValidationUtil{
private FacilityDAO facilityDao = new HibernateFacilityDAO();
public boolean isFacilityValid(String facility) throws FinderException{
boolean test;
FacilityImpl facilityImpl = facilityDao.findFacilityByNassCode(facility);
test = (facilityImpl==null)?false : true;
return test;
}
}
public class HibernateFacilityDAO extends HibernateAbstractDeltaDAO implements FacilityDAO {
public HibernateFacilityDAO() {
super(false);
}
}
Short Answer: You are trying to mock a variable (facUtil) that is local to your isStopValid method, so the mock version of this object in your test is never going to be called because you are 'newing it up" each time.
Long Answer: It looks like you are trying to mock the call to your FacilityValidationUtil class, and if this is the case, then you need to either make the class a field so that Mockito can inject the object by reflection (if this object is thread safe, which it looks like it is) or explore a mocking framework like PowerMockito that will allow you to mock a constructor (google for PowerMockito when new).
PowerMockito.whenNew(FacilityValidationUtil.class).withNoArguments().thenReturn(facUtil);
Mockito doesn't support any mocking of constructor args by default.
EDIT
If you are still having trouble, then I would suggest starting with a smaller example. I've put together one for you that works and uses the code you are trying to test (It's using inner classes though, which Mockito has some quirky rules about, but I'm just doing it to compress the example).
#RunWith(MockitoJUnitRunner.class)
public class MyFormTest {
#InjectMocks
private MyForm form = new MyForm();
#Mock
private FacilityValidationUtil facUtil;
#Test
public void testIsStopValid_false() {
when(facUtil.isFacilityValid("")).thenReturn(false);
assertEquals(false, form.isStopValid(""));
}
#Test
public void testIsStopValid_true() {
when(facUtil.isFacilityValid("")).thenReturn(true);
assertEquals(true, form.isStopValid(""));
}
public class MyForm {
private FacilityValidationUtil facUtil = new FacilityValidationUtil();
public boolean isStopValid(String stop) {
try {
return facUtil.isFacilityValid(stop);
} catch (FinderException e) {
return false;
}
}
}
public class FacilityValidationUtil {
public boolean isFacilityValid(String facility) throws FinderException {
throw new RuntimeException(facility);
}
}
public class FinderException extends RuntimeException {
public FinderException(String message) {
super(message);
}
}
}
What's really important is that your mock is not getting injected correctly. Until you get that resolved, you are going to keep getting the same error. Set a break point in your MyForm at the point you call facUtil.isFaciltyValid and look at the object. It should be a mockito object, not your class.
Good luck.