This commit is contained in:
michal_aibin 2016-11-12 23:11:46 +01:00
parent 94bfcd3b26
commit 9b0aa76e7e
12 changed files with 217 additions and 97 deletions

View File

@ -1,83 +1,53 @@
<?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>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>junit5</artifactId> <artifactId>junit5</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<name>junit5</name> <name>junit5</name>
<description>Intro to JUnit 5</description> <description>Intro to JUnit 5</description>
<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> <build>
<repository> <plugins>
<id>snapshots-repo</id> <plugin>
<url>https://oss.sonatype.org/content/repositories/snapshots</url> <artifactId>maven-compiler-plugin</artifactId>
<releases> <version>3.1</version>
<enabled>false</enabled> <configuration>
</releases> <source>${java.version}</source>
<snapshots> <target>${java.version}</target>
<!-- Do NOT cache JUnit 5 snapshot JARs. --> </configuration>
<updatePolicy>always</updatePolicy> </plugin>
<enabled>true</enabled> <plugin>
</snapshots> <artifactId>maven-surefire-plugin</artifactId>
</repository> <version>2.19</version>
</repositories> <dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<pluginRepositories> <dependencies>
<pluginRepository> <dependency>
<id>snapshots-repo</id> <groupId>org.junit.jupiter</groupId>
<url>https://oss.sonatype.org/content/repositories/snapshots</url> <artifactId>junit-jupiter-engine</artifactId>
<releases> <version>${junit.jupiter.version}</version>
<enabled>false</enabled> <scope>test</scope>
</releases> </dependency>
<snapshots>
<!-- Do NOT cache JUnit 5 snapshot JARs. -->
<updatePolicy>always</updatePolicy>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<build> </dependencies>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>surefire-junit5</artifactId>
<version>${junit.gen5.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit5-api</artifactId>
<version>${junit.gen5.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project> </project>

View 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);
}
}

View File

@ -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 {

View File

@ -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);
});
}
} }

View File

@ -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 {

View 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.");
}
}

View 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";
}
}

View File

@ -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;

View 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);
}
}

View File

@ -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 {

View 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 {
}