简体   繁体   English

如何使用 SpringJUnit4ClassRunner 将映射器(mapstruct)注入 Junit 测试

[英]How to inject mapper (mapstruct) to Junit test with SpringJUnit4ClassRunner

I have problems with configuring tests for SpringJUnit4ClassRunner.class).我在为 SpringJUnit4ClassRunner.class 配置测试时遇到问题。 My problem is because my mapper from map struct returns null when reached.我的问题是因为我的 map 结构映射器在到达时返回 null。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
class UserServiceTestSuite {
    @Spy
    private UserDto userDto;
    @Spy
    private UserMapper userMapper;
    @Mock
    private UserRepository userRepository;
    @InjectMocks
    private UserService userService;

    private User createUser() {
        return User.builder()
                .firstName("Steve")
                .lastName("Jobs")
                .login("SteveJobs")
                .password("password")
                .role(UserRole.ROLE_ADMIN)
                .build();

    }

    @Test
    public void testCreateUser() {
        //Given
        User user = createUser();
        Mockito.when(userRepository.save(user)).thenReturn(user);
        //When
        UserDto userDto = userService.createUser(userMapper.mapToUserDto(user));
        Long id = userDto.getId();
        //Then
        Assert.assertEquals("Steve", userDto.getFirstName());
        Assert.assertEquals("Jobs", userDto.getLastName());
        Assert.assertEquals("SteveJobs", userDto.getLogin());
        Assert.assertEquals("ROLE_ADMIN", userDto.getRole());
    }

In my opinion you have two options:在我看来,你有两种选择:

  1. Inject the mapper via @SpringBootTest(classes = {UserMapperImpl.class}) and通过@SpringBootTest(classes = {UserMapperImpl.class})注入映射器并
    @Autowired private UserMapper userMapper;
  2. Simply initialize the Mapper private UserMapper userMapper = new UserMapperImpl() (and remove @Spy )只需初始化映射器private UserMapper userMapper = new UserMapperImpl() (并删除@Spy

When using the second approach you can even remove the @SpringBootTest because in the given snippet you do not need a Spring context (created by the annotation).使用第二种方法时,您甚至可以删除@SpringBootTest ,因为在给定的代码段中,您不需要 Spring 上下文(由注释创建)。
@RunWith(MockitoJUnitRunner.class) can be used to automatically inject objects annotated with @Mock into your UserService . @RunWith(MockitoJUnitRunner.class)可用于自动将使用@Mock注释的对象注入到您的UserService中。 Writing unit tests without creating a spring context helps to keep test suite execution time low.在不创建 spring 上下文的情况下编写单元测试有助于保持较低的测试套件执行时间。

public interface UserMapper {
    UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);
    UserDTO userMapper.mapToUserDto(Object value);
}

Using this mapper interface you can instantiate from the mock and use the methods.使用此映射器接口,您可以从模拟实例化并使用这些方法。 For example:例如:

@Mock
private UserMapper userMapper;

Init the mocks:初始化模拟:

@Before
public void setUp() {
   MockitoAnnotations.initMocks(this);
}

and you use the call INSTANCE然后你使用调用INSTANCE

UserDto userDto = userService.createUser(userMapper.INSTANCE.mapToUserDto(user));

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

相关问题 JUnit Test无法使用SpringJUnit4ClassRunner回滚 - JUnit Test not rollbacking with SpringJUnit4ClassRunner 如何使用Parametrized运行JUnit SpringJUnit4ClassRunner? - How to run JUnit SpringJUnit4ClassRunner with Parametrized? 使用SpringJUnit4ClassRunner的Junit TestSuite - Junit TestSuite with SpringJUnit4ClassRunner 将junit侦听器添加到SpringJUnit4ClassRunner - Add a junit listener to a SpringJUnit4ClassRunner 如何将SpringJUnit4ClassRunner的'spring 3.1'面向junit4测试转换为基于spring的junit3.8测试? - How can I turn this 'spring 3.1' oriented junit4 test with SpringJUnit4ClassRunner into a spring oriented junit3.8 based test? SpringJUnit4ClassRunner是否为每个测试初始化​​bean? - SpringJUnit4ClassRunner initialize beans for each test? 如何增加SpringJUnit4ClassRunner使用的类路径? - How to augment the classpath used by SpringJUnit4ClassRunner? SpringJUnit4ClassRunner在JUnit测试用例结束时不关闭Application Context - SpringJUnit4ClassRunner does not close the Application Context at the end of JUnit test case 初始化错误在移动或删除类后使用SpringJUnit4ClassRunner运行junit测试 - initializationError running junit test with SpringJUnit4ClassRunner after moving or deleting a class java sbt test:如果使用SpringJUnit4ClassRunner运行测试,则忽略异常 - java sbt test: exception ignored if test is run with SpringJUnit4ClassRunner
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM