I have a SpringBoot application an I have a Unit test for a Rest Controller. This unit test uses the @SpringBootTest annotation.
My Controller:
@CrossOrigin(origins = "*")
@RestController
@Validated
public class EngineeringProfileController {
@Autowired
private EngineeringProfileService engineeringProfileService;
@Autowired
private AuthenticationTokenParser authenticationTokenParser;
...
}
My Unit test:
@ExtendWith(SpringExtension.class)
@ActiveProfiles("test")
@AutoConfigureMockMvc(addFilters = false)
@SpringBootTest // It is working!
//@WebMvcTest(value = EngineeringProfileController.class) // It does not work!
public class EngineeringProfileControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private EngineeringProfileService engineeringProfileServiceMock;
@MockBean
private AuthenticationTokenParser authenticationTokenParserMock;
...
}
When I run the test, it OK, but it takes a lot of time to load. Just recently I understood that @SpringBootTest Load all dependencies. So I tried use @WebMvcTest annotation (that is commented in code above). However, I replace @SpringBootTest by @WebMvcTest, I got this error:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'engineeringProfileEmailSender': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userProxy'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'profilemanagement.external.proxies.UserProxy': Unexpected exception during bean creation; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.cloud.openfeign.FeignContext' available
Strangely, this engineeringProfileEmailSender is not in controller but in EngineeringProfileService. It is as if instead of creating a EngineeringProfileService mock for EngineeringProfileController it is actualling using the real class. I tried to googled how to solve this issue but nothing that seemed to fit worked.
What is missing here?