add junit5 method order examples

This commit is contained in:
DOHA 2019-05-08 14:09:01 +02:00
parent 3010137d3a
commit afac1b1e92
4 changed files with 115 additions and 0 deletions

View File

@ -0,0 +1,33 @@
package com.baeldung.junit5.order;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.MethodOrderer.Alphanumeric;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
@TestMethodOrder(Alphanumeric.class)
public class AlphanumericOrderUnitTest {
private static StringBuilder output = new StringBuilder("");
@Test
public void myATest() {
output.append("A");
}
@Test
public void myBTest() {
output.append("B");
}
@Test
public void myaTest() {
output.append("a");
}
@AfterAll
public static void assertOutput() {
assertEquals(output.toString(), "ABa");
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.junit5.order;
import org.junit.jupiter.api.MethodDescriptor;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.MethodOrdererContext;
public class CustomOrder implements MethodOrderer{
@Override
public void orderMethods(MethodOrdererContext context) {
context.getMethodDescriptors().sort((MethodDescriptor m1, MethodDescriptor m2)->m1.getMethod().getName().compareToIgnoreCase(m2.getMethod().getName()));
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.junit5.order;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
@TestMethodOrder(CustomOrder.class)
public class CustomOrderUnitTest {
private static StringBuilder output = new StringBuilder("");
@Test
public void myATest() {
output.append("A");
}
@Test
public void myBTest() {
output.append("B");
}
@Test
public void myaTest() {
output.append("a");
}
@AfterAll
public static void assertOutput() {
assertEquals(output.toString(), "AaB");
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.junit5.order;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
@TestMethodOrder(OrderAnnotation.class)
public class OrderAnnotationUnitTest {
private static StringBuilder output = new StringBuilder("");
@Test
@Order(1)
public void firstTest() {
output.append("a");
}
@Test
@Order(2)
public void secondTest() {
output.append("b");
}
@Test
@Order(3)
public void thirdTest() {
output.append("c");
}
@AfterAll
public static void assertOutput() {
assertEquals(output.toString(), "abc");
}
}