Springブートプロジェクトで次のクラスのエンドツーエンドテストを作成していますが、No qualifying bean of type 'com.boot.cut_costs.service.CustomUserDetailsService' available
というエラーが表示されるのはorg.springframework.beans.factory.NoSuchBeanDefinitionException
です。"org.springframework.beans.factory.NoSuchBeanDefinitionException"のため、SpringブートjUnitテストが失敗する
@RestController
public class AuthenticationController {
@Autowired
protected AuthenticationManager authenticationManager;
@Autowired
private CustomUserDetailsService userDetailsServices;
@Autowired
private UserDetailsDtoValidator createUserDetailsDtoValidator;
@RequestMapping(value = "/signup", method = RequestMethod.POST)
public void create(@RequestBody UserDetailsDto userDetailsDTO, HttpServletResponse response, BindingResult result) {
// ...
userDetailsServices.saveIfNotExists(username, password, name);
// ...
if (authenticatedUser != null) {
AuthenticationService.addAuthentication(response, authenticatedUser.getName());
SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
} else {
throw new BadCredentialsException("Bad credentials provided");
}
}
}
Testクラス:
@RunWith(SpringRunner.class)
@WebMvcTest(AuthenticationController.class)
public class AuthenticationControllerFTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private AuthenticationManager authenticationManager;
@Test
public void testCreate() throws Exception {
Authentication authentication = Mockito.mock(Authentication.class);
Mockito.when(authentication.getName()).thenReturn("DUMMY_USERNAME");
Mockito.when(
authenticationManager.authenticate(Mockito
.any(UsernamePasswordAuthenticationToken.class)))
.thenReturn(authentication);
//....
RequestBuilder requestBuilder = MockMvcRequestBuilders
.post("/signup")
.accept(MediaType.APPLICATION_JSON).content(exampleUserInfo)
.contentType(MediaType.APPLICATION_JSON);
MvcResult result = mockMvc.perform(requestBuilder).andReturn();
MockHttpServletResponse response = result.getResponse();
}
}
私はそれをテスト環境で春のコンテキストはDEV /本番環境と同じようにロードされていないため、このエラーが発生したと思います。この問題をどのように修正する必要がありますか?
編集1
マイ春ブートアプリケーションエントリポイントがApp.java
です:
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
私は原因も文脈だと思います。試してみてください: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"location/to/test-config.xml")) –
「場所」とは何ですか?それは何を指していますか? –
を参照してください。http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/resources.html#resources-app-ctx –