[BAEL-9635] - Moved Junit vs TestNg junit code examples to junit-5 module from core-java
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.throwsexception;
|
||||
|
||||
public class DivideByZeroException extends RuntimeException {
|
||||
|
||||
public DivideByZeroException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.junit5vstestng;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SummationServiceIntegrationTest {
|
||||
private static List<Integer> numbers;
|
||||
|
||||
@BeforeAll
|
||||
public static void initialize() {
|
||||
numbers = new ArrayList<>();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void tearDown() {
|
||||
numbers = null;
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void runBeforeEachTest() {
|
||||
numbers.add(1);
|
||||
numbers.add(2);
|
||||
numbers.add(3);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user