[英]Null-pointer exception when invoking get in rest-assured: Cannot get property 'assertionClosure' on null object
[英]Null pointer Exception when injecting a rest-assured @Steps into spring-boot Test
我是 springboot 的新手,正在嘗試使用 Rest-assured 集成集成測試來測試我的 Rest-Api。
在將@Steps 注入 SpringBoot 測試時,我得到了 NPE。我正在引入一個步驟 class 以改進可重用性代碼。如果步驟方法在 IT 類中,則此測試運行良好。我嘗試了 @Component 注釋,但它沒有不工作
步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);
}
當我運行這個測試時,staffSteps 是 null。
這是我使用的依賴項
<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>
如果您需要更多信息,請告訴我。 謝謝
使用 SpringRunner 的解決方案:
使用@Bean
和@StepScope
注釋步驟,因此,此 object 將與 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;}
}
在測試 class 中,spring-batch-test 依賴項提供了一組有用的輔助方法和偵聽器,可用於在測試期間配置 Spring 批處理上下文。
@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 ="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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.