Junit 5
This commit is contained in:
parent
94bfcd3b26
commit
9b0aa76e7e
@ -1,6 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
@ -14,40 +13,10 @@
|
|||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<java.version>1.8</java.version>
|
<java.version>1.8</java.version>
|
||||||
<!-- Due to a bug in surefire-junit5-5.0.0-ALPHA we use the latest snapshot instead -->
|
<junit.jupiter.version>5.0.0-M2</junit.jupiter.version>
|
||||||
<junit.gen5.version>5.0.0-SNAPSHOT</junit.gen5.version>
|
<junit.platform.version>1.0.0-M2</junit.platform.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<repositories>
|
|
||||||
<repository>
|
|
||||||
<id>snapshots-repo</id>
|
|
||||||
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
|
|
||||||
<releases>
|
|
||||||
<enabled>false</enabled>
|
|
||||||
</releases>
|
|
||||||
<snapshots>
|
|
||||||
<!-- Do NOT cache JUnit 5 snapshot JARs. -->
|
|
||||||
<updatePolicy>always</updatePolicy>
|
|
||||||
<enabled>true</enabled>
|
|
||||||
</snapshots>
|
|
||||||
</repository>
|
|
||||||
</repositories>
|
|
||||||
|
|
||||||
<pluginRepositories>
|
|
||||||
<pluginRepository>
|
|
||||||
<id>snapshots-repo</id>
|
|
||||||
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
|
|
||||||
<releases>
|
|
||||||
<enabled>false</enabled>
|
|
||||||
</releases>
|
|
||||||
<snapshots>
|
|
||||||
<!-- Do NOT cache JUnit 5 snapshot JARs. -->
|
|
||||||
<updatePolicy>always</updatePolicy>
|
|
||||||
<enabled>true</enabled>
|
|
||||||
</snapshots>
|
|
||||||
</pluginRepository>
|
|
||||||
</pluginRepositories>
|
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
@ -63,9 +32,9 @@
|
|||||||
<version>2.19</version>
|
<version>2.19</version>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit</groupId>
|
<groupId>org.junit.platform</groupId>
|
||||||
<artifactId>surefire-junit5</artifactId>
|
<artifactId>junit-platform-surefire-provider</artifactId>
|
||||||
<version>${junit.gen5.version}</version>
|
<version>${junit.platform.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</plugin>
|
</plugin>
|
||||||
@ -74,10 +43,11 @@
|
|||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<artifactId>junit5-api</artifactId>
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
<version>${junit.gen5.version}</version>
|
<version>${junit.jupiter.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
29
junit5/src/test/java/com/baeldung/AssertionTest.java
Normal file
29
junit5/src/test/java/com/baeldung/AssertionTest.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.junit.jupiter.api.Assertions.expectThrows;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class AssertionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConvertToDoubleThrowException() {
|
||||||
|
String age = "eighteen";
|
||||||
|
expectThrows(NumberFormatException.class, () -> {
|
||||||
|
convertToInt(age);
|
||||||
|
});
|
||||||
|
|
||||||
|
assertThrows(NumberFormatException.class, () -> {
|
||||||
|
convertToInt(age);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Integer convertToInt(String str) {
|
||||||
|
if (str == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return Integer.valueOf(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,9 +1,11 @@
|
|||||||
package com.baeldung;
|
package com.baeldung;
|
||||||
|
|
||||||
import org.junit.gen5.api.Test;
|
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 static org.junit.gen5.api.Assertions.assertEquals;
|
import org.junit.jupiter.api.Test;
|
||||||
import static org.junit.gen5.api.Assumptions.*;
|
|
||||||
|
|
||||||
public class AssumptionTest {
|
public class AssumptionTest {
|
||||||
|
|
||||||
|
@ -1,18 +1,26 @@
|
|||||||
package com.baeldung;
|
package com.baeldung;
|
||||||
|
|
||||||
import org.junit.gen5.api.Test;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.junit.jupiter.api.Assertions.expectThrows;
|
||||||
|
|
||||||
import static org.junit.gen5.api.Assertions.assertEquals;
|
import org.junit.jupiter.api.Test;
|
||||||
import static org.junit.gen5.api.Assertions.expectThrows;
|
|
||||||
|
|
||||||
public class ExceptionTest {
|
public class ExceptionTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldThrowException() {
|
void shouldThrowException() {
|
||||||
Throwable exception = expectThrows(UnsupportedOperationException.class,
|
Throwable exception = expectThrows(UnsupportedOperationException.class, () -> {
|
||||||
() -> {
|
|
||||||
throw new UnsupportedOperationException("Not supported");
|
throw new UnsupportedOperationException("Not supported");
|
||||||
});
|
});
|
||||||
assertEquals(exception.getMessage(), "Not supported");
|
assertEquals(exception.getMessage(), "Not supported");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void convertToIntNullParameterAssertThrows() {
|
||||||
|
String str = null;
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
Integer.valueOf(str);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
package com.baeldung;
|
package com.baeldung;
|
||||||
|
|
||||||
import org.junit.gen5.api.Disabled;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import org.junit.gen5.api.Test;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.gen5.api.Assertions.*;
|
import org.junit.jupiter.api.Disabled;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
class FirstTest {
|
class FirstTest {
|
||||||
|
|
||||||
|
50
junit5/src/test/java/com/baeldung/JUnit5NewFeaturesTest.java
Normal file
50
junit5/src/test/java/com/baeldung/JUnit5NewFeaturesTest.java
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
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.Disabled;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
//@RunWith(JUnitPlatform.class)
|
||||||
|
public class JUnit5NewFeaturesTest {
|
||||||
|
|
||||||
|
private static final Logger log = Logger.getLogger(JUnit5NewFeaturesTest.class.getName());
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void setup() {
|
||||||
|
log.info("@BeforeAll - executes once before all test methods in this class");
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void init() {
|
||||||
|
log.info("@BeforeEach - executes before each test method in this class");
|
||||||
|
}
|
||||||
|
|
||||||
|
@DisplayName("Single test successful")
|
||||||
|
@Test
|
||||||
|
void testSingleSuccessTest() {
|
||||||
|
log.info("Success");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled("Not implemented yet.")
|
||||||
|
void testShowSomething() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void tearDown() {
|
||||||
|
log.info("@AfterEach - executed after each test method.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
static void done() {
|
||||||
|
log.info("@AfterAll - executed after all test methods.");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
38
junit5/src/test/java/com/baeldung/LiveTest.java
Normal file
38
junit5/src/test/java/com/baeldung/LiveTest.java
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DynamicTest;
|
||||||
|
import org.junit.jupiter.api.TestFactory;
|
||||||
|
|
||||||
|
public class LiveTest {
|
||||||
|
|
||||||
|
private List<String> in = new ArrayList<>(Arrays.asList("Hello", "Yes", "No"));
|
||||||
|
private List<String> out = new ArrayList<>(Arrays.asList("Cześć", "Tak", "Nie"));
|
||||||
|
|
||||||
|
@TestFactory
|
||||||
|
public Stream<DynamicTest> translateDynamicTestsFromStream() {
|
||||||
|
|
||||||
|
return in.stream().map(word -> DynamicTest.dynamicTest("Test translate " + word, () -> {
|
||||||
|
int id = in.indexOf(word);
|
||||||
|
assertEquals(out.get(id), translate(word));
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String translate(String word) {
|
||||||
|
if ("Hello".equalsIgnoreCase(word)) {
|
||||||
|
return "Cześć";
|
||||||
|
} else if ("Yes".equalsIgnoreCase(word)) {
|
||||||
|
return "Tak";
|
||||||
|
} else if ("No".equalsIgnoreCase(word)) {
|
||||||
|
return "Nie";
|
||||||
|
}
|
||||||
|
return "Error";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,10 +1,14 @@
|
|||||||
package com.baeldung;
|
package com.baeldung;
|
||||||
|
|
||||||
import org.junit.gen5.api.*;
|
|
||||||
|
|
||||||
import java.util.EmptyStackException;
|
import java.util.EmptyStackException;
|
||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class NestedTest {
|
public class NestedTest {
|
||||||
Stack<Object> stack;
|
Stack<Object> stack;
|
||||||
boolean isRun = false;
|
boolean isRun = false;
|
||||||
|
11
junit5/src/test/java/com/baeldung/StringUtils.java
Normal file
11
junit5/src/test/java/com/baeldung/StringUtils.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
public final class StringUtils {
|
||||||
|
|
||||||
|
public static Double convertToDouble(String str) {
|
||||||
|
if (str == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return Double.valueOf(str);
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,9 @@
|
|||||||
package com.baeldung;
|
package com.baeldung;
|
||||||
|
|
||||||
import org.junit.gen5.api.Tag;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import org.junit.gen5.api.Test;
|
|
||||||
|
|
||||||
import static org.junit.gen5.api.Assertions.assertEquals;
|
import org.junit.jupiter.api.Tag;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
@Tag("Test case")
|
@Tag("Test case")
|
||||||
public class TaggedTest {
|
public class TaggedTest {
|
||||||
|
8
junit5/src/test/java/com/baeldung/suites/AllTests.java
Normal file
8
junit5/src/test/java/com/baeldung/suites/AllTests.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package com.baeldung.suites;
|
||||||
|
|
||||||
|
//@RunWith(JUnitPlatform.class)
|
||||||
|
//@SelectPackages("com.baeldung")
|
||||||
|
//@SelectClasses({AssertionTest.class, AssumptionTest.class, ExceptionTest.class})
|
||||||
|
public class AllTests {
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user