Spring 如何按顺序执行junit测试用例
我正在编写用于创建和更新数据的集成测试用例Spring 如何按顺序执行junit测试用例,spring,junit,integration-testing,Spring,Junit,Integration Testing,我正在编写用于创建和更新数据的集成测试用例 @RunWith(SpringRunner.class) @SpringBootTest(classes = MyApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class MyIntegrationTest { private St
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApplication.class, webEnvironment =
SpringBootTest.WebEnvironment.DEFINED_PORT)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class MyIntegrationTest {
private String baseUrl="http://192.168.6.177/api/v1/";
@Autowired
TestRestTemplate restTemplate;
Long createdId; // trying to set ID which is coming after test1_AddData
@Test
public void test1_AddData() throws Exception {
ABC abc = new ABC("Name");
HttpEntity<ABC> requestBodyData = new HttpEntity<>(ABC);
ParameterizedTypeReference<RestTemplateResponseEnvelope<ABC>> typeReference =
new ParameterizedTypeReference<RestTemplateResponseEnvelope<ABC>>() {
};
ResponseEntity<RestTemplateResponseEnvelope<ABC>> response = restTemplate.exchange(
baseUrl + "/presenceType",
HttpMethod.POST, requestBodyData, typeReference);
Assert.assertTrue(HttpStatus.CREATED.equals(response.getStatusCode()));
createdId = response.getBody().getData().getId();
}
@Test
public void test2_updateData() throws Exception {
ABC abc = new ABC("NEW NAME");
System.out.println("------------------------------------------" + createdId); /// it is giving null
HttpEntity<ABC> requestBodyData = new HttpEntity<>(ABC);
ResponseEntity<ABC> response = restTemplate.exchange(
baseUrl + "/presenceType/" + createdId,
HttpMethod.PUT, requestBodyData, ABC.class);
Assert.assertTrue(HttpStatus.OK.equals(response.getStatusCode()));
createdId = response.getBody().getId();
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(类=MyApplication.class,webEnvironment=
SpringBootTest.WebEnvironment.DEFINED\u端口)
@FixMethodOrder(MethodSorters.NAME\u升序)
公共类MyIntegrationTest{
私有字符串baseUrl=”http://192.168.6.177/api/v1/";
@自动连线
TestRestTemplate-restTemplate;
Long createdId;//试图设置test1\u AddData之后的ID
@试验
public void test1_AddData()引发异常{
ABC=新ABC(“名称”);
HttpEntity requestBodyData=新的HttpEntity(ABC);
参数化类型引用类型引用=
新的ParameteredTypeReference(){
};
ResponseEntity response=restemplate.exchange(
baseUrl+“/presenceType”,
HttpMethod.POST、requestBodyData、typeReference);
Assert.assertTrue(HttpStatus.CREATED.equals(response.getStatusCode());
createdId=response.getBody().getData().getId();
}
@试验
public void test2_updateData()引发异常{
ABC=新ABC(“新名称”);
System.out.println(“-----------------------------------------------”+createdId);///它给出的是null
HttpEntity requestBodyData=新的HttpEntity(ABC);
ResponseEntity response=restemplate.exchange(
baseUrl+“/presenceType/”+createdId,
HttpMethod.PUT、requestBodyData、ABC.class);
Assert.assertTrue(HttpStatus.OK.equals(response.getStatusCode());
createdId=response.getBody().getId();
}
}
我的执行结果
------------------------------------------空的
执行此操作需要做什么,即在执行第一个函数后调用第二个函数。
注意:代码还包含delete方法,该方法需要在第三个之后调用。尽管在测试中固定顺序不是好的做法。但是,如果您使用的是4.11版以上的JUnit,那么它有注释@FixMethodOrder 您可以按方法名称设置顺序 例如:
import org.junit.runners.MethodSorters;
import org.junit.FixMethodOrder;
import org.junit.Test;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class OrderTest {
@Test
public void test1() {
System.out.println("test1");
}
@Test
public void test2() {
System.out.println("test2");
}
}
供进一步阅读@
Junit Git页面:
自定义实现:Change
Long createdId代码>到静态长createdId代码>。JUnit将在测试之间丢弃所有其他内容。