JUnit + Maven. How to do something before and after all tests?



  • It seems to me that I have learned JUnit completely. But I got such a problem when the connection needs to be received for a long time, not more than one, and in the end needs to be closed, and in the middle there are tests.
    In this case, the interface is serial. I decided to do this by synchronizing read / write operations, now all threads are waiting for their turn to communicate with the device.

    On the one hand, you can register opening and closing in blocks @ BeforeClass / AfterClass + add to the opening operation waiting for the port to be released, but this is crooked and the tests will be long, opening is an expensive operation.

    How can I do this?
    If I create your own TestRunner or Suite, then idea and maven will launch both, which will break everything.



  • In general, this solution will be useful to anyone who wants to test consistently and in a given order.

    It creates a suite, where in the annotation we list the classes in the order of execution:

    @RunWith (Suite.class)
    @ Suite.SuiteClasses ({OpenConnection.class,
                     GetServerIdTest.class,
                     ModbusStatusCodesTest.class,
                     ModbusSerialTransactionTest.class,
                     CloseConnection.class})
    public class OrderedTestSuite {}
    

    Add the plugin to pom.xml:

    <plugin>
             <groupId> org.apache.maven.plugins </groupId>
             <artifactId> maven-surefire-plugin </artifactId>
             <configuration>
                 <includes>
                     <include> ** / OrderedTestSuite.class </include>
                 </includes>
             </configuration>
    </plugin>
    


  • @baileigh Thanks. This is an interesting option.



Suggested Topics