ModelMapper在实时代码上工作,但在junits期间不工作

abithluo  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(504)

背景
我有一个简单的springboot应用程序,我正在其中测试从dto到域对象的更新。自然-我使用modelmapper从dto转换到->实体。我遇到的问题是,虽然modelmapper在实时运行中工作得很好,但在junits期间却不工作。我把一个断点放在 initBaseModelMapper 在我的配置文件中,在junit和live运行期间,断点成功命中。但是在junits中,在实际的Map过程中 null 值仍应用于域实体,但在实际运行期间没有应用,因为实际运行效果很好。
配置

@Configuration
public class ModelMapperConfiguration {
    @Bean(name = "myEntityMapper")
    public ModelMapper modelMapper() {
        return initBaseModelMapper();
    }

    public static ModelMapper initBaseModelMapper() {
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.getConfiguration().setPropertyCondition(Conditions.isNotNull());
        modelMapper.getConfiguration().setSkipNullEnabled(true); // Tried without this as well
        return modelMapper; // Gets hit during LIVE and JUNITS
    }
}

测试中的主类方法

public class MyCaseService {
    @Autowired
    @Qualifier("myEntityMapper")
    private ModelMapper modelMapper;

    @Override
    @Transactional
    public @ResponseBody
    MyCaseEntity updateMyCase(
            @Valid final String myCaseId,
            @Valid MyCaseDTO myCase) throws Exception {

        MyCaseEntity existingEntity = entityRepository.find(myCaseId);
        modelMapper.map(myCase, existingEntity);
        return existingEntity;
    }

朱尼特
我把一个断点 ModelConfiguration 我可以看到它被初始化,就像代码运行时一样。但是,由于某些原因,modelmapper忽略了空字段的跳过,这与它在运行时不同

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes= {ModelMapperConfiguration.class})
public class MyCaseServiceTest {
    @InjectMocks
    private MyCaseService testSubject;

    @Spy
    @Qualifier("myEntityMapper")
    private ModelMapper modelMapper;

    @Before
    public void setUp() {
        // Initialized `testEntityCase` etc with Id etc
    }

    @Test
    public void testUpdate() throws Exception {
        Mockito.when(entityRepository.find(Mockito.any())).thenReturn(testEntityCase);

        MyCaseEntity myCase = testSubject.updateMyCase(
                "1", 
                testCaseDTO);

        assertEquals(1L, myCase.getId().longValue()); // <- Test Fails with NullPointer. Id becomes null during  JUNIT.
    }
oyjwcjzk

oyjwcjzk1#

克服这些问题的一种方法是自动连接mycaseservice的构造而不是私有成员

public class MyCaseService {

    private ModelMapper modelMapper;

    @Autowired 
    MyCaserService(@Qualifier("myEntityMapper") ModelMapper modelMapper) {
        this.modelMapper = modelMapper;
    }

    @Override
    @Transactional
    public @ResponseBody
    MyCaseEntity updateMyCase(
            @Valid final String myCaseId,
            @Valid MyCaseDTO myCase) throws Exception {

        MyCaseEntity existingEntity = entityRepository.find(myCaseId);
        modelMapper.map(myCase, existingEntity);
        return existingEntity;
    }
}

在测试中,您可以使用spy创建服务

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes= {ModelMapperConfiguration.class})
public class MyCaseServiceTest {

    @Spy
    @Qualifier("myEntityMapper")
    private ModelMapper modelMapper;

    private MyCaseService testSubject;

    @Before
    public void setUp() {
        testSubject = new MyCaseService(modelMapper);
        // Initialized `testEntityCase` etc with Id etc
    }
...

相关问题