我在mapper下方,我使用服务通过代码获取实体
对于单元测试,我嘲笑了这些服务,但是它显示错误java.lang.NullPointerException
@Mapper(uses = { LevelMaturityService.class, SatisfactionLevelService.class })
public interface EvaluationMapper extends EntityMapper<EvaluationDTO, Evaluation> {
@Mapping(source = "levelMaturity.code", target = "levelMaturity", qualifiedByName = { "levelMaturityService", "getLevelMaturityByCode" } )
@Mapping(source = "satisfactionLevel.code", target = "satisfactionLevel", qualifiedByName = { "satisfactionLevelService", "getSatisfactionLevelByCode" })
Evaluation toEntity(EvaluationDTO evaluationDTO);
}
public class EvaluationMapperTest {
private EvaluationMapper evaluationMapper = InstanceMapperConstant.EVALUATION_INSTANCE;
@Mock
private LevelMaturityService levelMaturityService;
@Mock
private SatisfactionLevelService satisfactionLevelService;
private static EvaluationInit evaluationInit;
@BeforeClass
public static void setUp() throws Exception {
evaluationInit = new EvaluationInit();
}
@Test
public void givenEvaluationDTO_thenReturnEvaluation() {
EvaluationDTO evaluationDTO = evaluationInit.buildEvaluationDTOWithId();
when(levelMaturityService.getOne(evaluationDTO.getLevelMaturity().getCode())).thenReturn(Mockito.any(LevelMaturity.class)); // error
when(satisfactionLevelService.getOne(evaluationDTO.getSatisfactionLevel().getCode())).thenReturn(Mockito.any(SatisfactionLevel.class));
Evaluation evaluation = evaluationMapper.toEntity(evaluationDTO);
assertEquals(evaluationDTO.getBusinessValueGroup(), evaluation.getBusinessValueGroup());
}
}
java.lang.NullPointerException在 com.softilys.soyouz.service.mapper.EvaluationMapperTest.givenEvaluationDTO_thenReturnEvaluation(EvaluationMapperTest.java:46) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:498)在 org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)处 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 在org.junit.runners.ParentRunner处$ 3.run(ParentRunner.java:290)在 org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)在 org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)在 org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)在 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 在org.junit.runners.ParentRunner.run(ParentRunner.java:363)处 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89) 在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
答案 0 :(得分:1)
evaluationDTO.getLevelMaturity().getCode()
由于您从未分配任何值,因此会抛出NullPointerException
。
在evaluationDTO
中设置值。它只是初始化为空。