0
votes

I am writing a JUnit test case using Mockito and I am getting a NullPointerException.

In my Code it's like this:

@Service
@Transactional
public class OrganizationServiceImpl implements OrganizationService {

 private final OrganizationRepository organizationRepository;

 private final OrganizationMapper organizationMapper;

 private final OrganizationSearchRepository organizationSearchRepository;

 public OrganizationServiceImpl(OrganizationRepository organizationRepository, OrganizationMapper organizationMapper,
  OrganizationSearchRepository organizationSearchRepository) {
 this.organizationRepository = organizationRepository;
 this.organizationMapper = organizationMapper;
 this.organizationSearchRepository = organizationSearchRepository;
}

 @Override
 @Transactional(readOnly = true)
 public List<OrganizationDTO> findAll() {
  return organizationRepository.findAll().stream().map(organizationMapper::toDto).collect(Collectors.toCollection(LinkedList::new));
 }
}



@RunWith(MockitoJUnitRunner.class)
public class OrganizationServiceTest {

 @Mock
 private OrganizationRepository organizationRepository;

 @InjectMocks
 private OrganizationServiceImpl organizationService;

 OrganizationMapper organizationMapper = OrganizationMapper.INSTANCE;

 @Before
 public void setup() {
    MockitoAnnotations.initMocks(this);
 }

 @Test
 public void testGetAllOrganization() {
    List<Organization> organizationList = Arrays.asList(organizationInit());

    when(organizationRepository.findAll()).thenReturn(organizationList);

    List<OrganizationDTO> result = organizationService.findAll(); //NullPointerException
    assertEquals(3, result.size());
 }
}

java.lang.NullPointerException at com.prg.apags.service.impl.OrganizationServiceImpl.findAll(OrganizationServiceImpl.java:57) at com.prg.apags.service.OrganizationServiceTest.testGetAllOrganization(OrganizationServiceTest.java:92) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37) at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

1
Using the MockitoJUnitRunner and calling MockitoAnnotations.initMocks(this) before the tests is redundant, because the former should have already initialized the mocks and injected them. Maybe try to remove the setup() method?Alexandre Dupriez
i removed the setup() method but it show me the same errorAymen Kanzari

1 Answers

0
votes

You're never mocking or injecting OrganizationMapper into your Service. What InjectMocks does is inject what you annotate with Mock into your OrganizationServiceImpl, but since you're not using Mock on OrganizationMapper, mockito doesn't know that it needs to use it. Something like this should fix it:

public class OrganizationServiceTest {

 private OrganizationRepository organizationRepository;

 private OrganizationServiceImpl organizationService;

 private OrganizationSearchRepository searchRepository;

 private OrganizationMapper organizationMapper = OrganizationMapper.INSTANCE;

 @Before
 public void setup() {
    organizationRepository = Mockito.mock(OrganizationRepository.class);
    searchRepository = Mockito.mock(OrganizationSearchRepository.class);
    organizationService = new OrganizationServiceImpl(organizationRepository, organizationMapper, searchRepository);
 }

 @Test
 public void testGetAllOrganization() {
    List<Organization> organizationList = Arrays.asList(organizationInit());

    when(organizationRepository.findAll()).thenReturn(organizationList);

    List<OrganizationDTO> result = organizationService.findAll();
    assertEquals(3, result.size());
 }
}