2

I have Controller:

@RestController
@RequestMapping("/api/users")
public class UserController {

  private final UserService userService;

  @Autowired
  public UserController(UserService userService) {
    this.userService= userService;
  }

  @PostMapping(value = "/login")
  public UserDto login(@RequestBody UserDto user) {
    return userService.getUserWithAuth(user);
  }
}

I have ControllerAdvice:

@Slf4j
@ControllerAdvice
public class ExceptionHandlerAdvice extends ResponseEntityExceptionHandler {

  private final ErrorMessagesService errorMessagesService;

  @Autowired
  public ExceptionHandlerAdvice(ErrorMessagesService errorMessagesService) {
    this.errorMessagesService = errorMessagesService;
  }


  @ExceptionHandler(SomeException.class)
  protected ResponseEntity<RestResponse<RestResponse.RestError>> handleSomeException(SomeException e) {
    ErrorMessage messages = errorMessagesService.findByCode(e.getCode());
    RestResponse.RestError restError = new RestResponse.RestError(e.getCode(), formatErrorMessage(messages));
    return ResponseEntity.ok().body(new RestResponse<>(restError, null));
  }
}

And I write UnitTest for My controller:

@RunWith(SpringRunner.class)
@WebMvcTest(UserController.class)
public class UserControllerTest {

  @Autowired
  private MockMvc mockMvc;

  @MockBean(name = "mockUserService")
  private UserService userService;

  private UserDto userDtoResponse;
  private UserDto userDtoRequest;

  @Before
  public void init() {

    cashierDtoRequest = new UserDto();
    userDtoRequest.setLogin("Test");
    userDtoRequest.setPassword("Test");

    userDtoResponse= new UserDto();
    userDtoResponse.setLogin("Test");
    userDtoResponse.setPassword("Test");
    userDtoResponse.setAuthToken("123");

    when(userService.getUserWithAuth(userDtoRequest)).thenReturn(userDtoResponse);
  }

  @Test
  public void shouldReturnDefaultMessage() throws Exception {
    mockMvc.perform(post("/api/user/login")).andDo(print()).andExpect(status().isOk())
      .andExpect(jsonPath("$.login").value(userDtoResponse.getLogin()))
      .andExpect(jsonPath("$.authToken").value(userDtoResponse.getAuthToken())));
  }

But when I start test I get exception:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'mypackeg.ErrorMessagesService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
ip696
  • 6,574
  • 12
  • 65
  • 128
  • 1
    Have you tried `@MockBean ErrorMessagesService errorMessagesService` in your test class? – Sam Brannen Jul 04 '18 at 13:17
  • @ Sam Brannen yes I tried and now I get exception: Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel must be present! if I remove @EnableJpaRepositories test work – ip696 Jul 04 '18 at 13:23

1 Answers1

0

Try to exclude creating of bean in @WebMvcTest by using option of annotation

@WebMvcTest(controllers = UserController.class,
 excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = ExceptionHandlerAdvice.class ))
borino
  • 1,720
  • 1
  • 16
  • 24