- Invade the Code π½
- Posts
- π Day 10 of #100DaysOfCode: Exploring Advanced JUnit 5 Features! π
π Day 10 of #100DaysOfCode: Exploring Advanced JUnit 5 Features! π
Hey Invaders! π½
#100DaysOfCode day 10, I continued working on my JUnit course and finished section 6: Advanced JUnit 5. Today, I learned and practiced using method orders and test instances. Here's a summary of what I covered:
Method Orders:
Control the order in which test methods are executed.
@TestMethodOrder(MethodOrderer.Random.class): Executes test methods in a random order.
@TestMethodOrder(MethodOrderer.MethodName.class): Executes test methods in lexicographic(a generalization of the alphabetical order of the dictionaries to sequences of ordered symbols or, more generally, of elements of a totally ordered set.) order by method name.
@TestMethodOrder(MethodOrderer.OrderAnnotation.class): Executes test methods based on the
@Order
annotation.
Test Instances:
Manage the lifecycle of test instances.
@TestInstance(TestInstance.Lifecycle.PER_METHOD): A new test instance is created for each test method (default behavior, you donβt have to add this annotation but this is what Junit uses).
@TestInstance(TestInstance.Lifecycle.PER_CLASS): A single test instance is used for all test methods within the test class.
Here's some code snippet demonstrating these concepts:
@TestMethodOrder(MethodOrderer.Random.class): these can be executed as DBAC when the tests are complied for example
@TestMethodOrder(MethodOrderer.Random.class)
public class MethodOrderedRandomlyTest {
@Test
void TestA(){
System.out.println("Running Test A");
}
@Test
void TestB(){
System.out.println("Running Test B");
}
@Test
void TestC(){
System.out.println("Running Test C");
}
@Test
void TestD(){
System.out.println("Running Test D");
}
@TestMethodOrder(MethodOrderer.MethodName.class): these will run as ABCD no matter the order they are put in
@TestMethodOrder(MethodOrderer.MethodName.class)
public class MethodOrderedRandomlyTest {
@Test
void TestD(){
System.out.println("Running Test D");
}
@Test
void TestA(){
System.out.println("Running Test A");
}
@Test
void TestC(){
System.out.println("Running Test C");
}
@Test
void TestB(){
System.out.println("Running Test B");
}
@TestMethodOrder(MethodOrderer.OrderAnnotation.class): these will be DACB because of the Order annotation used
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class MethodOrderedRandomlyTest {
@Order(1)
@Test
void TestD(){
System.out.println("Running Test D");
}
@Order(2)
@Test
void TestA(){
System.out.println("Running Test A");
}
@Order(3)
@Test
void TestC(){
System.out.println("Running Test C");
}
@Order(4)
@Test
void TestB(){
System.out.println("Running Test B");
}
Hereβs a little graphic I made to help you visualize how the Test Instance annotations work in Junit!
@TestInstance(TestInstance.Lifecycle.PER_CLASS): for an example of how to use this annotation please visit my github repo avrubio/JUnitUdemyCourse
This course continues to be enlightening and is helping me become proficient in writing efficient and well-structured tests.
Happy coding! πͺ
Reply