Merge pull request #12114 from Alex-Golub/study/test-order

The Order of Tests in JUnit
This commit is contained in:
Loredana Crusoveanu 2022-05-27 21:41:39 +03:00 committed by GitHub
commit a1b83f76f2
5 changed files with 50 additions and 51 deletions

View File

@ -1,33 +1,34 @@
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;
import static org.junit.jupiter.api.Assertions.assertEquals;
@TestMethodOrder(MethodOrderer.MethodName.class)
public class AlphanumericOrderUnitTest {
private static StringBuilder output = new StringBuilder("");
private static final StringBuilder output = new StringBuilder("");
@Test
public void myATest() {
void myATest() {
output.append("A");
}
@Test
public void myBTest() {
void myBTest() {
output.append("B");
}
@Test
public void myaTest() {
void myaTest() {
output.append("a");
}
@AfterAll
public static void assertOutput() {
assertEquals(output.toString(), "ABa");
assertEquals("ABa", output.toString());
}
}

View File

@ -1,33 +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;
import static org.junit.jupiter.api.Assertions.assertEquals;
@TestMethodOrder(CustomOrder.class)
public class CustomOrderUnitTest {
private static StringBuilder output = new StringBuilder("");
private static final StringBuilder output = new StringBuilder("");
@Test
public void myATest() {
void myATest() {
output.append("A");
}
@Test
public void myBTest() {
output.append("B");
void myBTest() {
output.append("B");
}
@Test
public void myaTest() {
void myaTest() {
output.append("a");
}
@AfterAll
public static void assertOutput() {
assertEquals(output.toString(), "AaB");
assertEquals("AaB", output.toString());
}
}

View File

@ -1,36 +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;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class DefaultOrderUnitTest {
private static StringBuilder output = new StringBuilder("");
private static final StringBuilder output = new StringBuilder();
@Test
@DisplayName("Test A")
public void myATest() {
void myATest() {
output.append("A");
}
@Test
@DisplayName("Test B")
public void myBTest() {
void myBTest() {
output.append("B");
}
@Test
@DisplayName("Test C")
public void myCTest() {
void myCTest() {
output.append("C");
}
@AfterAll
public static void assertOutput() {
assertEquals(output.toString(), "ABC");
assertEquals("ABC", output.toString());
}
}

View File

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

View File

@ -1,35 +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;
import static org.junit.jupiter.api.Assertions.assertEquals;
@TestMethodOrder(MethodOrderer.Random.class)
public class RandomOrderUnitTest {
private static StringBuilder output = new StringBuilder("");
private static final StringBuilder output = new StringBuilder();
@Test
public void myATest() {
void myATest() {
output.append("A");
}
@Test
public void myBTest() {
void myBTest() {
output.append("B");
}
@Test
public void myCTest() {
void myCTest() {
output.append("C");
}
@AfterAll
public static void assertOutput() {
assertEquals(output.toString(), "ACB");
assertEquals("ACB", output.toString());
}
}