2017-07-19 7 views
0

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); 
    } 
} 
+0

私は原因も文脈だと思います。試してみてください: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"location/to/test-config.xml")) –

+0

「場所」とは何ですか?それは何を指していますか? –

+0

を参照してください。http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/resources.html#resources-app-ctx –

答えて

1

@WebMvcTestはコントローラの設定のみを読み込みます。だからこそあなたはこのDIエラーを持っています(それで、あなたのサービスのためにモックを提供しなければなりません)。したがって、サービスを注入する必要がある場合は@SpringBootTestを使用できます。

@SpringBootTestを使用する場合は、@AutoConfigureMockMvcを使用してMockMvcを設定する必要があります。

0

あなたは、あなたのテストに@ContextConfigurationアノテーションを使用して、Beanのコンポーネント・スキャンを行う構成でプルする必要がありますクラス。

これは、必要のない設定をロードする可能性があり、テストが失敗する可能性があるため、テストを実行するために必要なものだけを使用して設定クラスを書く方が安全ですあなたの豆の関連するコンポーネントをスキャンします)。この設定クラスをテストクラスの静的な内部クラスとして記述すると、@ContextConfigurationアノテーションをパラメータなしで使用するだけでこの設定を取得できます。

@ContextConfiguration 
public class MyTestClass { 

    @Test 
    public void myTest() { 
    ... 
    } 

    @Configuration 
    @ComponentScan("my.package") 
    public static class MyTestConfig { 
    } 
+0

これは春の起動アプリケーションなので、設定ファイルはありません。 –

+0

@ArianHosseinzadehああ、私は答えを更新しました。このようにして、テストコンテキストでコンポーネントをスキャンするように手動でスプリングブートアプリケーションを強制するには、これを実行できるはずです。 – Plog

+0

@ContextConfiguration(classes = {App.class})をテストクラスに追加しましたが、問題を解決しませんでした。私は自分の質問を編集し、 'App.java'について解説した –

関連する問題