Merge pull request #12100 from Alex-Golub/study/junit5-intro
JUnit5 writing test conventions, reformatting and import optimization
This commit is contained in:
commit
98d8db1b11
|
@ -1,35 +1,34 @@
|
||||||
package com.baeldung;
|
package com.baeldung;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import org.junit.jupiter.api.Test;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import org.junit.jupiter.api.function.Executable;
|
||||||
|
|
||||||
import java.util.ConcurrentModificationException;
|
import java.util.ConcurrentModificationException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import org.junit.jupiter.api.function.Executable;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
public class ExceptionUnitTest {
|
class ExceptionUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldThrowException() {
|
void shouldThrowException() {
|
||||||
Throwable exception = assertThrows(UnsupportedOperationException.class, () -> {
|
Throwable exception = assertThrows(UnsupportedOperationException.class, () -> {
|
||||||
throw new UnsupportedOperationException("Not supported");
|
throw new UnsupportedOperationException("Not supported");
|
||||||
});
|
});
|
||||||
assertEquals(exception.getMessage(), "Not supported");
|
|
||||||
|
assertEquals("Not supported", exception.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void assertThrowsException() {
|
void assertThrowsException() {
|
||||||
String str = null;
|
String str = null;
|
||||||
assertThrows(IllegalArgumentException.class, () -> {
|
assertThrows(IllegalArgumentException.class, () -> Integer.valueOf(str));
|
||||||
Integer.valueOf(str);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenModifyMapDuringIteration_thenThrowExecption() {
|
void whenModifyMapDuringIteration_thenThrowException() {
|
||||||
Map<Integer, String> hashmap = new HashMap<>();
|
Map<Integer, String> hashmap = new HashMap<>();
|
||||||
hashmap.put(1, "One");
|
hashmap.put(1, "One");
|
||||||
hashmap.put(2, "Two");
|
hashmap.put(2, "Two");
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
package com.baeldung;
|
package com.baeldung;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import org.junit.jupiter.api.Disabled;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Disabled;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
class FirstUnitTest {
|
class FirstUnitTest {
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ class FirstUnitTest {
|
||||||
void lambdaExpressions() {
|
void lambdaExpressions() {
|
||||||
List<Integer> numbers = Arrays.asList(1, 2, 3);
|
List<Integer> numbers = Arrays.asList(1, 2, 3);
|
||||||
assertTrue(numbers.stream()
|
assertTrue(numbers.stream()
|
||||||
.mapToInt(i -> i)
|
.mapToInt(Integer::intValue)
|
||||||
.sum() > 5, "Sum should be greater than 5");
|
.sum() > 5, "Sum should be greater than 5");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,12 +22,16 @@ class FirstUnitTest {
|
||||||
@Test
|
@Test
|
||||||
void groupAssertions() {
|
void groupAssertions() {
|
||||||
int[] numbers = {0, 1, 2, 3, 4};
|
int[] numbers = {0, 1, 2, 3, 4};
|
||||||
assertAll("numbers", () -> assertEquals(numbers[0], 1), () -> assertEquals(numbers[3], 3), () -> assertEquals(numbers[4], 1));
|
assertAll("numbers",
|
||||||
|
() -> assertEquals(numbers[0], 1),
|
||||||
|
() -> assertEquals(numbers[3], 3),
|
||||||
|
() -> assertEquals(numbers[4], 1)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Disabled
|
@Disabled("Disabled test example")
|
||||||
void disabledTest() {
|
void disabledTest() {
|
||||||
assertTrue(false);
|
fail();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,10 @@
|
||||||
package com.baeldung;
|
package com.baeldung;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.*;
|
||||||
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.junit.jupiter.api.AfterAll;
|
class JUnit5NewFeaturesUnitTest {
|
||||||
import org.junit.jupiter.api.AfterEach;
|
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Disabled;
|
|
||||||
import org.junit.jupiter.api.DisplayName;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
public class JUnit5NewFeaturesUnitTest {
|
|
||||||
|
|
||||||
private static final Logger log = Logger.getLogger(JUnit5NewFeaturesUnitTest.class.getName());
|
private static final Logger log = Logger.getLogger(JUnit5NewFeaturesUnitTest.class.getName());
|
||||||
|
|
||||||
|
@ -28,7 +22,6 @@ public class JUnit5NewFeaturesUnitTest {
|
||||||
@Test
|
@Test
|
||||||
void testSingleSuccessTest() {
|
void testSingleSuccessTest() {
|
||||||
log.info("Success");
|
log.info("Success");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -1,22 +1,22 @@
|
||||||
package com.baeldung;
|
package com.baeldung;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import org.junit.jupiter.api.DynamicTest;
|
||||||
|
import org.junit.jupiter.api.TestFactory;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import org.junit.jupiter.api.DynamicTest;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import org.junit.jupiter.api.TestFactory;
|
|
||||||
|
|
||||||
public class LiveTest {
|
class LiveTest {
|
||||||
|
|
||||||
private List<String> in = new ArrayList<>(Arrays.asList("Hello", "Yes", "No"));
|
private List<String> in = new ArrayList<>(Arrays.asList("Hello", "Yes", "No"));
|
||||||
private List<String> out = new ArrayList<>(Arrays.asList("Cześć", "Tak", "Nie"));
|
private List<String> out = new ArrayList<>(Arrays.asList("Cześć", "Tak", "Nie"));
|
||||||
|
|
||||||
@TestFactory
|
@TestFactory
|
||||||
public Stream<DynamicTest> translateDynamicTestsFromStream() {
|
Stream<DynamicTest> translateDynamicTestsFromStream() {
|
||||||
|
|
||||||
return in.stream()
|
return in.stream()
|
||||||
.map(word -> DynamicTest.dynamicTest("Test translate " + word, () -> {
|
.map(word -> DynamicTest.dynamicTest("Test translate " + word, () -> {
|
||||||
|
|
|
@ -1,28 +1,26 @@
|
||||||
package com.baeldung.migration.junit5;
|
package com.baeldung.migration.junit5;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
import static org.junit.jupiter.api.Assumptions.assumeFalse;
|
|
||||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
|
||||||
import static org.junit.jupiter.api.Assumptions.assumingThat;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class AssumptionUnitTest {
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assumptions.*;
|
||||||
|
|
||||||
|
class AssumptionUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void trueAssumption() {
|
void trueAssumption() {
|
||||||
assumeTrue(5 > 1, () -> "5 is greater the 1");
|
assumeTrue(5 > 1, () -> "5 is greater the 1");
|
||||||
assertEquals(5 + 2, 7);
|
assertEquals(5 + 2, 7);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void falseAssumption() {
|
void falseAssumption() {
|
||||||
assumeFalse(5 < 1, () -> "5 is less then 1");
|
assumeFalse(5 < 1, () -> "5 is less then 1");
|
||||||
assertEquals(5 + 2, 7);
|
assertEquals(5 + 2, 7);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void assumptionThat() {
|
void assumptionThat() {
|
||||||
String someString = "Just a string";
|
String someString = "Just a string";
|
||||||
assumingThat(someString.equals("Just a string"), () -> assertEquals(2 + 2, 4));
|
assumingThat(someString.equals("Just a string"), () -> assertEquals(2 + 2, 4));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue