Search code examples
javatestingjunitintegration-testing

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.


Solution

  • 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
        }
    }