2017-05-05 4 views
1

@RunWith & @SpringBootTestを使用してコントローラをテストしようとしています。スプリングブート1.5.2コントローラレイヤーテスト

コントローラ

@RestController 
public class HomeController { 

    @RequestMapping(value = "/home", method = RequestMethod.GET) 
    public String get(HttpServletResponse response) throws IOException { 
     return "Hello World"; 
    } 
} 

テストクラス

@RunWith(SpringRunner.class) 
@SpringBootTest(classes = Application.class) 
@WebAppConfiguration 
public class SampleTestNGApplicationTests{ 

    @Autowired 
    private TestRestTemplate restTemplate; 

    @Test 
    public void testHome() throws Exception { 

     ResponseEntity<String> entity = this.restTemplate.getForEntity("/home", String.class); 

     assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); 
     assertThat(entity.getBody()).isEqualTo("Hello World"); 
    } 

} 

@RunWith & @SpringBootTest見つからない注釈を

<dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-test</artifactId> 
     <scope>test</scope> 
</dependency> 

をテストするための依存関係は、私はそれのために任意のライブラリ足りませんか?私は春のブートで非常に変化があることを知っている1.5.2は、春のブート1.4.2に比べています。

質問の上

更新が解決されました、実際に私はテストのための新しいモジュールを作成し、コントローラがdifferntモジュールです。私はテストモジュールのメイン→src-> javaの下にテストコードを書いています。spring-boot-starter-test依存関係のスコープはtestになりますので、<scope>test</scope>を削除しました。@RunWith & @SpringBootTestアノテーションを取得できます。

今、私はエラー@ConditionalOnWebApplication (required) found 'session' scope (OnWebApplicationCondition)

エラーログ

========================= 
AUTO-CONFIGURATION REPORT 
========================= 


Positive matches: 
----------------- 

    DispatcherServletAutoConfiguration matched: 
     - @ConditionalOnClass found required class 'org.springframework.web.servlet.DispatcherServlet'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition) 
     - @ConditionalOnWebApplication (required) found 'session' scope (OnWebApplicationCondition) 

    DispatcherServletAutoConfiguration.DispatcherServletConfiguration matched: 
     - @ConditionalOnClass found required class 'javax.servlet.ServletRegistration'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition) 
     - Default DispatcherServlet did not find dispatcher servlet beans (DispatcherServletAutoConfiguration.DefaultDispatcherServletCondition) 

+0

jarが壊れています。誤ったインポートです。伝えるのは難しい。 –

答えて

1

何かを行うことができ、私は@SpringBootTestwebEnvironmentを追加し、それは私のために働いています。

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 
0
@SpringBootTest(classes = Application.class) 

は全体のアプリケーションコンテキストをロードします取得しています。私はあなたのコントローラ層だけをテストしたい場合は必要ないと思います。あなたはそれに費やした時間をたくさんした後

このような
@RunWith(SpringRunner.class) 
@WebMvcTest(controllers = HomeController.class) 
public class HomeControllerTest { 

    @Autowired 
    private MockMvc mvc; 

    @MockBean 
    private SomeService someservice // Mock any other dependencies which you have in your controller. 

    @Test 
    public void someTest() throws Exception { 

    doAnswer(invocation -> { 
     // return some predefined data which would be returned from your service layer 
    }).when(someservice).someMethod(); 

    mvc.perform(MockMvcRequestBuilders.get("/home").contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)) 
     .andExpect(status().isOk()) 
     .andExpect(content().string("expectedString")); 
    } 

} 
+0

SpringBootTestを削除してWebMvcTestを追加しましたが、例外 "ApplicationContextを読み込めませんでした" –

+0

@WebAppConfigurationを削除し、RestTemplateも使用しません(これはアプリケーションコンテキスト全体を設定する必要があります) – pvpkiran

関連する問題