Sunday 29 August 2021

Junit5: Test suite: SelectPackages: Select packages to run test cases

@SelectPackages annotation specifies the names of packages to select when running a test suite on the JUnit Platform. @SelectPackages run the test classes in the packages and sub packages.

 

Example

@RunWith(JUnitPlatform.class)
@SuiteDisplayName("JUnit Platform Suite Demo")
@SelectPackages({"com.sample.app.dummy", "com.sample.app.arithmetic"})
public class SuiteDemo1 {

}

‘SuiteDemo1’ class run the tests methods located in the packages "com.sample.app.dummy", and "com.sample.app.arithmetic".

 

Find the below working application.

 

ArithmeticTest.java

package com.sample.app.arithmetic;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class ArithmeticTest {

    private static int sum(int a, int b) {
        return a + b;
    }

    @Test
    public void test1() {
        assertEquals(5, sum(2, 3));
    }

}

 

DummyTest.java

package com.sample.app.dummy;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

public class DummyTest {

    @Test
    public void test1() {
        assertTrue(true);
    }
}

SuiteDemo1.java

package com.sample.app.suite;

import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.SuiteDisplayName;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
@SuiteDisplayName("JUnit Platform Suite Demo")
@SelectPackages({"com.sample.app.dummy", "com.sample.app.arithmetic"})
public class SuiteDemo1 {

}


Run SuiteDemo1.java, you will see the test methods executed in junit window.




Note

By default, it will only include test classes whose names either begin with Test or end with Test or Tests.


 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment