Merge pull request #6918 from Doha2012/master
add junit5 method order examples
This commit is contained in:
commit
997ec5f4df
|
@ -21,11 +21,21 @@
|
||||||
<artifactId>junit-platform-engine</artifactId>
|
<artifactId>junit-platform-engine</artifactId>
|
||||||
<version>${junit.platform.version}</version>
|
<version>${junit.platform.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<version>${junit.jupiter.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<artifactId>junit-jupiter-params</artifactId>
|
<artifactId>junit-jupiter-params</artifactId>
|
||||||
<version>${junit.jupiter.version}</version>
|
<version>${junit.jupiter.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
<version>${junit.jupiter.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.platform</groupId>
|
<groupId>org.junit.platform</groupId>
|
||||||
<artifactId>junit-platform-runner</artifactId>
|
<artifactId>junit-platform-runner</artifactId>
|
||||||
|
|
|
@ -2,7 +2,21 @@ package com.baeldung;
|
||||||
|
|
||||||
import static java.time.Duration.ofSeconds;
|
import static java.time.Duration.ofSeconds;
|
||||||
import static java.util.Arrays.asList;
|
import static java.util.Arrays.asList;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertLinesMatch;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTimeout;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
@ -91,11 +105,12 @@ public class AssertionUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenMultipleAssertion_whenAssertingAll_thenOK() {
|
public void givenMultipleAssertion_whenAssertingAll_thenOK() {
|
||||||
|
Object obj = null;
|
||||||
assertAll(
|
assertAll(
|
||||||
"heading",
|
"heading",
|
||||||
() -> assertEquals(4, 2 * 2, "4 is 2 times 2"),
|
() -> assertEquals(4, 2 * 2, "4 is 2 times 2"),
|
||||||
() -> assertEquals("java", "JAVA".toLowerCase()),
|
() -> assertEquals("java", "JAVA".toLowerCase()),
|
||||||
() -> assertEquals(null, null, "null is equal to null")
|
() -> assertEquals(obj, null, "null is equal to null")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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()));
|
||||||
|
}
|
||||||
|
}
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue