空指针异常,当将 rest-assured 中的 @Steps 注入到 spring-boot 测试中时。

huangapple 未分类评论47阅读模式
英文:

Null pointer Exception when injecting a rest-assured @Steps into spring-boot Test

问题

Am new to springboot and am trying to integrate Integration Tests using Rest-assured to test my Rest-Api.

Am getting NPE when injecting @Steps into SpringBoot Test. I'm introducing a step class to improve on re-usability code. This test runs well if the step method is in the IT-class. I tried @Component annotation but it didn't work.

Step class

import net.thucydides.core.annotations.Step;
import org.apache.http.HttpStatus;

import static com.jayway.restassured.RestAssured.when;

public class StaffSteps {
    protected static String BASE_STAFF_URL = "/api/v1a/staff/";
    protected static Staff staff;

    @Step
    public StaffSteps getStaffMemberById(String id) {
        staff = when().get(BASE_STAFF_URL + id)
                .then().assertThat()
                .statusCode(HttpStatus.SC_OK)
                .extract()
                .as(Staff.class);
        return this;
    }

    @Step
    public Staff getStaff() {
        return staff;
    }
}


import net.thucydides.core.annotations.Steps;
import org.apache.http.HttpStatus;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Arrays;
import java.util.List;

import static com.jayway.restassured.RestAssured.when;

@RunWith(SpringJUnit4ClassRunner.class)
public class StaffControllerIT extends BaseTest {

    @Steps
    private StaffSteps staffSteps;

    @Before
    public void setUp() {
    }

    @Test
    public void getStaffMemberById() {
        String id = "ff8081817049a34e017049a379320000";
        Staff staff = staffSteps.getStaffMemberById(id).getStaff();

        System.err.println(staff);
    }

When i run this test, staffSteps is null.  
Here is my dependency i used 

	<dependency>
			<groupId>net.serenity-bdd</groupId>
			<artifactId>serenity-core</artifactId>
			<version>1.9.4</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>io.rest-assured</groupId>
			<artifactId>rest-assured</artifactId>
			<version>3.0.0</version>
			<scope>test</scope>
		</dependency>
Please let me know if you need more information on this. Thanks
英文:

Am new to springboot and am trying to integrate Integration Tests using Rest-assured to test my Rest-Api.

Am getting NPE when injecting @Steps into SpringBoot Test.I'm introducing a step class to improve on re-usability code.This test runs well if the step method is in the IT-class.I tried @Component annotation but it didn't work

Step class

import net.thucydides.core.annotations.Step;
import org.apache.http.HttpStatus;

import static com.jayway.restassured.RestAssured.when;

public class StaffSteps {
    protected  static String BASE_STAFF_URL = &quot;/api/v1a/staff/&quot;;
    protected  static Staff staff;

    @Step
    public StaffSteps getStaffMemberById(String id){
        staff = when().get(BASE_STAFF_URL+id)
                .then().assertThat()
                .statusCode(HttpStatus.SC_OK)
                .extract()
                .as(Staff.class);
    return this;
    }
    @Step
    public Staff getStaff(){return staff;}
}

import net.thucydides.core.annotations.Steps;
import org.apache.http.HttpStatus;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Arrays;
import java.util.List;

import static com.jayway.restassured.RestAssured.when;

@RunWith(SpringJUnit4ClassRunner.class)
public class StaffControllerIT extends BaseTest {

    @Steps
    private StaffSteps staffSteps;

    @Before
    public void setUp(){
    }

    @Test
    public void getStaffMemberById(){
        String id =&quot;ff8081817049a34e017049a379320000&quot;;
        Staff staff = staffSteps.getStaffMemberById(id).getStaff();

        System.err.println(staff);
    }

When i run this test, staffSteps is null.
Here is my dependency i used

	&lt;dependency&gt;
			&lt;groupId&gt;net.serenity-bdd&lt;/groupId&gt;
			&lt;artifactId&gt;serenity-core&lt;/artifactId&gt;
			&lt;version&gt;1.9.4&lt;/version&gt;
			&lt;scope&gt;test&lt;/scope&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;io.rest-assured&lt;/groupId&gt;
			&lt;artifactId&gt;rest-assured&lt;/artifactId&gt;
			&lt;version&gt;3.0.0&lt;/version&gt;
			&lt;scope&gt;test&lt;/scope&gt;
		&lt;/dependency&gt;

Please let me know if you need more information on this. Thanks

答案1

得分: 0

以下是您要求的代码部分的中文翻译:

// 使用 SpringRunner 的解决方案:

// 使用 @Bean 和 @StepScope 对步骤进行注释,因此该对象将与 StepExecution 共享其生命周期。
public class StaffStepsConfig {
    protected static String BASE_STAFF_URL = "/api/v1a/staff/";
    protected static Staff staff;

    @Bean
    @StepScope
    public StaffSteps getStaffMemberById(String id){
        staff = when().get(BASE_STAFF_URL+id)
                .then().assertThat()
                .statusCode(HttpStatus.SC_OK)
                .extract()
                .as(Staff.class);
        return this;
    }

    @Bean
    @StepScope
    public Staff getStaff(){return staff;}
}

// 在测试类中,spring-batch-test 依赖项提供了一组有用的辅助方法和监听器,可用于在测试期间配置 Spring Batch 上下文。
@RunWith(SpringRunner.class)
@EnableAutoConfiguration
@ContextConfiguration(classes = { StaffStepsConfig.class })
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
public class StaffControllerIT extends BaseTest {
    @Autowired
    private StaffSteps staffSteps;

    @Before
    public void setUp(){
    }

    @Test
    public void getStaffMemberById(){
        String id = "ff8081817049a34e017049a379320000";
        Staff staff = staffSteps.getStaffMemberById(id).getStaff();

        System.err.println(staff);
    }
}

// 注意:此运行器识别 @SpringBootTest。我认为问题出在测试结果生成方式上。步骤未被 Serenity 读取。
// Spring 会注入 @Autowired 类,而 Serenity 会注入 @Steps 类。我认为这是因为 Serenity 和 Spring 在不同的上下文中创建组件。

// 在 pom.xml 中所需的依赖项:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-batch</artifactId>
    <version>2.1.9.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.1.9.RELEASE</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.batch</groupId>
    <artifactId>spring-batch-test</artifactId>
    <version>4.2.0.RELEASE</version>
    <scope>test</scope>
</dependency>

请注意,这是您提供的代码的中文翻译部分,没有其他内容。如果您需要进一步的帮助或有其他问题,请随时提出。

英文:

Solution using SpringRunner:

Annotate the steps with @Bean and @StepScope, and as a result, this object will share its lifetime with StepExecution.

public class StaffStepsConfig {
    protected  static String BASE_STAFF_URL = &quot;/api/v1a/staff/&quot;;
    protected  static Staff staff;

    @Bean
    @StepScope
    public StaffSteps getStaffMemberById(String id){
        staff = when().get(BASE_STAFF_URL+id)
                .then().assertThat()
                .statusCode(HttpStatus.SC_OK)
                .extract()
                .as(Staff.class);
    return this;
    }

    @Bean
    @StepScope
    public Staff getStaff(){return staff;}
}

In the Test class, the spring-batch-test dependency provides a set of useful helper methods and listeners that can be used to configure the Spring Batch context during testing.

@RunWith(SpringRunner.class)
//@SpringBatchTest
//@SpringBootTest
@EnableAutoConfiguration
@ContextConfiguration(classes = { StaffStepsConfig.class })
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
public class StaffControllerIT extends BaseTest {
    @Autowired
    private StaffSteps staffSteps;

    @Before
    public void setUp(){
    }

    @Test
    public void getStaffMemberById(){
        String id =&quot;ff8081817049a34e017049a379320000&quot;;
        Staff staff = staffSteps.getStaffMemberById(id).getStaff();

        System.err.println(staff);
    }
}

Note: This runner recognizes @SpringBootTest . I think the problem is in the way the test outcomes are generated. The steps are not being read by Serenity. Spring will inject @Autowired classes, and @serenity will inject @steps classes. I assume this happens because serenity and spring are creating components in different contexts .

required dependencies in your pom.xml:

&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-batch&lt;/artifactId&gt;
    &lt;version&gt;2.1.9.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
    &lt;version&gt;2.1.9.RELEASE&lt;/version&gt;
    &lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.batch&lt;/groupId&gt;
    &lt;artifactId&gt;spring-batch-test&lt;/artifactId&gt;
    &lt;version&gt;4.2.0.RELEASE&lt;/version&gt;
    &lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt; 

空指针异常,当将 rest-assured 中的 @Steps 注入到 spring-boot 测试中时。

huangapple
  • 本文由 发表于 2020年4月6日 04:25:12
  • 转载请务必保留本文链接:https://java.coder-hub.com/61049029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定