Как ввести Mapper из Mapstruct в интеграционный тест @webmockmvcJAVA

Программисты JAVA общаются здесь
Anonymous
 Как ввести Mapper из Mapstruct в интеграционный тест @webmockmvc

Сообщение Anonymous »

Я пытаюсь протестировать контроллер Spring Boot, контроллер имеет зависимость пользователя , а пользовательская зависимость имеет зависимость usermapper , код компилируется и запускается безупречно, но при попытке пытаться Запустите тест ниже, я получаю ошибку: < /p>

Код: Выделить всё

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userControllerImpl':
Unsatisfied dependency expressed through field 'userMapper': Error creating bean with name 'com.acneUserMapper':
Failed to instantiate [com.acne.user.UserMapper]: Specified class is an interface

< /code>
test: < /p>
@WebMvcTest(UserController.class)
@Import(UserMapper.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@AutoConfigureWebMvc
public class UserControllerTests {
@Autowired
private MockMvc mockMvc;

@Test
public void shouldGetAuthenticatedUser() throws Exception {
this.mockMvc.perform(get("/api/users/me").with(oidcLogin())).andExpect(status().isOk());
}
}
< /code>
usermapper: < /p>
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public interface UserMapper {
public UserResponseDTO toUserResponseDTO(UserEntity userEntity);

public UserDTO toDto(UserEntity entity);

public UserEntity toEntity(UserDTO dto);
}
< /code>
usercontroller & Impl: < /p>
public interface UserController {

@GetMapping("/me")
public ResponseEntity getCurrentUser(Authentication authentication);
}
< /code>
@RestController
public class UserControllerImpl implements UserController {

@Autowired
UserMapper userMapper;

@Autowired
UserService userService;

@Override
public ResponseEntity getCurrentUser(Authentication currentUser) {
UserEntity loggedUser = userService.findByLoginOrError(currentUser.getName());
return ResponseEntity.ok(userMapper.toUserResponseDTO(loggedUser));
}
}
Если я изменю тест импорт на @import (usermapperimpl.class) ошибка изменяется на:

Код: Выделить всё

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.acne.user.UserService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency a
nnotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Мой вопрос: что я могу сделать, чтобы проверить контроллер и пружину правильно ввести карту?


Подробнее здесь: https://stackoverflow.com/questions/794 ... ation-test

Вернуться в «JAVA»