Add JUnit 5 examples
This commit is contained in:
commit
bcc31e1855
83
junit5/pom.xml
Normal file
83
junit5/pom.xml
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
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">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>junit5</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<name>junit5</name>
|
||||||
|
<description>Intro to JUnit 5</description>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
<!-- Due to a bug in surefire-junit5-5.0.0-ALPHA we use the latest snapshot instead -->
|
||||||
|
<junit.gen5.version>5.0.0-SNAPSHOT</junit.gen5.version>
|
||||||
|
</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>
|
||||||
|
<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>
|
30
junit5/src/test/java/com/baeldung/AssumptionTest.java
Normal file
30
junit5/src/test/java/com/baeldung/AssumptionTest.java
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import org.junit.gen5.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.gen5.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.gen5.api.Assumptions.*;
|
||||||
|
|
||||||
|
public class AssumptionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void trueAssumption() {
|
||||||
|
assumeTrue(5 > 1);
|
||||||
|
assertEquals(5 + 2, 7);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void falseAssumption() {
|
||||||
|
assumeFalse(5 < 1);
|
||||||
|
assertEquals(5 + 2, 7);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void assumptionThat() {
|
||||||
|
String someString = "Just a string";
|
||||||
|
assumingThat(
|
||||||
|
someString.equals("Just a string"),
|
||||||
|
() -> assertEquals(2 + 2, 4)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
18
junit5/src/test/java/com/baeldung/ExceptionTest.java
Normal file
18
junit5/src/test/java/com/baeldung/ExceptionTest.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import org.junit.gen5.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.gen5.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.gen5.api.Assertions.expectThrows;
|
||||||
|
|
||||||
|
public class ExceptionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldThrowException() {
|
||||||
|
Throwable exception = expectThrows(UnsupportedOperationException.class,
|
||||||
|
() -> {
|
||||||
|
throw new UnsupportedOperationException("Not supported");
|
||||||
|
});
|
||||||
|
assertEquals(exception.getMessage(), "Not supported");
|
||||||
|
}
|
||||||
|
}
|
37
junit5/src/test/java/com/baeldung/FirstTest.java
Normal file
37
junit5/src/test/java/com/baeldung/FirstTest.java
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import org.junit.gen5.api.Disabled;
|
||||||
|
import org.junit.gen5.api.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.gen5.api.Assertions.*;
|
||||||
|
|
||||||
|
class FirstTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void lambdaExpressions() {
|
||||||
|
List<Integer> numbers = Arrays.asList(1, 2, 3);
|
||||||
|
assertTrue(numbers
|
||||||
|
.stream()
|
||||||
|
.mapToInt(i -> i)
|
||||||
|
.sum() > 5, "Sum should be greater than 5");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void groupAssertions() {
|
||||||
|
int[] numbers = {0, 1, 2, 3, 4};
|
||||||
|
assertAll("numbers",
|
||||||
|
() -> assertEquals(numbers[0], 1),
|
||||||
|
() -> assertEquals(numbers[3], 3),
|
||||||
|
() -> assertEquals(numbers[4], 1)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
void disabledTest() {
|
||||||
|
assertTrue(false);
|
||||||
|
}
|
||||||
|
}
|
77
junit5/src/test/java/com/baeldung/NestedTest.java
Normal file
77
junit5/src/test/java/com/baeldung/NestedTest.java
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import org.junit.gen5.api.*;
|
||||||
|
|
||||||
|
import java.util.EmptyStackException;
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
|
public class NestedTest {
|
||||||
|
Stack<Object> stack;
|
||||||
|
boolean isRun = false;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("is instantiated with new Stack()")
|
||||||
|
void isInstantiatedWithNew() {
|
||||||
|
new Stack<Object>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("when new")
|
||||||
|
class WhenNew {
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void init() {
|
||||||
|
stack = new Stack<Object>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("is empty")
|
||||||
|
void isEmpty() {
|
||||||
|
Assertions.assertTrue(stack.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("throws EmptyStackException when popped")
|
||||||
|
void throwsExceptionWhenPopped() {
|
||||||
|
Assertions.expectThrows(EmptyStackException.class, () -> stack.pop());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("throws EmptyStackException when peeked")
|
||||||
|
void throwsExceptionWhenPeeked() {
|
||||||
|
Assertions.expectThrows(EmptyStackException.class, () -> stack.peek());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("after pushing an element")
|
||||||
|
class AfterPushing {
|
||||||
|
|
||||||
|
String anElement = "an element";
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void init() {
|
||||||
|
stack.push(anElement);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("it is no longer empty")
|
||||||
|
void isEmpty() {
|
||||||
|
Assertions.assertFalse(stack.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("returns the element when popped and is empty")
|
||||||
|
void returnElementWhenPopped() {
|
||||||
|
Assertions.assertEquals(anElement, stack.pop());
|
||||||
|
Assertions.assertTrue(stack.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("returns the element when peeked but remains not empty")
|
||||||
|
void returnElementWhenPeeked() {
|
||||||
|
Assertions.assertEquals(anElement, stack.peek());
|
||||||
|
Assertions.assertFalse(stack.isEmpty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
16
junit5/src/test/java/com/baeldung/TaggedTest.java
Normal file
16
junit5/src/test/java/com/baeldung/TaggedTest.java
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import org.junit.gen5.api.Tag;
|
||||||
|
import org.junit.gen5.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.gen5.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
@Tag("Test case")
|
||||||
|
public class TaggedTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Tag("Method")
|
||||||
|
void testMethod() {
|
||||||
|
assertEquals(2 + 2, 4);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user