I am writing integration test case which is used to create and update a data
@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();
}
}
the output of my execution ------------------------------------------null
What needs to be done to make this execution i.e calling of second function after the exection of first. NOTE : The code also contains delete method which needs to be called after third.
FixMethodOrder not working for me, so I find another way to do this.
We can use @TestMethodOrder from Junit jupiter
https://junit.org/junit5/docs/5.5.0/api/org/junit/jupiter/api/TestMethodOrder.html
@Order annotation used to specify the sequence
Example code:
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class DummyTest1 {
@Test
@Order(2)
public void read(){
System.out.println("read");
}
@Test
@Order(4)
public void delete(){
System.out.println("delete");
}
@Test
@Order(1)
public void create(){
System.out.println("create");
}
@Test
@Order(3)
public void update(){
System.out.println("update");
}
}
It will execute in sequence order order1, order2, order3, order4
So testing order will be,
Although it is not good practice to fix order in Tests. But If you are using JUnit above version 4.11, it has annotation @FixMethodOrder.
You can set order by method names.Example:
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");
}
}
For further reading @FixMethodOder
Junit Git Page: https://github.com/junit-team/junit4/wiki/test-execution-order
Custom Implemenation: https://memorynotfound.com/run-junit-tests-method-order/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With