简体   繁体   中英

Junit and Integration Tests best approach

I want to make some integration test to test my whole program (it's a standart command line Java application with program args)

Basically I have 3 tests : one to create a resource, one to update the resource and finally one to delete it.

I could do something like this :

@Test
public void create_resource() {
    MainApp.main(new String[] {"create", "my_resource_name"});
}

@Test
public void update_resource() {
    MainApp.main(new String[] {"update", "my_resource_name"});
}

@Test
public void delete_resource() {
    MainApp.main(new String[] {"delete", "my_resource_name"});
}

It works... as long as the methods are executed in the correct order. I've heard that the good execution of a test should not depend of the order.

It's true that ordering tests is considered a smell. Having said that, there might be cases where it might make sense, especially for integration tests.

Your sample code is a little vague since there are no assertions there. But it seems to me you could probably combine the three operation into a single test method. If you can't do that then you can just run them in order. JUnit 5 supports it using the @Order annotation:

@TestMethodOrder(OrderAnnotation.class)
class OrderedTestsDemo {

    @Test
    @Order(1)
    void nullValues() {
        // perform assertions against null values
    }

    @Test
    @Order(2)
    void emptyValues() {
        // perform assertions against empty values
    }

    @Test
    @Order(3)
    void validValues() {
        // perform assertions against valid values
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM