[BAEL-9635] - Moved Junit vs TestNg junit code examples to junit-5 module from core-java
This commit is contained in:
@@ -1,15 +0,0 @@
|
||||
package com.baeldung.throwsexception;
|
||||
|
||||
public class Calculator {
|
||||
|
||||
public double divide(double a, double b) {
|
||||
if (b == 0) {
|
||||
throw new DivideByZeroException("Divider cannot be equal to zero!");
|
||||
}
|
||||
return a/b;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.throwsexception;
|
||||
|
||||
public class DivideByZeroException extends RuntimeException {
|
||||
|
||||
public DivideByZeroException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.baeldung.junit4vstestng;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runners.MethodSorters;
|
||||
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class SortedUnitTest {
|
||||
|
||||
@Test
|
||||
public void a_givenString_whenChangedtoInt_thenTrue() {
|
||||
assertTrue(Integer.valueOf("10") instanceof Integer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void b_givenInt_whenChangedtoString_thenTrue() {
|
||||
assertTrue(String.valueOf(10) instanceof String);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package com.baeldung.junit4vstestng;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SummationServiceIntegrationTest {
|
||||
private static List<Integer> numbers;
|
||||
|
||||
@BeforeClass
|
||||
public static void initialize() {
|
||||
numbers = new ArrayList<>();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDown() {
|
||||
numbers = null;
|
||||
}
|
||||
|
||||
@Before
|
||||
public void runBeforeEachTest() {
|
||||
numbers.add(1);
|
||||
numbers.add(2);
|
||||
numbers.add(3);
|
||||
}
|
||||
|
||||
@After
|
||||
public void runAfterEachTest() {
|
||||
numbers.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNumbers_sumEquals_thenCorrect() {
|
||||
int sum = numbers.stream()
|
||||
.reduce(0, Integer::sum);
|
||||
Assert.assertEquals(6, sum);
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void givenEmptyList_sumEqualsZero_thenCorrect() {
|
||||
int sum = numbers.stream()
|
||||
.reduce(0, Integer::sum);
|
||||
Assert.assertEquals(6, sum);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void givenNumber_whenThrowsException_thenCorrect() {
|
||||
int i = 1 / 0;
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.throwsexception;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public class CalculatorUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenDividerIsZero_thenDivideByZeroExceptionIsThrown() {
|
||||
Calculator calculator = new Calculator();
|
||||
|
||||
assertThrows(DivideByZeroException.class,
|
||||
() -> calculator.divide(10, 0));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package org.baeldung.java.customtestname;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
public class CustomNameUnitTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = { "Hello", "World" })
|
||||
@DisplayName("Test Method to check that the inputs are not nullable")
|
||||
void givenString_TestNullOrNot(String word) {
|
||||
assertNotNull(word);
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package org.baeldung.java.parameterisedsource;
|
||||
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.EnumSet;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.EnumSource;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import com.baeldung.enums.PizzaDeliveryStrategy;
|
||||
|
||||
public class ParameterizedUnitTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = { "Hello", "World" })
|
||||
void givenString_TestNullOrNot(String word) {
|
||||
assertNotNull(word);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@EnumSource(value = PizzaDeliveryStrategy.class, names = {"EXPRESS", "NORMAL"})
|
||||
void givenEnum_TestContainsOrNot(PizzaDeliveryStrategy timeUnit) {
|
||||
assertTrue(EnumSet.of(PizzaDeliveryStrategy.EXPRESS, PizzaDeliveryStrategy.NORMAL).contains(timeUnit));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("wordDataProvider")
|
||||
void givenMethodSource_TestInputStream(String argument) {
|
||||
assertNotNull(argument);
|
||||
}
|
||||
|
||||
static Stream<String> wordDataProvider() {
|
||||
return Stream.of("foo", "bar");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({ "1, Car", "2, House", "3, Train" })
|
||||
void givenCSVSource_TestContent(int id, String word) {
|
||||
assertNotNull(id);
|
||||
assertNotNull(word);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package org.baeldung.java.suite;
|
||||
|
||||
import org.baeldung.java.suite.childpackage1.Class1UnitTest;
|
||||
import org.baeldung.java.suite.childpackage2.Class2UnitTest;
|
||||
import org.junit.platform.runner.JUnitPlatform;
|
||||
import org.junit.platform.suite.api.SelectClasses;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(JUnitPlatform.class)
|
||||
@SelectClasses({Class1UnitTest.class, Class2UnitTest.class})
|
||||
public class SelectClassesSuiteUnitTest {
|
||||
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package org.baeldung.java.suite;
|
||||
|
||||
import org.junit.platform.runner.JUnitPlatform;
|
||||
import org.junit.platform.suite.api.SelectPackages;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(JUnitPlatform.class)
|
||||
@SelectPackages({ "org.baeldung.java.suite.childpackage1", "org.baeldung.java.suite.childpackage2" })
|
||||
public class SelectPackagesSuiteUnitTest {
|
||||
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package org.baeldung.java.suite.childpackage1;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class Class1UnitTest {
|
||||
|
||||
@Test
|
||||
public void testCase_InClass1UnitTest() {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package org.baeldung.java.suite.childpackage2;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class Class2UnitTest {
|
||||
|
||||
@Test
|
||||
public void testCase_InClass2UnitTest() {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user