Professional Documents
Culture Documents
Junit 5
Junit 5
JUnit Platform
Launches testing frameworks on the JVM
Has TestEngine API used to build a testing framework that runs on the JUnit
platform
-- Test Runners
-- Provides context to run tests
-- Test Engine
Unit Jupiter
Blend of new programming model for writing tests and extension model for extensions
Addition of new annotations like @BeforeEach, @AfterEach, @AfterAll, @BeforeAll
etc.
JUnit Vintage
Provides support to execute previous JUnit version 3 and 4 tests on this new
platform
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@DisplayName
@Disabled
@Timeout(value = 500, unit = TimeUnit.MILLISECONDS)
@RepeatedTest
@Nested
assertAll()
assertThrow(expectedType, Executable)
Conditional Execution:
@EnableOnOS(OS.LINUX) @DisabledOnOS
@EnableOnJre()JRE.JAVA_11) @DisabledOnJre
@EnableIf
@EnabledIfSystemProperty
@EnabledIfEnvironmentVariable
Assumptions:
assertTrue()
assertFalse()
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<!-- include tags -->
<groups>integration, feature-168</groups>
<!-- exclude tags -->
<excludedGroups>slow</excludedGroups>
</configuration>
</plugin>
mvn -DexcludedGroups="slow"
MethodOrderer
Alphanumeric
OrderAnnotation
Random
Custom Order
Alphanumeric
1.1 It sorts test methods alphanumerically.
@TestMethodOrder(MethodOrderer.Alphanumeric.class)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
It sorts test methods based on the @Order values.
@Order(2)
@TestMethodOrder(MethodOrderer.Random.class)
TestSuite in JUnit 5
@SelectPackages("....")
@SelectPackages({"..." , "..." })
@SelectClasses(...)
@SelectClasses({..., ..., ...})
@RunWith(JUnitPlatform.class)
@SelectPackages("....")
@IncludePackages()
@ExcludePackages()
@IncludeTags()
@ExcludeTags()
In JUnit, you can organize the test cases into different categories, and run those
categorized test cases with @Categories.ExcludeCategory or
@Categories.IncludeCategory
@Category(PerformanceTests.class)
@Category({PerformanceTests.class, RegressionTests.class})
public class ClassB {
@Test
public void test_b_1() {
assertThat(1 == 1, is(true));
}
}
@RunWith(Categories.class)
@Categories.IncludeCategory(PerformanceTests.class)
//Include multiple categories
//@Categories.IncludeCategory({PerformanceTests.class, RegressionTests.class})
@Suite.SuiteClasses({ClassA.class, ClassB.class, ClassC.class})
public class PerformanceTestSuite {
}
@RunWith(Categories.class)
@Categories.ExcludeCategory(PerformanceTests.class)
@Suite.SuiteClasses({ClassA.class, ClassB.class, ClassC.class})
public class ExcludePerformanceTestSuite {
}
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version> 1.1.1</version>
<scope>test</scope>
</dependency>
// This test will run sequentially 5 times with 1 argument each time
@ParameterizedTest
@ValueSource(ints = {8,4,2,6,10})
void test_int_arrays(int arg) {
System.out.println("arg => "+arg);
assertTrue(arg % 2 == 0);
}
}
Placeholder Description
{displayName} placeholder for the display name of the method.
{index} placeholder for the current invocation index (starts from 1).
{arguments} placeholder for the complete, comma-separated arguments list.
{0}, {1}, �? placeholder for an individual argument.
public class Junit5_CustomDisplayNames_Test {
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
</reporting>
Run mvn site command from the terminal. It will generate HTML reports in
target/site directory.
site surefire-report:report