首页 > 解决方案 > ModelMapper JUnit Mockito 抛出 NullPointerException

问题描述

我正在尝试在服务类中测试一个方法,该方法使用 ModelMapper 将实体转换为 dto,但我在mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);服务类的这一行得到 NullPointerException。

例外

java.lang.NullPointerException
at pro.budthapa.service.impl.StockCategoryServiceImpl.getStockCategoryByIdAndBusinessGroupId(StockCategoryServiceImpl.java:56)
at pro.budthapa.service.impl.StockCategoryServiceImplTest.WhenCategoryPresent_ShouldReturnCategory(StockCategoryServiceImplTest.java:81)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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)

测试班

@RunWith(MockitoJUnitRunner.class)
public class StockCategoryServiceImplTest {

@Mock
private ModelMapper mapper;

@Mock
private StockCategoryRepository stockCategoryRepository;

@InjectMocks
private StockCategoryServiceImpl stockCategoryService;

@Before
public void setup() {
    mapper = new ModelMapper();
}

@Test
public void WhenCategoryPresent_ShouldReturnCategory() throws Exception {
    int bgId = 10;
    int categoryId = 5;

    StockCategory sc = new StockCategory();
    sc.setCategoryId(categoryId);
    sc.setBusinessGroupId(String.valueOf(bgId));
    sc.setDescription("Test Item");

    Mockito.when(stockCategoryRepository.findByCategoryIdAndBusinessGroupId(categoryId, String.valueOf(bgId))).thenReturn(Optional.of(sc));

    StockCategoryDto result = stockCategoryService.getStockCategoryByIdAndBusinessGroupId(categoryId, bgId);

    assertEquals(5, result.getCategoryId() );
    assertEquals(10, result.getBusinessGroupId());
    assertNotNull(result.getDescription());
    Mockito.verify(stockCategoryRepository, Mockito.times(1)).findByCategoryIdAndBusinessGroupId(categoryId, String.valueOf(bgId));
}

}

服务等级

@Service
public class StockCategoryServiceImpl implements StockCategoryService {

private ModelMapper mapper;
private StockCategoryRepository stockCategoryRepository;

public StockCategoryServiceImpl(ModelMapper mapper, StockCategoryRepository stockCategoryRepository) {
    this.mapper = mapper;
    this.stockCategoryRepository = stockCategoryRepository;
}

@Override
public StockCategoryDto getStockCategoryByIdAndBusinessGroupId(int categoryId, int bgId) {

    Optional<StockCategory> cat = stockCategoryRepository.findByCategoryIdAndBusinessGroupId(categoryId, String.valueOf(bgId));
    if(cat.isPresent()) {

        //getting NullPointerException at this line        
    mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);

        return mapper.map(cat.get(), StockCategoryDto.class);
    }

    StockCategoryDto dto = new StockCategoryDto();
    dto.setMessage("Category not found for given id: "+categoryId);
    return dto;
}

}

标签: javaspring-bootjunitmockitomodelmapper

解决方案


你不应该重新分配你的模拟ModelMapper方法setup。删除此方法

而且我在您的代码模拟定义中找不到mapper.getConfiguration()

when(mapper.getConfiguration()).thenReturn(...)

你应该告诉它模仿。


推荐阅读