简体   繁体   English

如何测试在 Springboot 中使用 Mapstruct 映射器的服务 class 方法?

[英]How to test Service class methods which uses Mapstruct mappers in Springboot?

@Mapper(uses = SomeMapper.class,imports = Date.class)
public interface DomainModelMapper {    
    Model domainToModel(Domain domain);
    
    @Mapping(target="dateUpdated", source="dateUpdated" ,defaultExpression = "java(Date.from(java.time.OffsetDateTime.now().toInstant()))")
    @Mapping(target="id.key",source="id.key",defaultExpression = "java(com.datastax.driver.core.utils.UUIDs.timeBased())")
    Domain modelToDomain(Model model);
}

I have a mapper class to do some Date conversions我有一个映射器 class 来做一些日期转换

public class SomeMapper {

    public Date OffsetDateTimeToDate(OffsetDateTime offsetDateTime) {
        return offsetDateTime != null ? Date.from(offsetDateTime.toInstant()):null;
    }

    public OffsetDateTime DateToOffsetDateTime(Date date) {
        return date != null ? date.toInstant().atOffset(ZoneOffset.UTC) : null;
    }
}

This is my service class where I use DomainModelMapper这是我使用 DomainModelMapper 的服务 class

@Service
public class SomeServiceImpl implements SomeService {
    
    @Autowired
    someRepository someRepository;
    
    
    private final DomainModelMapper domainToModelMapper = 
            Mappers.getMapper(DomainModelMapper.class);

    @Override
    public Model saveSomething(Model model) {
        return DomainModelMapper.domainToModel(someRepository
                .save(DomainModelMapper.modelToDomain(model)));
    }

How can I unit test saveSomething(Model model) method?如何对 saveSomething(Model model) 方法进行单元测试? How I can inject Mapstruct classes or mock them?我如何注入 Mapstruct 类或模拟它们?

If you make the @Mapper interface as a Spring-based component model, then it can be autowired through @Autowired annotation.如果您将@Mapper接口作为基于Spring 的组件model,那么它可以通过@Autowired注解自动装配。 Read more at 4.2.4.2 阅读更多内容。 Using dependency injection 使用依赖注入

@Mapper(uses = SomeMapper.class,imports = Date.class, componentModel = "spring")
public interface DomainModelMapper { 
    // IMPLEMENTATION
}
@Service
public class SomeServiceImpl implements SomeService {
    
    @Autowired
    SomeRepository someRepository;
    
    @Autowired
    DomainModelMapper domainModelMapper;

    // THE REST OF THE IMPLEMENTATION
}

The testing becomes fairly easy since all the beans can be also injected in the @SpringBootTest with the @Autowired annotation.测试变得相当容易,因为所有 bean 也可以使用@Autowired注释注入到@SpringBootTest中。

  • The DomainModelMapper can be autowired and used in the unit test as is and rely on its implementation DomainModelMapper可以自动装配并按原样在单元测试中使用,并依赖于它的实现
  • The SomeRepository shall be either mocked using @MockBean which overwrites an existing bean or creates a new one if none of that type exists... or autowired as in the implementation if you use an in-memory database for the testing phase, such as H2. SomeRepository应该使用@MockBean来模拟,它会覆盖现有的 bean,或者如果不存在该类型,则创建一个新的 bean......或者如果您在测试阶段使用内存数据库(例如 H2),则在实现中自动装配.

In any case, the test class will be ready for testing.无论如何,测试 class 将准备好进行测试。

@SpringBootTest
public class SomeServiceTest {
    
    @Autowired // or @MockBean
    SomeRepository someRepository;
    
    @Autowired // no need to mock it
    DomainModelMapper domainModelMapper;

    @Test
    public void test() {
        // TEST
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM