0
votes

I have tje below mapper, i used services to get entities by code

For the unit tests i mocked these services, but it displays an error 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 at com.softilys.soyouz.service.mapper.EvaluationMapperTest.givenEvaluationDTO_thenReturnEvaluation(EvaluationMapperTest.java:46) 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.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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

1
please add the stacktrace.shalama

1 Answers

1
votes
evaluationDTO.getLevelMaturity().getCode()

This throws NullPointerException since you never assigned it any value.

Set the values in your evaluationDTO . It is just initialized empty.