2017-03-15 13 views
5

キュウリのテストでMockMvcを使用しようとしましたが、バネの依存関係は解決されません。@RunWith(Cucumber.class)と@Autowired MockMvc

@RunWith(Cucumber.class) 
@CucumberOptions(format = "pretty", features = "src/test/resources/features"}) 
@SpringBootTest 
public class CucumberTest { 

} 

はキュウリ機能

を実行し、手順については、このクラス:

@WebMvcTest(VersionController.class) 
@AutoConfigureWebMvc 
public class VersionControllerSteps { 

    @Autowired 
    private MockMvc mvc; 

    private MvcResult result; 

    @When("^the client calls /version$") 
    public void the_client_issues_GET_version() throws Throwable { 
     result = mvc.perform(get("/version")).andDo(print()).andReturn(); 
    } 

    @Then("^the client receives status code of (\\d+)$") 
    public void the_client_receives_status_code_of(int statusCode) throws Throwable { 
     assertThat(result.getResponse().getStatus()).isEqualTo(statusCode); 
    } 

    @And("^the client receives server version (.+)$") 
    public void the_client_receives_server_version_body(String version) throws Throwable { 
     assertThat(result.getResponse().getContentAsString()).isEqualTo(version); 
    } 
} 

が、このスロー例外:

java.lang.NullPointerException 
at com.example.rest.VersionControllerSteps.the_client_issues_GET_version(VersionControllerSteps.java:30) 
at ✽.When the client calls /version(version.feature:8) 

私はこのクラスを作成しました

ここには.feがありますature:

Feature: the version can be retrieved 

    As a api user 
    I want to know which api version is exposed 
    In order to be a good api user 

    Scenario: client makes call to GET /version 
    When the client calls /version 
    Then the client receives status code of 200 
    And the client receives server version 1.0 

キュウリとスプリングブートを使用するようにテストを設定するにはどうすればよいですか?

ありがとうございます。

+0

正確な命令がヌルポインタ例外をスローしていますか? – Antonio

答えて

0

コードリストとエラーログから、キュウリ+春の設定またはアプリケーションエラーの問題かどうかは不明です。

スタックトレースは、NULLポインタ例外をスローしている行30を指します。あなたのコードリストから、それは命令のためかもしれないと考えられます。

コントローラが実際にボディを返すかどうかを確認することは価値があります。例えば、戻り値に@ResponseBodyという注釈を付ける必要があるかもしれません。

@RequestMapping(
    value = "/campaigns/new", 
    method = RequestMethod.GET, 
    ) 
    public @ResponseBody String vers() { 
     return "1.0.1"; 
    } 
関連する問題