黄瓜StepDef ResultActions NullPointerException

问题描述 投票:0回答:1

[尝试执行将JSON从.feature文件传递到localhost上的REST端点的Cucumber StepDefs文件时,我收到以下NullPointerException。

  • 我曾尝试过其他方式实例化ResultActions,并收到相同的错误。

  • 与测试链接的Controller工作正常,并且指向正确的REST端点。

  • 问题出在result中的personStepDefs

  • [我不认为我在构建ResultActions result时缺少RequestBuilder的参数
java.lang.NullPointerException at com.///.v2.PersonStepDefs.i\_add\_a\_new\_Person\_using\_POST\_at\_with\_JSON([PersonStepDefs.java:49](https://PersonStepDefs.java:49)) at

✽.I add a new Person using POST at "[http://localhost:8080/services/person/add](http://localhost:8080/services/person/add)" with JSON:(file:///C:/path/to/src/test/resources/Person.feature:6)

PersonStepDefs.java

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

@RunWith(SpringJUnit4ClassRunner.class) 
@WebAppConfiguration
@Transactional
/**
 * Person Step Definition class to execute Scenario contained in Person.feature
 * @author Lewis Jones
 *
 */
public class PersonStepDefs {

    @Autowired
    private volatile WebApplicationContext wac;

    @Autowired
    private volatile MockMvc mockMvc;

    private ResultActions result;

    /**
     * Runs the application server before every scenario.
     */
    @BeforeClass
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    @When("I add a new Person using POST at {string} with JSON:")
    public void i_add_a_new_Person_using_POST_at_with_JSON(String request, String json) throws Exception {
        result = mockMvc.perform(post(request).contentType(MediaType.APPLICATION_JSON)
                .content(json.getBytes()));
    }

    @Then("the response code should be {int}")
    public void the_response_code_should_be(Integer responseCode) throws Exception {
        result.andExpect(status().is(responseCode));
    }

}

RunMvcTest.java

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
        plugin = {"pretty","html:build/cucumber-html-report"},
        features = "src/test/resources", strict = true)
@WebAppConfiguration
@ContextConfiguration(classes = V2Application.class)
/**
 * A class to run the Cucumber .feature files located in 'features'
 * @author Lewis Jones
 *
 */
public class RunMvcTest {

}

Person.feature

Feature: Person CRUD
  As a User, I want to add a Person

  @repo
  Scenario: Person.Repo.Add
    When I add a new Person using POST at "http://localhost:8080/services/person/add" with JSON:
      """
      {"firstName":"Lewis","lastName":"Jones","addressId":"1", "dob":"1999-07-11"}
      """
    Then the response code should be 200

[当尝试执行一个Cucumber StepDefs文件时,我收到以下NullPointerException,该文件将JSON从.feature文件传递到localhost上的REST端点;我试图实例化...

java spring-boot cucumber mockmvc
1个回答
0
投票

找到解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.