Copy/paste and edit these templates to suit your coding style.
A test method is an instance method that is directly or meta-annotated with @Test, @RepeatedTest, @ParameterizedTest, @TestFactory, or @TestTemplate.Standard Test Class and Methods
Copy and paste below template and edit this template as per your requirement:
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
class StandardTests {
@BeforeAll
static void initAll() {
}
@BeforeEach
void init() {
}
@Test
void succeedingTest() {
}
@Test
void failingTest() {
fail("a failing test");
}
@Test
@Disabled("for demonstration purposes")
void skippedTest() {
// not executed
}
@AfterEach
void tearDown() {
}
@AfterAll
static void tearDownAll() {
}
}
Examples of Test Class and Test Methods
import org.junit.jupiter.api.*;
public class JUnit5Tests {
@BeforeAll
static void setup() {
System.out.println("@BeforeAll executed");
}
@BeforeEach
void setupThis() {
System.out.println("@BeforeEach executed");
}
@Tag("DEV")
@Test
void testCalcOne() {
System.out.println("======TEST ONE EXECUTED=======");
Assertions.assertEquals(4, Calculator.add(2, 2));
}
@Tag("PROD")
@Disabled
@Test
void testCalcTwo() {
System.out.println("======TEST TWO EXECUTED=======");
Assertions.assertEquals(6, Calculator.add(2, 4));
}
@AfterEach
void tearThis() {
System.out.println("@AfterEach executed");
}
@AfterAll
static void tear() {
System.out.println("@AfterAll executed");
}
}Conclusion
JUnit 5 Related Posts
- Overview of JUnit 5
- JUnit 5 Maven Example
- JUnit 5 Standard Test Class Basic Template
- JUnit 5 Annotations with Examples
- JUnit 5 Assertions with Examples
- JUnit 5 Nested Tests Example
- JUnit 5 Disabling Tests Examples
- JUnit 5 Display Names Example
- JUnit 5 Repeated Tests with Examples
- JUnit 5 Exception Testing with Example
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner
Comments
Post a Comment
Leave Comment