Merge pull request #15734 from thibaultfaure/improvement/BAEL-7503-Junit-vs-TestNG-comparison
BAEL-7503 improvement to JUnit vs TestNG comparison article
This commit is contained in:
commit
bd2120a4a2
|
@ -4,10 +4,10 @@ import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
public class CalculatorUnitTest {
|
class CalculatorUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenDividerIsZero_thenDivideByZeroExceptionIsThrown() {
|
void whenDividerIsZero_thenDivideByZeroExceptionIsThrown() {
|
||||||
Calculator calculator = new Calculator();
|
Calculator calculator = new Calculator();
|
||||||
|
|
||||||
assertThrows(DivideByZeroException.class,
|
assertThrows(DivideByZeroException.class,
|
||||||
|
|
|
@ -6,7 +6,7 @@ import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
import org.junit.jupiter.params.provider.ValueSource;
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
|
||||||
public class CustomNameUnitTest {
|
class CustomNameUnitTest {
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@ValueSource(strings = { "Hello", "World" })
|
@ValueSource(strings = { "Hello", "World" })
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
package com.baeldung.junit5vstestng;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.MethodOrderer;
|
||||||
|
import org.junit.jupiter.api.Order;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.TestMethodOrder;
|
||||||
|
|
||||||
|
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||||
|
class OrderedUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(2)
|
||||||
|
void a_givenString_whenChangedtoInt_thenTrue() {
|
||||||
|
assertTrue(Integer.valueOf("10") instanceof Integer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(1)
|
||||||
|
void b_givenInt_whenChangedtoString_thenTrue() {
|
||||||
|
assertTrue(String.valueOf(10) instanceof String);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -12,7 +12,7 @@ import org.junit.jupiter.params.provider.EnumSource;
|
||||||
import org.junit.jupiter.params.provider.MethodSource;
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
import org.junit.jupiter.params.provider.ValueSource;
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
|
||||||
public class ParameterizedUnitTest {
|
class ParameterizedUnitTest {
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@ValueSource(strings = { "Hello", "World" })
|
@ValueSource(strings = { "Hello", "World" })
|
||||||
|
|
|
@ -5,6 +5,6 @@ import org.junit.platform.suite.api.Suite;
|
||||||
|
|
||||||
@Suite
|
@Suite
|
||||||
@SelectClasses({Class1UnitTest.class, Class2UnitTest.class})
|
@SelectClasses({Class1UnitTest.class, Class2UnitTest.class})
|
||||||
public class SelectClassesSuiteUnitTest {
|
class SelectClassesSuiteUnitTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import org.junit.platform.suite.api.SelectPackages;
|
||||||
import org.junit.platform.suite.api.Suite;
|
import org.junit.platform.suite.api.Suite;
|
||||||
|
|
||||||
@Suite
|
@Suite
|
||||||
@SelectPackages({ "com.baeldung.java.suite.junit4", "com.baeldung.java.suite.junit5" })
|
@SelectPackages({ "com.baeldung.junit4", "com.baeldung.junit5" })
|
||||||
public class SelectPackagesSuiteUnitTest {
|
class SelectPackagesSuiteUnitTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.baeldung.junit5vstestng;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.MethodOrderer;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.TestMethodOrder;
|
||||||
|
|
||||||
|
@TestMethodOrder(MethodOrderer.MethodName.class)
|
||||||
|
class SortedUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void a_givenString_whenChangedtoInt_thenTrue() {
|
||||||
|
assertTrue(Integer.valueOf("10") instanceof Integer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void b_givenInt_whenChangedtoString_thenTrue() {
|
||||||
|
assertTrue(String.valueOf(10) instanceof String);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -9,43 +9,44 @@ import org.junit.jupiter.api.AfterAll;
|
||||||
import org.junit.jupiter.api.AfterEach;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Disabled;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class SummationServiceUnitTest {
|
class SummationServiceUnitTest {
|
||||||
private static List<Integer> numbers;
|
private static List<Integer> numbers;
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void initialize() {
|
static void initialize() {
|
||||||
numbers = new ArrayList<>();
|
numbers = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterAll
|
@AfterAll
|
||||||
public static void tearDown() {
|
static void tearDown() {
|
||||||
numbers = null;
|
numbers = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void runBeforeEachTest() {
|
void runBeforeEachTest() {
|
||||||
numbers.add(1);
|
numbers.add(1);
|
||||||
numbers.add(2);
|
numbers.add(2);
|
||||||
numbers.add(3);
|
numbers.add(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
public void runAfterEachTest() {
|
void runAfterEachTest() {
|
||||||
numbers.clear();
|
numbers.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNumbers_sumEquals_thenCorrect() {
|
void givenNumbers_sumEquals_thenCorrect() {
|
||||||
int sum = numbers.stream()
|
int sum = numbers.stream()
|
||||||
.reduce(0, Integer::sum);
|
.reduce(0, Integer::sum);
|
||||||
Assert.assertEquals(6, sum);
|
Assert.assertEquals(6, sum);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Ignore
|
@Disabled
|
||||||
@Test
|
@Test
|
||||||
public void givenEmptyList_sumEqualsZero_thenCorrect() {
|
void givenEmptyList_sumEqualsZero_thenCorrect() {
|
||||||
int sum = numbers.stream()
|
int sum = numbers.stream()
|
||||||
.reduce(0, Integer::sum);
|
.reduce(0, Integer::sum);
|
||||||
Assert.assertEquals(6, sum);
|
Assert.assertEquals(6, sum);
|
||||||
|
|
Loading…
Reference in New Issue