Merge pull request #11401 from azhwani/BAEL-5113

BAEL-5113: Improve the Test Order Article
This commit is contained in:
Loredana Crusoveanu 2021-11-19 16:41:28 +02:00 committed by GitHub
commit d3de4302f2
4 changed files with 80 additions and 7 deletions

View File

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

View File

@ -0,0 +1,36 @@
package com.baeldung.junit5.order;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class DefaultOrderUnitTest {
private static StringBuilder output = new StringBuilder("");
@Test
@DisplayName("Test A")
public void myATest() {
output.append("A");
}
@Test
@DisplayName("Test B")
public void myBTest() {
output.append("B");
}
@Test
@DisplayName("Test C")
public void myCTest() {
output.append("C");
}
@AfterAll
public static void assertOutput() {
assertEquals(output.toString(), "ABC");
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.junit5.order;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
@TestMethodOrder(MethodOrderer.Random.class)
public class RandomOrderUnitTest {
private static StringBuilder output = new StringBuilder("");
@Test
public void myATest() {
output.append("A");
}
@Test
public void myBTest() {
output.append("B");
}
@Test
public void myCTest() {
output.append("C");
}
@AfterAll
public static void assertOutput() {
assertEquals(output.toString(), "ACB");
}
}

View File

@ -0,0 +1,2 @@
junit.jupiter.execution.order.random.seed=100
junit.jupiter.testmethod.order.default = org.junit.jupiter.api.MethodOrderer$DisplayName