我正在使用Mockito编写Junit Test Case,并且我正在获得NullpoInterException。
在我的代码中,就是这样:
@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 在com.prg.apags.service.impl.organizationserviceimpl.findall(ablisationServiceImpl.java:57( 在com.prg.apags.service.organizationservicetest.testgetallorganization(ablistionServiceTest.java:92( 在sun.reflect.nativemethodaccessorimpl.invoke0(天然方法( 在sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62( 在sun.reflect.delegatingmethodaccessorimpl.invoke(授权methodaccessorimpl.java:43( 在java.lang.reflect.method.invoke(method.java:498( atorg.junit.runners.model.frameworkmethod $ 1.RunreFlectiveCall(frameworkmethod.java:50( at org.junit.internal.runners.model.reflectivecallable.run(ReflectiveCallable.java:12( atorg.junit.runners.model.frameworkmethod.invokeexplosively(frameworkmethod.java:47( atorg.junit.internal.runners.statement.invokemethod.evaluate(InvokeMethod.java:17( atorg.junit.internal.runners.statement.runbefores.evaluate(runbefores.java:26( atorg.junit.runners.parentrunner.runleaf(parentrunner.java:325( atorg.junit.runners.blockjunit4classrunner.runchild(blockJunit4Classrunner.java:78( atorg.junit.runners.blockjunit4classrunner.runchild(blockJunit4Classrunner.java:57( atorg.junit.runners.parentrunner $ 3.run(parentrunner.java:290( at org.junit.runners.parentrunner $ 1.Schedule(pardrunner.java:71( 请访问org.junit.runners.parentrunner.runchildren(parentrunner.java:288( 请访问org.junit.runners.parentrunner.Access $ 000(parentrunner.java:58( atorg.junit.runners.parentrunner $ 2.Evaluate(pardrunner.java:268( at org.junit.runners.parentrunner.run(pardrunner.java:363( 在org.mockito.internal.runners.junit45 andHigherrunnerimpl.run(junit45 andhigherrunnerimpl.java:37( 在org.mockito.runners.mockitojunitrunner.run(MockitoJunitrunner.java:62( atrg.eclipse.jdt.internal.junit4.runner.junit4testreference.run(junit4testreference.java:86( 在org.eclipse.jdt.internal.junit.runner.testexecution.run(testexecution.java:38( atrg.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:538( atrg.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:760( 在org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner.java:460( atrg.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner.java:206(
您永远不会嘲笑或向您的服务注入OrganizationMapper
。InjectMocks
的作用是将您用Mock
注释的内容注入OrganizationServiceImpl
,但是由于您没有在OrganizationMapper
上使用Mock
,因此Mockito不知道它需要使用它。这样的东西应该解决它:
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());
}
}