Add this to your pom.xml:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
Add this to your build.gradle:
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.2'
import org.junit.jupiter.api.*;
public class MyTest {
@Test
void testMethod() {
// Test code here
}
@BeforeEach
void setUp() {
// Runs before each test
}
@AfterEach
void tearDown() {
// Runs after each test
}
@BeforeAll
static void setUpAll() {
// Runs once before all tests in the class
}
@AfterAll
static void tearDownAll() {
// Runs once after all tests in the class
}
@Disabled("Reason for disabling")
@Test
void disabledTest() {
// This test will not run
}
}
import static org.junit.jupiter.api.Assertions.*;
@Test
void testAssertions() {
assertEquals(expected, actual);
assertTrue(condition);
assertFalse(condition);
assertNull(object);
assertNotNull(object);
assertSame(expected, actual);
assertNotSame(expected, actual);
assertArrayEquals(expectedArray, actualArray);
assertThrows(ExpectedException.class, () -> {
// Code that should throw ExpectedException
});
assertAll("grouped assertions",
() -> assertEquals(expected1, actual1),
() -> assertEquals(expected2, actual2)
);
}
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class LifecycleTest {
@BeforeAll
void initAll() {
// Runs once before all test methods
}
@BeforeEach
void init() {
// Runs before each test method
}
@Test
void testMethod1() {
// Test code
}
@Test
void testMethod2() {
// Test code
}
@AfterEach
void tearDown() {
// Runs after each test method
}
@AfterAll
void tearDownAll() {
// Runs once after all test methods
}
}
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.*;
class ParameterizedTests {
@ParameterizedTest
@ValueSource(strings = { "racecar", "radar", "able was I ere I saw elba" })
void palindromes(String candidate) {
assertTrue(isPalindrome(candidate));
}
@ParameterizedTest
@CsvSource({
"apple, 1",
"banana, 2",
"'lemon, lime', 3"
})
void testWithCsvSource(String fruit, int rank) {
assertNotNull(fruit);
assertTrue(rank > 0);
}
@ParameterizedTest
@EnumSource(TimeUnit.class)
void testWithEnumSource(TimeUnit timeUnit) {
assertNotNull(timeUnit);
}
@ParameterizedTest
@MethodSource("stringProvider")
void testWithMethodSource(String argument) {
assertNotNull(argument);
}
static Stream<String> stringProvider() {
return Stream.of("apple", "banana", "cherry");
}
}
import static org.junit.jupiter.api.Assumptions.*;
@Test
void testOnlyOnDeveloperWorkstation() {
assumeTrue("DEV".equals(System.getenv("ENV")));
// remainder of test
}
@Test
void testOnlyOnCiServer() {
assumeTrue("CI".equals(System.getenv("ENV")),
() -> "Aborting test: not on CI server");
// remainder of test
}
@Test
@Timeout(value = 500, unit = TimeUnit.MILLISECONDS)
void timeoutTest() {
// Test code that should complete within 500 milliseconds
}
@Test
void exceptionTesting() {
Exception exception = assertThrows(ArithmeticException.class, () -> {
int result = 1 / 0;
});
assertEquals("/ by zero", exception.getMessage());
}
@Suite
@SelectClasses({
TestClass1.class,
TestClass2.class
})
class TestSuite {
// This class remains empty
}
@Nested
class NestedTests {
@Test
void test1() {
// Test code
}
@Test
void test2() {
// Test code
}
}
@TestFactory
Collection<DynamicTest> dynamicTests() {
return Arrays.asList(
dynamicTest("1st dynamic test", () -> assertTrue(true)),
dynamicTest("2nd dynamic test", () -> assertEquals(4, 2 * 2))
);
}
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
interface TestLifecycleLogger {
@BeforeAll
default void beforeAllTests() {
System.out.println("Before all tests");
}
@AfterAll
default void afterAllTests() {
System.out.println("After all tests");
}
@BeforeEach
default void beforeEachTest(TestInfo testInfo) {
System.out.println("About to execute [" + testInfo.getDisplayName() + "]");
}
@AfterEach
default void afterEachTest(TestInfo testInfo) {
System.out.println("Finished executing [" + testInfo.getDisplayName() + "]");
}
}
class TestInterfaceDemo implements TestLifecycleLogger {
@Test
void test1() {
// Test code
}
}
Use descriptive test method names
@Test
void shouldReturnTrueWhenInputIsValid() {
// Test code
}
Keep tests independent
Use @BeforeEach and @AfterEach for setup and teardown
Group related tests using @Nested classes
Use parameterized tests for testing multiple inputs
Aim for fast tests
Follow the AAA pattern: Arrange, Act, Assert
@Test
void testAddition() {
// Arrange
Calculator calc = new Calculator();
// Act
int result = calc.add(2, 3);
// Assert
assertEquals(5, result);
}
Use assumptions for environment-specific tests
Avoid logic in tests
Use assertAll for multiple related assertions
2024 © All rights reserved - buraxta.com