How to execute JUnit and TestNG tests in same project using maven-surefire-plugin?

前端 未结 12 1981
花落未央
花落未央 2020-12-13 06:04

Right now I have both type of tests but when I say \"mvn test\" it only executes TestNG tests and not Junit. I want to execute both one after another. Any Idea ?

12条回答
  •  北海茫月
    2020-12-13 06:42

    I found out a solution to run both test types with TestNG without changing your build tool configuration.

    I tested with Gradle but should work with Maven too.

    Note that this will run JUnit tests inside TestNG, but not the other way back.

    The trick is to use both frameworks' annotations in the test classes and use TestNG asserts for JUnit compatibility.

    import static org.testng.AssertJUnit.*;
    
    import org.junit.AfterClass;
    import org.junit.BeforeClass;
    import org.junit.Test;
    
    @org.testng.annotations.Test
    public final class ApplicationTest {
    
        @org.testng.annotations.BeforeClass
        @BeforeClass
        public static void setup () {}
    
        @org.testng.annotations.AfterClass
        @AfterClass
        public static void cleanup () {}
    
        @Test public void json () throws IOException {
            assertTrue (true);
        }
    }
    

    Using this hack, you can easily run existing JUnit tests with TestNG, helping you migrate them when time allows.

    Hope it helps!

提交回复
热议问题