renamed back to testing-modules, pulled together testing-modules-2 modules into single module

This commit is contained in:
Sjmillington
2019-09-04 18:03:26 +01:00
parent ff871516ee
commit ce9ea390ba
558 changed files with 16 additions and 75 deletions

View File

@@ -0,0 +1,14 @@
### Relevant Articles:
- [A Guide to @RepeatedTest in Junit 5](http://www.baeldung.com/junit-5-repeated-test)
- [Guide to Dynamic Tests in Junit 5](http://www.baeldung.com/junit5-dynamic-tests)
- [A Guide to JUnit 5 Extensions](http://www.baeldung.com/junit-5-extensions)
- [Inject Parameters into JUnit Jupiter Unit Tests](http://www.baeldung.com/junit-5-parameters)
- [Mockito and JUnit 5 Using ExtendWith](http://www.baeldung.com/mockito-junit-5-extension)
- [JUnit5 Programmatic Extension Registration with @RegisterExtension](http://www.baeldung.com/junit-5-registerextension-annotation)
- [The Order of Tests in JUnit](http://www.baeldung.com/junit-5-test-order)
- [Running JUnit Tests Programmatically, from a Java Application](https://www.baeldung.com/junit-tests-run-programmatically-from-java)
- [Testing an Abstract Class With JUnit](https://www.baeldung.com/junit-test-abstract-class)
- [A Quick JUnit vs TestNG Comparison](http://www.baeldung.com/junit-vs-testng)
- [Guide to JUnit 5 Parameterized Tests](https://www.baeldung.com/parameterized-tests-junit-5)
- [JUnit 5 Conditional Test Execution with Annotations](https://www.baeldung.com/junit-5-conditional-test-execution)
- [Assertions in JUnit 4 and JUnit 5](http://www.baeldung.com/junit-assertions)

View File

@@ -0,0 +1,147 @@
<?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>
<!-- NOT NEEDED - JSP <groupId>com.baeldung</groupId> -->
<artifactId>junit-5</artifactId>
<version>1.0-SNAPSHOT</version>
<name>junit-5</name>
<description>Intro to JUnit 5</description>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
<version>${junit.platform.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit.vintage.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-migrationsupport</artifactId>
<version>${junit.vintage.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${exec-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.baeldung.TestLauncher</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<junit.jupiter.version>5.4.2</junit.jupiter.version>
<mockito.junit.jupiter.version>2.23.0</mockito.junit.jupiter.version>
<junit.platform.version>1.4.2</junit.platform.version>
<junit.vintage.version>5.4.2</junit.vintage.version>
<log4j2.version>2.8.2</log4j2.version>
<powermock.version>2.0.0-RC.1</powermock.version>
<maven-surefire-plugin.version>2.22.0</maven-surefire-plugin.version>
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
<spring.version>5.0.1.RELEASE</spring.version>
</properties>
</project>

View File

@@ -0,0 +1,17 @@
/**
*
*/
package com.baeldung.abstractclass.abstractmethod;
/**
* When method calls abstract method.
*/
public abstract class AbstractMethodCalling {
public abstract String abstractFunc();
public String defaultImpl() {
String res = abstractFunc();
return (res == null) ? "Default" : (res + " Default");
}
}

View File

@@ -0,0 +1,14 @@
package com.baeldung.abstractclass.indepedentmethod;
/**
* Test Independent Method
*
*/
public abstract class AbstractIndependent {
public abstract int abstractFunc();
public String defaultImpl() {
return "DEFAULT-1";
}
}

View File

@@ -0,0 +1,10 @@
package com.baeldung.abstractclass.indepedentmethod;
public class ConcreteImpl extends AbstractIndependent {
@Override
public int abstractFunc() {
return 4;
}
}

View File

@@ -0,0 +1,22 @@
package com.baeldung.abstractclass.instancefields;
/**
* Test Independent Method
*/
public abstract class AbstractInstanceFields {
protected int count;
private boolean active = false;
public abstract int abstractFunc();
public String testFunc() {
String response;
if (count > 5) {
response = "Overflow";
} else {
response = active ? "Added" : "Blocked";
}
return response;
}
}

View File

@@ -0,0 +1,17 @@
package com.baeldung.abstractclass.privatemethod;
import java.time.LocalDateTime;
public abstract class AbstractPrivateMethods {
public abstract int abstractFunc();
public String defaultImpl() {
return getCurrentDateTime() + "DEFAULT-1";
}
private String getCurrentDateTime() {
return LocalDateTime.now()
.toString();
}
}

View File

@@ -0,0 +1,41 @@
package com.baeldung.junit5.mockito;
public class User {
private Integer id;
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}

View File

@@ -0,0 +1,9 @@
package com.baeldung.junit5.mockito.repository;
import com.baeldung.junit5.mockito.User;
public interface MailClient {
void sendUserRegistrationMail(User user);
}

View File

@@ -0,0 +1,9 @@
package com.baeldung.junit5.mockito.repository;
public interface SettingRepository {
int getUserMinAge();
int getUserNameMinLength();
}

View File

@@ -0,0 +1,10 @@
package com.baeldung.junit5.mockito.repository;
import com.baeldung.junit5.mockito.User;
public interface UserRepository {
User insert(User user);
boolean isUsernameAlreadyExists(String userName);
}

View File

@@ -0,0 +1,46 @@
package com.baeldung.junit5.mockito.service;
import com.baeldung.junit5.mockito.User;
import com.baeldung.junit5.mockito.repository.MailClient;
import com.baeldung.junit5.mockito.repository.SettingRepository;
import com.baeldung.junit5.mockito.repository.UserRepository;
public class DefaultUserService implements UserService {
private UserRepository userRepository;
private SettingRepository settingRepository;
private MailClient mailClient;
public DefaultUserService(UserRepository userRepository, SettingRepository settingRepository, MailClient mailClient) {
this.userRepository = userRepository;
this.settingRepository = settingRepository;
this.mailClient = mailClient;
}
@Override
public User register(User user) {
validate(user);
User insertedUser = userRepository.insert(user);
mailClient.sendUserRegistrationMail(insertedUser);
return insertedUser;
}
private void validate(User user) {
if(user.getName() == null) {
throw new RuntimeException(Errors.USER_NAME_REQUIRED);
}
if(user.getName().length() < settingRepository.getUserNameMinLength()) {
throw new RuntimeException(Errors.USER_NAME_SHORT);
}
if(user.getAge() < settingRepository.getUserMinAge()) {
throw new RuntimeException(Errors.USER_AGE_YOUNG);
}
if(userRepository.isUsernameAlreadyExists(user.getName())) {
throw new RuntimeException(Errors.USER_NAME_DUPLICATE);
}
}
}

View File

@@ -0,0 +1,10 @@
package com.baeldung.junit5.mockito.service;
public class Errors {
public static final String USER_NAME_REQUIRED = "user.name.required";
public static final String USER_NAME_SHORT = "user.name.short";
public static final String USER_AGE_YOUNG = "user.age.young";
public static final String USER_NAME_DUPLICATE = "user.name.duplicate";
}

View File

@@ -0,0 +1,9 @@
package com.baeldung.junit5.mockito.service;
import com.baeldung.junit5.mockito.User;
public interface UserService {
User register(User user);
}

View File

@@ -0,0 +1,15 @@
package com.baeldung.junit5vstestng;
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;
}
}

View File

@@ -0,0 +1,9 @@
package com.baeldung.junit5vstestng;
public class DivideByZeroException extends RuntimeException {
public DivideByZeroException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@@ -0,0 +1,192 @@
package com.baeldung;
import static java.time.Duration.ofSeconds;
import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import static org.junit.jupiter.api.Assertions.assertLinesMatch;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeout;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.function.BooleanSupplier;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
/**
* Unit test that demonstrate the different assertions available within JUnit 4
*/
@DisplayName("Test case for assertions")
public class AssertionUnitTest {
@Test
@DisplayName("Arrays should be equals")
public void whenAssertingArraysEquality_thenEqual() {
char[] expected = {'J', 'u', 'p', 'i', 't', 'e', 'r'};
char[] actual = "Jupiter".toCharArray();
assertArrayEquals(expected, actual, "Arrays should be equal");
}
@Test
@DisplayName("The area of two polygons should be equal")
public void whenAssertingEquality_thenEqual() {
float square = 2 * 2;
float rectangle = 2 * 2;
assertEquals(square, rectangle);
}
@Test
public void whenAssertingEqualityWithDelta_thenEqual() {
float square = 2 * 2;
float rectangle = 3 * 2;
float delta = 2;
assertEquals(square, rectangle, delta);
}
@Test
public void whenAssertingConditions_thenVerified() {
assertTrue(5 > 4, "5 is greater the 4");
assertTrue(null == null, "null is equal to null");
}
@Test
public void whenAssertingNull_thenTrue() {
Object cat = null;
assertNull(cat, () -> "The cat should be null");
}
@Test
public void whenAssertingNotNull_thenTrue() {
Object dog = new Object();
assertNotNull(dog, () -> "The dog should not be null");
}
@Test
public void whenAssertingSameObject_thenSuccessfull() {
String language = "Java";
Optional<String> optional = Optional.of(language);
assertSame(language, optional.get());
}
@Test
public void givenBooleanSupplier_whenAssertingCondition_thenVerified() {
BooleanSupplier condition = () -> 5 > 6;
assertFalse(condition, "5 is not greater then 6");
}
@Test
@Disabled
public void whenFailingATest_thenFailed() {
// Test not completed
fail("FAIL - test not completed");
}
@Test
public void givenMultipleAssertion_whenAssertingAll_thenOK() {
Object obj = null;
assertAll(
"heading",
() -> assertEquals(4, 2 * 2, "4 is 2 times 2"),
() -> assertEquals("java", "JAVA".toLowerCase()),
() -> assertEquals(obj, null, "null is equal to null")
);
}
@Test
public void givenTwoLists_whenAssertingIterables_thenEquals() {
Iterable<String> al = new ArrayList<>(asList("Java", "Junit", "Test"));
Iterable<String> ll = new LinkedList<>(asList("Java", "Junit", "Test"));
assertIterableEquals(al, ll);
}
@Test
public void whenAssertingTimeout_thenNotExceeded() {
assertTimeout(
ofSeconds(2),
() -> {
// code that requires less then 2 minutes to execute
Thread.sleep(1000);
}
);
}
@Test
public void whenAssertingTimeoutPreemptively_thenNotExceeded() {
assertTimeoutPreemptively(
ofSeconds(2),
() -> {
// code that requires less then 2 minutes to execute
Thread.sleep(1000);
}
);
}
@Test
public void whenAssertingEquality_thenNotEqual() {
Integer value = 5; // result of an algorithm
assertNotEquals(0, value, "The result cannot be 0");
}
@Test
public void whenAssertingEqualityListOfStrings_thenEqual() {
List<String> expected = asList("Java", "\\d+", "JUnit");
List<String> actual = asList("Java", "11", "JUnit");
assertLinesMatch(expected, actual);
}
@Test
void whenAssertingException_thenThrown() {
Throwable exception = assertThrows(
IllegalArgumentException.class,
() -> {
throw new IllegalArgumentException("Exception message");
}
);
assertEquals("Exception message", exception.getMessage());
}
@Test
public void testConvertToDoubleThrowException() {
String age = "eighteen";
assertThrows(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

@@ -0,0 +1,126 @@
package com.baeldung;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.function.ThrowingConsumer;
import com.baeldung.helpers.Employee;
import com.baeldung.helpers.EmployeeDao;
public class DynamicTestsExample {
@TestFactory
Collection<DynamicTest> dynamicTestsWithCollection() {
return Arrays.asList(DynamicTest.dynamicTest("Add test", () -> assertEquals(2, Math.addExact(1, 1))), DynamicTest.dynamicTest("Multiply Test", () -> assertEquals(4, Math.multiplyExact(2, 2))));
}
@TestFactory
Iterable<DynamicTest> dynamicTestsWithIterable() {
return Arrays.asList(DynamicTest.dynamicTest("Add test", () -> assertEquals(2, Math.addExact(1, 1))), DynamicTest.dynamicTest("Multiply Test", () -> assertEquals(4, Math.multiplyExact(2, 2))));
}
@TestFactory
Iterator<DynamicTest> dynamicTestsWithIterator() {
return Arrays.asList(DynamicTest.dynamicTest("Add test", () -> assertEquals(2, Math.addExact(1, 1))), DynamicTest.dynamicTest("Multiply Test", () -> assertEquals(4, Math.multiplyExact(2, 2))))
.iterator();
}
@TestFactory
Stream<DynamicTest> dynamicTestsFromIntStream() {
return IntStream.iterate(0, n -> n + 2)
.limit(10)
.mapToObj(n -> DynamicTest.dynamicTest("test" + n, () -> assertTrue(n % 2 == 0)));
}
@TestFactory
Stream<DynamicTest> dynamicTestsFromStream() {
// sample input and output
List<String> inputList = Arrays.asList("www.somedomain.com", "www.anotherdomain.com", "www.yetanotherdomain.com");
List<String> outputList = Arrays.asList("154.174.10.56", "211.152.104.132", "178.144.120.156");
// input generator that generates inputs using inputList
Iterator<String> inputGenerator = inputList.iterator();
// a display name generator that creates a different name based on the input
Function<String, String> displayNameGenerator = (input) -> "Resolving: " + input;
// the test executor, which actually has the logic of how to execute the test case
DomainNameResolver resolver = new DomainNameResolver();
ThrowingConsumer<String> testExecutor = (input) -> {
int id = inputList.indexOf(input);
assertEquals(outputList.get(id), resolver.resolveDomain(input));
};
// combine everything and return a Stream of DynamicTest
return DynamicTest.stream(inputGenerator, displayNameGenerator, testExecutor);
}
@TestFactory
Stream<DynamicTest> dynamicTestsFromStreamInJava8() {
DomainNameResolver resolver = new DomainNameResolver();
List<String> inputList = Arrays.asList("www.somedomain.com", "www.anotherdomain.com", "www.yetanotherdomain.com");
List<String> outputList = Arrays.asList("154.174.10.56", "211.152.104.132", "178.144.120.156");
return inputList.stream()
.map(dom -> DynamicTest.dynamicTest("Resolving: " + dom, () -> {
int id = inputList.indexOf(dom);
assertEquals(outputList.get(id), resolver.resolveDomain(dom));
}));
}
@TestFactory
Stream<DynamicTest> dynamicTestsForEmployeeWorkflows() {
List<Employee> inputList = Arrays.asList(new Employee(1, "Fred"), new Employee(2), new Employee(3, "John"));
EmployeeDao dao = new EmployeeDao();
Stream<DynamicTest> saveEmployeeStream = inputList.stream()
.map(emp -> DynamicTest.dynamicTest("saveEmployee: " + emp.toString(), () -> {
Employee returned = dao.save(emp.getId());
assertEquals(returned.getId(), emp.getId());
}));
Stream<DynamicTest> saveEmployeeWithFirstNameStream = inputList.stream()
.filter(emp -> !emp.getFirstName()
.isEmpty())
.map(emp -> DynamicTest.dynamicTest("saveEmployeeWithName" + emp.toString(), () -> {
Employee returned = dao.save(emp.getId(), emp.getFirstName());
assertEquals(returned.getId(), emp.getId());
assertEquals(returned.getFirstName(), emp.getFirstName());
}));
return Stream.concat(saveEmployeeStream, saveEmployeeWithFirstNameStream);
}
class DomainNameResolver {
private Map<String, String> ipByDomainName = new HashMap<>();
DomainNameResolver() {
this.ipByDomainName.put("www.somedomain.com", "154.174.10.56");
this.ipByDomainName.put("www.anotherdomain.com", "211.152.104.132");
this.ipByDomainName.put("www.yetanotherdomain.com", "178.144.120.156");
}
public String resolveDomain(String domainName) {
return ipByDomainName.get(domainName);
}
}
}

View File

@@ -0,0 +1,50 @@
package com.baeldung;
import java.sql.SQLException;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import com.baeldung.extensions.EmployeeDaoParameterResolver;
import com.baeldung.extensions.EmployeeDatabaseSetupExtension;
import com.baeldung.extensions.EnvironmentExtension;
import com.baeldung.extensions.IgnoreFileNotFoundExceptionExtension;
import com.baeldung.extensions.LoggingExtension;
import com.baeldung.helpers.Employee;
import com.baeldung.helpers.EmployeeJdbcDao;
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith({ EnvironmentExtension.class, EmployeeDatabaseSetupExtension.class, EmployeeDaoParameterResolver.class })
@ExtendWith(LoggingExtension.class)
@ExtendWith(IgnoreFileNotFoundExceptionExtension.class)
public class EmployeesUnitTest {
private EmployeeJdbcDao employeeDao;
private Logger logger;
public EmployeesUnitTest(EmployeeJdbcDao employeeDao) {
this.employeeDao = employeeDao;
}
@Test
public void whenAddEmployee_thenGetEmployee() throws SQLException {
Employee emp = new Employee(1, "john");
employeeDao.add(emp);
assertEquals(1, employeeDao.findAll()
.size());
}
@Test
public void whenGetEmployees_thenEmptyList() throws SQLException {
assertEquals(0, employeeDao.findAll()
.size());
}
public void setLogger(Logger logger) {
this.logger = logger;
}
}

View File

@@ -0,0 +1,28 @@
package com.baeldung;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.extension.RegisterExtension;
import com.baeldung.extensions.EmployeeDatabaseSetupExtension;
public class MultipleExtensionsUnitTest {
@Order(1)
@RegisterExtension
static EmployeeDatabaseSetupExtension SECOND_DB =
new EmployeeDatabaseSetupExtension("jdbc:h2:mem:DbTwo;DB_CLOSE_DELAY=-1", "org.h2.Driver", "sa", "");
@Order(0)
@RegisterExtension
static EmployeeDatabaseSetupExtension FIRST_DB =
new EmployeeDatabaseSetupExtension("jdbc:h2:mem:DbOne;DB_CLOSE_DELAY=-1", "org.h2.Driver", "sa", "");
@RegisterExtension
static EmployeeDatabaseSetupExtension LAST_DB =
new EmployeeDatabaseSetupExtension("jdbc:h2:mem:DbLast;DB_CLOSE_DELAY=-1", "org.h2.Driver", "sa", "");
@Test
public void justDemonstratingTheIdea() {
// empty test
}
}

View File

@@ -0,0 +1,81 @@
package com.baeldung;
import java.util.EmptyStackException;
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 NestedUnitTest {
Stack<Object> stack;
@Test
@DisplayName("is instantiated with new Stack()")
void isInstantiatedWithNew() {
new Stack<>();
}
@Nested
@DisplayName("when new")
class WhenNew {
@BeforeEach
void init() {
stack = new Stack<>();
}
@Test
@DisplayName("is empty")
void isEmpty() {
Assertions.assertTrue(stack.isEmpty());
}
@Test
@DisplayName("throws EmptyStackException when popped")
void throwsExceptionWhenPopped() {
Assertions.assertThrows(EmptyStackException.class, () -> stack.pop());
}
@Test
@DisplayName("throws EmptyStackException when peeked")
void throwsExceptionWhenPeeked() {
Assertions.assertThrows(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());
}
}
}
}

View File

@@ -0,0 +1,40 @@
package com.baeldung;
import java.sql.SQLException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
import com.baeldung.helpers.Employee;
import com.baeldung.extensions.EmployeeDaoParameterResolver;
import com.baeldung.extensions.EmployeeDatabaseSetupExtension;
import com.baeldung.extensions.EnvironmentExtension;
import com.baeldung.helpers.EmployeeJdbcDao;
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith({ EnvironmentExtension.class, EmployeeDaoParameterResolver.class })
public class ProgrammaticEmployeesUnitTest {
private EmployeeJdbcDao employeeDao;
@RegisterExtension static EmployeeDatabaseSetupExtension DB =
new EmployeeDatabaseSetupExtension("jdbc:h2:mem:AnotherDb;DB_CLOSE_DELAY=-1", "org.h2.Driver", "sa", "");
public ProgrammaticEmployeesUnitTest(EmployeeJdbcDao employeeDao) {
this.employeeDao = employeeDao;
}
@Test
public void whenAddEmployee_thenGetEmployee() throws SQLException {
Employee emp = new Employee(1, "john");
employeeDao.add(emp);
assertEquals(1, employeeDao.findAll().size());
}
@Test
public void whenGetEmployees_thenEmptyList() throws SQLException {
assertEquals(0, employeeDao.findAll().size());
}
}

View File

@@ -0,0 +1,27 @@
package com.baeldung;
import com.baeldung.extensions.RegisterExtensionSampleExtension;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
/**
* This test demonstrates the use of the same extension in two ways.
* 1. Once as instance level field: Only method level callbacks are called.
* 2. Once as class level static field: All methods are called.
*/
public class RegisterExtensionUnitTest {
@RegisterExtension
static RegisterExtensionSampleExtension staticExtension = new RegisterExtensionSampleExtension("static version");
@RegisterExtension
RegisterExtensionSampleExtension instanceLevelExtension = new RegisterExtensionSampleExtension("instance version");
@Test
public void demoTest() {
Assertions.assertEquals("instance version", instanceLevelExtension.getType());
}
}

View File

@@ -0,0 +1,52 @@
package com.baeldung;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.RepetitionInfo;
import org.junit.jupiter.api.TestInfo;
public class RepeatedTestExample {
@BeforeEach
void beforeEachTest() {
System.out.println("Before Each Test");
}
@AfterEach
void afterEachTest() {
System.out.println("After Each Test");
System.out.println("=====================");
}
@RepeatedTest(3)
void repeatedTest(TestInfo testInfo) {
System.out.println("Executing repeated test");
assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2");
}
@RepeatedTest(value = 3, name = RepeatedTest.LONG_DISPLAY_NAME)
void repeatedTestWithLongName() {
System.out.println("Executing repeated test with long name");
assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2");
}
@RepeatedTest(value = 3, name = RepeatedTest.SHORT_DISPLAY_NAME)
void repeatedTestWithShortName() {
System.out.println("Executing repeated test with long name");
assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2");
}
@RepeatedTest(value = 3, name = "Custom name {currentRepetition}/{totalRepetitions}")
void repeatedTestWithCustomDisplayName() {
assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2");
}
@RepeatedTest(3)
void repeatedTestWithRepetitionInfo(RepetitionInfo repetitionInfo) {
System.out.println("Repetition #" + repetitionInfo.getCurrentRepetition());
assertEquals(3, repetitionInfo.getTotalRepetitions());
}
}

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

@@ -0,0 +1,16 @@
package com.baeldung;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
@Tag("Test case")
public class TaggedUnitTest {
@Test
@Tag("Method")
void testMethod() {
assertEquals(2 + 2, 4);
}
}

View File

@@ -0,0 +1,39 @@
package com.baeldung;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestPlan;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
import java.io.PrintWriter;
import org.junit.platform.launcher.Launcher;
public class TestLauncher {
public static void main(String[] args) {
//@formatter:off
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(selectClass("com.baeldung.EmployeesUnitTest"))
.configurationParameter("junit.conditions.deactivate", "com.baeldung.extensions.*")
.configurationParameter("junit.jupiter.extensions.autodetection.enabled", "true")
.build();
//@formatter:on
TestPlan plan = LauncherFactory.create()
.discover(request);
Launcher launcher = LauncherFactory.create();
SummaryGeneratingListener summaryGeneratingListener = new SummaryGeneratingListener();
launcher.execute(request, new TestExecutionListener[] { summaryGeneratingListener });
launcher.execute(request);
summaryGeneratingListener.getSummary()
.printTo(new PrintWriter(System.out));
}
}

View File

@@ -0,0 +1,38 @@
package com.baeldung.abstractclass.abstractmethod;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
public class AbstractMethodCallingUnitTest {
private AbstractMethodCalling cls;
@BeforeEach
public void setup() {
cls = Mockito.mock(AbstractMethodCalling.class);
}
@Test
public void givenDefaultImpl_whenMockAbstractFunc_thenExpectedBehaviour() {
Mockito
.when(cls.abstractFunc())
.thenReturn("Abstract");
Mockito
.doCallRealMethod()
.when(cls)
.defaultImpl();
// validate result by mock abstractFunc's behaviour
Assertions.assertEquals("Abstract Default", cls.defaultImpl());
// check the value with null response from abstract method
Mockito
.doReturn(null)
.when(cls)
.abstractFunc();
Assertions.assertEquals("Default", cls.defaultImpl());
}
}

View File

@@ -0,0 +1,25 @@
/**
*
*/
package com.baeldung.abstractclass.indepedentmethod;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
public class AbstractIndependentUnitTest {
@Test
public void givenNonAbstractMethod_whenConcreteImpl_testCorrectBehaviour() {
ConcreteImpl conClass = new ConcreteImpl();
String actual = conClass.defaultImpl();
Assertions.assertEquals("DEFAULT-1", actual);
}
@Test
public void givenNonAbstractMethod_whenMockitoMock_testCorrectBehaviour() {
AbstractIndependent absCls = Mockito.mock(AbstractIndependent.class, Mockito.CALLS_REAL_METHODS);
Assertions.assertEquals("DEFAULT-1", absCls.defaultImpl());
}
}

View File

@@ -0,0 +1,42 @@
package com.baeldung.abstractclass.instancefields;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.reflect.Whitebox;
public class AbstractInstanceFieldsUnitTest {
@Test
public void givenProtectedInstanceField_whenMockClassCountGt5_thenTestNonAbstractMethod() {
// mock
AbstractInstanceFields instClass = Mockito.mock(AbstractInstanceFields.class);
Mockito
.doCallRealMethod()
.when(instClass)
.testFunc();
// set counter greater than 5
instClass.count = 7;
// compare the result
Assertions.assertEquals("Overflow", instClass.testFunc());
}
@Test
public void givenNonAbstractMethodAndPrivateField_whenPowerMockitoAndActiveFieldTrue_thenCorrectBehaviour() {
AbstractInstanceFields instClass = PowerMockito.mock(AbstractInstanceFields.class);
PowerMockito
.doCallRealMethod()
.when(instClass)
.testFunc();
Whitebox.setInternalState(instClass, "active", true);
// compare the expected result with actual
Assertions.assertEquals("Added", instClass.testFunc());
}
}

View File

@@ -0,0 +1,41 @@
/**
*
*/
package com.baeldung.abstractclass.privatemethod;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.time.LocalDateTime;
/**
* Providing custom values for private methods using powermock
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(AbstractPrivateMethods.class)
public class AbstractPrivateMethodsUnitTest {
@Test
public void givenNonAbstractMethodAndCallPrivateMethod_whenMockPrivateMethod_thenVerifyBehaviour() throws Exception {
AbstractPrivateMethods mockClass = PowerMockito.mock(AbstractPrivateMethods.class);
String dateTime = LocalDateTime
.now()
.toString();
PowerMockito
.doCallRealMethod()
.when(mockClass)
.defaultImpl();
PowerMockito
.doReturn(dateTime)
.when(mockClass, "getCurrentDateTime");// .thenReturn(dateTime);
String actual = mockClass.defaultImpl();
Assertions.assertEquals(dateTime + "DEFAULT-1", actual);
}
}

View File

@@ -0,0 +1,119 @@
package com.baeldung.conditional;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.*;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public class ConditionalAnnotationsUnitTest {
@Test
@EnabledOnOs({OS.WINDOWS, OS.MAC})
public void shouldRunBothWindowsAndMac() {
System.out.println("runs on Windows and Mac");
}
@Test
@DisabledOnOs(OS.LINUX)
public void shouldNotRunAtLinux() {
System.out.println("will not run on Linux");
}
@Test
@EnabledOnJre({JRE.JAVA_10, JRE.JAVA_11})
public void shouldOnlyRunOnJava10And11() {
System.out.println("runs with java 10 and 11");
}
@Test
@DisabledOnJre(JRE.OTHER)
public void thisTestOnlyRunsWithUpToDateJREs() {
System.out.println("this test will only run on java8, 9, 10 and 11.");
}
@Test
@EnabledIfSystemProperty(named = "java.vm.vendor", matches = "Oracle.*")
public void onlyIfVendorNameStartsWithOracle() {
System.out.println("runs only if vendor name starts with Oracle");
}
@Test
@DisabledIfSystemProperty(named = "file.separator", matches = "[/]")
public void disabledIfFileSeperatorIsSlash() {
System.out.println("Will not run if file.sepeartor property is /");
}
@Test
@EnabledIfEnvironmentVariable(named = "GDMSESSION", matches = "ubuntu")
public void onlyRunOnUbuntuServer() {
System.out.println("only runs if GDMSESSION is ubuntu");
}
@Test
@DisabledIfEnvironmentVariable(named = "LC_TIME", matches = ".*UTF-8.")
public void shouldNotRunWhenTimeIsNotUTF8() {
System.out.println("will not run if environment variable LC_TIME is UTF-8");
}
@Test
@EnabledIf("'FR' == systemProperty.get('user.country')")
public void onlyFrenchPeopleWillRunThisMethod() {
System.out.println("will run only if user.country is FR");
}
@Test
@DisabledIf("java.lang.System.getProperty('os.name').toLowerCase().contains('mac')")
public void shouldNotRunOnMacOS() {
System.out.println("will not run if our os.name is mac");
}
@Test
@EnabledIf(value = {
"load('nashorn:mozilla_compat.js')",
"importPackage(java.time)",
"",
"var thisMonth = LocalDate.now().getMonth().name()",
"var february = Month.FEBRUARY.name()",
"thisMonth.equals(february)"
},
engine = "nashorn",
reason = "Self-fulfilling: {result}")
public void onlyRunsInFebruary() {
System.out.println("this test only runs in February");
}
@Test
@DisabledIf("systemEnvironment.get('XPC_SERVICE_NAME') != null " +
"&& systemEnvironment.get('XPC_SERVICE_NAME').contains('intellij')")
public void notValidForIntelliJ() {
System.out.println("this test will run if our ide is INTELLIJ");
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Test
@DisabledOnOs({OS.WINDOWS, OS.SOLARIS, OS.OTHER})
@EnabledOnJre({JRE.JAVA_9, JRE.JAVA_10, JRE.JAVA_11})
@interface ThisTestWillOnlyRunAtLinuxAndMacWithJava9Or10Or11 {
}
@ThisTestWillOnlyRunAtLinuxAndMacWithJava9Or10Or11
public void someSuperTestMethodHere() {
System.out.println("this method will run with java9, 10, 11 and Linux or macOS.");
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@DisabledIf("Math.random() >= 0.5")
@interface CoinToss {
}
@RepeatedTest(2)
@CoinToss
public void gamble() {
System.out.println("This tests run status is a gamble with %50 rate");
}
}

View File

@@ -0,0 +1,25 @@
package com.baeldung.extensions;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
import com.baeldung.helpers.EmployeeJdbcDao;
import com.baeldung.helpers.JdbcConnectionUtil;
public class EmployeeDaoParameterResolver implements ParameterResolver {
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
return parameterContext.getParameter()
.getType()
.equals(EmployeeJdbcDao.class);
}
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
return new EmployeeJdbcDao(JdbcConnectionUtil.getConnection());
}
}

View File

@@ -0,0 +1,56 @@
package com.baeldung.extensions;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Savepoint;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import com.baeldung.helpers.EmployeeJdbcDao;
import com.baeldung.helpers.JdbcConnectionUtil;
public class EmployeeDatabaseSetupExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback {
private Connection con;
private EmployeeJdbcDao employeeDao;
private Savepoint savepoint;
public EmployeeDatabaseSetupExtension() {
con = JdbcConnectionUtil.getConnection();
employeeDao = new EmployeeJdbcDao(con);
}
public EmployeeDatabaseSetupExtension(String jdbcUrl, String driver, String username, String password) {
con = JdbcConnectionUtil.getConnection(jdbcUrl, driver, username, password);
employeeDao = new EmployeeJdbcDao(con);
}
@Override
public void afterAll(ExtensionContext context) throws SQLException {
if (con != null) {
con.close();
}
}
@Override
public void beforeAll(ExtensionContext context) throws SQLException {
employeeDao.createTable();
}
@Override
public void afterEach(ExtensionContext context) throws SQLException {
con.rollback(savepoint);
}
@Override
public void beforeEach(ExtensionContext context) throws SQLException {
con.setAutoCommit(false);
savepoint = con.setSavepoint("before");
}
}

View File

@@ -0,0 +1,27 @@
package com.baeldung.extensions;
import java.io.IOException;
import java.util.Properties;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
public class EnvironmentExtension implements ExecutionCondition {
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
Properties props = new Properties();
try {
props.load(EnvironmentExtension.class.getResourceAsStream("application.properties"));
String env = props.getProperty("env");
if ("qa".equalsIgnoreCase(env)) {
return ConditionEvaluationResult.disabled("Test disabled on QA environment");
}
} catch (IOException e) {
e.printStackTrace();
}
return ConditionEvaluationResult.enabled("Test enabled on QA environment");
}
}

View File

@@ -0,0 +1,23 @@
package com.baeldung.extensions;
import java.io.FileNotFoundException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
public class IgnoreFileNotFoundExceptionExtension implements TestExecutionExceptionHandler {
Logger logger = LogManager.getLogger(IgnoreFileNotFoundExceptionExtension.class);
@Override
public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
if (throwable instanceof FileNotFoundException) {
logger.error("File not found:" + throwable.getMessage());
return;
}
throw throwable;
}
}

View File

@@ -0,0 +1,18 @@
package com.baeldung.extensions;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestInstancePostProcessor;
public class LoggingExtension implements TestInstancePostProcessor {
@Override
public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception {
Logger logger = LogManager.getLogger(testInstance.getClass());
testInstance.getClass()
.getMethod("setLogger", Logger.class)
.invoke(testInstance, logger);
}
}

View File

@@ -0,0 +1,34 @@
package com.baeldung.extensions;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This extension is meant to demonstrate the use of RegisterExtension.
*/
public class RegisterExtensionSampleExtension implements BeforeAllCallback, BeforeEachCallback {
private final String type;
Logger logger = LoggerFactory.getLogger(RegisterExtensionSampleExtension.class);
public RegisterExtensionSampleExtension(String type) {
this.type = type;
}
@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
logger.info("Type {} In beforeAll : {}", type, extensionContext.getDisplayName());
}
@Override
public void beforeEach(ExtensionContext extensionContext) throws Exception {
logger.info("Type {} In beforeEach : {}", type, extensionContext.getDisplayName());
}
public String getType() {
return type;
}
}

View File

@@ -0,0 +1,38 @@
package com.baeldung.helpers;
public class Employee {
private long id;
private String firstName;
public Employee(long id) {
this.id = id;
this.firstName = "";
}
public Employee(long id, String firstName) {
this.id = id;
this.firstName = firstName;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName + "]";
}
}

View File

@@ -0,0 +1,16 @@
package com.baeldung.helpers;
public class EmployeeDao {
public Employee save(long id) {
return new Employee(id);
}
public Employee save(long id, String firstName) {
return new Employee(id, firstName);
}
public Employee update(Employee employee) {
return employee;
}
}

View File

@@ -0,0 +1,51 @@
package com.baeldung.helpers;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class EmployeeJdbcDao {
private Connection con;
public EmployeeJdbcDao(Connection con) {
this.con = con;
}
public void createTable() throws SQLException {
String createQuery = "CREATE TABLE IF NOT EXISTS employees(id long primary key, firstName varchar(50))";
PreparedStatement pstmt = con.prepareStatement(createQuery);
pstmt.execute();
}
public void add(Employee emp) throws SQLException {
String insertQuery = "INSERT INTO employees(id, firstName) VALUES(?,?)";
PreparedStatement pstmt = con.prepareStatement(insertQuery);
pstmt.setLong(1, emp.getId());
pstmt.setString(2, emp.getFirstName());
pstmt.executeUpdate();
}
public List<Employee> findAll() throws SQLException {
List<Employee> employees = new ArrayList<>();
String query = "SELECT * FROM employees";
PreparedStatement pstmt = con.prepareStatement(query);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Employee emp = new Employee(rs.getLong("id"), rs.getString("firstName"));
employees.add(emp);
}
return employees;
}
}

View File

@@ -0,0 +1,59 @@
package com.baeldung.helpers;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class JdbcConnectionUtil {
private static Connection con;
public static Connection getConnection() {
if (con == null || isClosed(con)) {
try {
Properties props = new Properties();
props.load(JdbcConnectionUtil.class.getResourceAsStream("jdbc.properties"));
String jdbcUrl = props.getProperty("jdbc.url");
String driver = props.getProperty("jdbc.driver");
String username = props.getProperty("jdbc.user");
String password = props.getProperty("jdbc.password");
con = getConnection(jdbcUrl, driver, username, password);
return con;
} catch (IOException exc) {
exc.printStackTrace();
}
return null;
}
return con;
}
public static Connection getConnection(String jdbcUrl, String driver, String username, String password) {
if (con == null || isClosed(con)) {
try {
Class.forName(driver);
con = DriverManager.getConnection(jdbcUrl, username, password);
return con;
} catch (ClassNotFoundException exc) {
exc.printStackTrace();
} catch (SQLException exc) {
exc.printStackTrace();
}
return null;
}
return con;
}
private static boolean isClosed(Connection con) {
try {
return con.isClosed();
} catch (SQLException e) {
return true;
}
}
}

View File

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

View File

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

View File

@@ -0,0 +1,126 @@
package com.baeldung.junit5.mockito;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.stubbing.Answer;
import com.baeldung.junit5.mockito.repository.MailClient;
import com.baeldung.junit5.mockito.repository.SettingRepository;
import com.baeldung.junit5.mockito.repository.UserRepository;
import com.baeldung.junit5.mockito.service.DefaultUserService;
import com.baeldung.junit5.mockito.service.Errors;
import com.baeldung.junit5.mockito.service.UserService;
@ExtendWith(MockitoExtension.class)
@RunWith(JUnitPlatform.class)
public class UserServiceUnitTest {
UserService userService;
SettingRepository settingRepository;
@Mock UserRepository userRepository;
@Mock MailClient mailClient;
User user;
@BeforeEach
void init(@Mock SettingRepository settingRepository) {
userService = new DefaultUserService(userRepository, settingRepository, mailClient);
lenient().when(settingRepository.getUserMinAge()).thenReturn(10);
when(settingRepository.getUserNameMinLength()).thenReturn(4);
lenient().when(userRepository.isUsernameAlreadyExists(any(String.class))).thenReturn(false);
this.settingRepository = settingRepository;
}
@Test
void givenValidUser_whenSaveUser_thenSucceed(@Mock MailClient mailClient) {
// Given
user = new User("Jerry", 12);
when(userRepository.insert(any(User.class))).then(new Answer<User>() {
int sequence = 1;
@Override
public User answer(InvocationOnMock invocation) throws Throwable {
User user = (User) invocation.getArgument(0);
user.setId(sequence++);
return user;
}
});
userService = new DefaultUserService(userRepository, settingRepository, mailClient);
// When
User insertedUser = userService.register(user);
// Then
verify(userRepository).insert(user);
Assertions.assertNotNull(user.getId());
verify(mailClient).sendUserRegistrationMail(insertedUser);
}
@Test
void givenShortName_whenSaveUser_thenGiveShortUsernameError() {
// Given
user = new User("tom", 12);
// When
try {
userService.register(user);
fail("Should give an error");
} catch(Exception ex) {
assertEquals(ex.getMessage(), Errors.USER_NAME_SHORT);
}
// Then
verify(userRepository, never()).insert(user);
}
@Test
void givenSmallAge_whenSaveUser_thenGiveYoungUserError() {
// Given
user = new User("jerry", 3);
// When
try {
userService.register(user);
fail("Should give an error");
} catch(Exception ex) {
assertEquals(ex.getMessage(), Errors.USER_AGE_YOUNG);
}
// Then
verify(userRepository, never()).insert(user);
}
@Test
void givenUserWithExistingName_whenSaveUser_thenGiveUsernameAlreadyExistsError() {
// Given
user = new User("jerry", 12);
Mockito.reset(userRepository);
when(userRepository.isUsernameAlreadyExists(any(String.class))).thenReturn(true);
// When
try {
userService.register(user);
fail("Should give an error");
} catch(Exception ex) {
assertEquals(ex.getMessage(), Errors.USER_NAME_DUPLICATE);
}
// Then
verify(userRepository, never()).insert(user);
}
}

View File

@@ -0,0 +1,33 @@
package com.baeldung.junit5.order;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.MethodOrderer.Alphanumeric;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
@TestMethodOrder(Alphanumeric.class)
public class AlphanumericOrderUnitTest {
private static StringBuilder output = new StringBuilder("");
@Test
public void myATest() {
output.append("A");
}
@Test
public void myBTest() {
output.append("B");
}
@Test
public void myaTest() {
output.append("a");
}
@AfterAll
public static void assertOutput() {
assertEquals(output.toString(), "ABa");
}
}

View File

@@ -0,0 +1,12 @@
package com.baeldung.junit5.order;
import org.junit.jupiter.api.MethodDescriptor;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.MethodOrdererContext;
public class CustomOrder implements MethodOrderer{
@Override
public void orderMethods(MethodOrdererContext context) {
context.getMethodDescriptors().sort((MethodDescriptor m1, MethodDescriptor m2)->m1.getMethod().getName().compareToIgnoreCase(m2.getMethod().getName()));
}
}

View File

@@ -0,0 +1,33 @@
package com.baeldung.junit5.order;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
@TestMethodOrder(CustomOrder.class)
public class CustomOrderUnitTest {
private static StringBuilder output = new StringBuilder("");
@Test
public void myATest() {
output.append("A");
}
@Test
public void myBTest() {
output.append("B");
}
@Test
public void myaTest() {
output.append("a");
}
@AfterAll
public static void assertOutput() {
assertEquals(output.toString(), "AaB");
}
}

View File

@@ -0,0 +1,37 @@
package com.baeldung.junit5.order;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
@TestMethodOrder(OrderAnnotation.class)
public class OrderAnnotationUnitTest {
private static StringBuilder output = new StringBuilder("");
@Test
@Order(1)
public void firstTest() {
output.append("a");
}
@Test
@Order(2)
public void secondTest() {
output.append("b");
}
@Test
@Order(3)
public void thirdTest() {
output.append("c");
}
@AfterAll
public static void assertOutput() {
assertEquals(output.toString(), "abc");
}
}

View File

@@ -0,0 +1,17 @@
package com.baeldung.junit5vstestng;
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));
}
}

View File

@@ -0,0 +1,15 @@
package com.baeldung.junit5vstestng;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.Test;
public class Class1UnitTest {
@Test
public void testCase_InClass1UnitTest() {
assertTrue(true);
}
}

View File

@@ -0,0 +1,14 @@
package com.baeldung.junit5vstestng;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.Test;
public class Class2UnitTest {
@Test
public void testCase_InClass2UnitTest() {
assertTrue(true);
}
}

View File

@@ -0,0 +1,17 @@
package com.baeldung.junit5vstestng;
import static org.junit.Assert.assertNotNull;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
public class CustomNameUnitTest {
@ParameterizedTest
@ValueSource(strings = { "Hello", "World" })
@DisplayName("Test Method to check that the inputs are not nullable")
void givenString_TestNullOrNot(String word) {
assertNotNull(word);
}
}

View File

@@ -0,0 +1,45 @@
package com.baeldung.junit5vstestng;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.EnumSet;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
public class ParameterizedUnitTest {
@ParameterizedTest
@ValueSource(strings = { "Hello", "World" })
void givenString_TestNullOrNot(String word) {
assertNotNull(word);
}
@ParameterizedTest
@EnumSource(value = PizzaDeliveryStrategy.class, names = {"EXPRESS", "NORMAL"})
void givenEnum_TestContainsOrNot(PizzaDeliveryStrategy timeUnit) {
assertTrue(EnumSet.of(PizzaDeliveryStrategy.EXPRESS, PizzaDeliveryStrategy.NORMAL).contains(timeUnit));
}
@ParameterizedTest
@MethodSource("wordDataProvider")
void givenMethodSource_TestInputStream(String argument) {
assertNotNull(argument);
}
static Stream<String> wordDataProvider() {
return Stream.of("foo", "bar");
}
@ParameterizedTest
@CsvSource({ "1, Car", "2, House", "3, Train" })
void givenCSVSource_TestContent(int id, String word) {
assertNotNull(id);
assertNotNull(word);
}
}

View File

@@ -0,0 +1,6 @@
package com.baeldung.junit5vstestng;
public enum PizzaDeliveryStrategy {
EXPRESS,
NORMAL;
}

View File

@@ -0,0 +1,11 @@
package com.baeldung.junit5vstestng;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.runner.RunWith;
@RunWith(JUnitPlatform.class)
@SelectClasses({Class1UnitTest.class, Class2UnitTest.class})
public class SelectClassesSuiteUnitTest {
}

View File

@@ -0,0 +1,11 @@
package com.baeldung.junit5vstestng;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.runner.RunWith;
@RunWith(JUnitPlatform.class)
@SelectPackages({ "org.baeldung.java.suite.childpackage1", "org.baeldung.java.suite.childpackage2" })
public class SelectPackagesSuiteUnitTest {
}

View File

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

View File

@@ -0,0 +1,33 @@
package com.baeldung.methodorders;
import static org.junit.Assert.assertEquals;
import org.junit.AfterClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
@FixMethodOrder(MethodSorters.DEFAULT)
public class DefaultOrderOfExecutionUnitTest {
private static StringBuilder output = new StringBuilder("");
@Test
public void secondTest() {
output.append("b");
}
@Test
public void thirdTest() {
output.append("c");
}
@Test
public void firstTest() {
output.append("a");
}
@AfterClass
public static void assertOutput() {
assertEquals(output.toString(), "cab");
}
}

View File

@@ -0,0 +1,26 @@
package com.baeldung.methodorders;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
@FixMethodOrder(MethodSorters.JVM)
public class JVMOrderOfExecutionUnitTest {
private static StringBuilder output = new StringBuilder("");
@Test
public void secondTest() {
output.append("b");
}
@Test
public void thirdTest() {
output.append("c");
}
@Test
public void firstTest() {
output.append("a");
}
}

View File

@@ -0,0 +1,33 @@
package com.baeldung.methodorders;
import static org.junit.Assert.assertEquals;
import org.junit.AfterClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class NameAscendingOrderOfExecutionUnitTest {
private static StringBuilder output = new StringBuilder("");
@Test
public void secondTest() {
output.append("b");
}
@Test
public void thirdTest() {
output.append("c");
}
@Test
public void firstTest() {
output.append("a");
}
@AfterClass
public static void assertOutput() {
assertEquals(output.toString(), "abc");
}
}

View File

@@ -0,0 +1,59 @@
package com.baeldung.param;
import java.util.Random;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
public class InvalidPersonParameterResolver implements ParameterResolver {
/**
* The "bad" (invalid) data for testing purposes has to go somewhere, right?
*/
public static Person[] INVALID_PERSONS = { new Person().setId(1L)
.setLastName("Ad_ams")
.setFirstName("Jill,"),
new Person().setId(2L)
.setLastName(",Baker")
.setFirstName(""),
new Person().setId(3L)
.setLastName(null)
.setFirstName(null),
new Person().setId(4L)
.setLastName("Daniel&")
.setFirstName("{Joseph}"),
new Person().setId(5L)
.setLastName("")
.setFirstName("English, Jane"),
new Person()/* .setId(6L).setLastName("Fontana").setFirstName("Enrique") */,
// TODO: ADD MORE DATA HERE
};
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
Object ret = null;
//
// Return a random, valid Person object if Person.class is the type of Parameter
/// to be resolved. Otherwise return null.
if (parameterContext.getParameter()
.getType() == Person.class) {
ret = INVALID_PERSONS[new Random().nextInt(INVALID_PERSONS.length)];
}
return ret;
}
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
boolean ret = false;
//
// If the Parameter.type == Person.class, then we support it, otherwise, get outta here!
if (parameterContext.getParameter()
.getType() == Person.class) {
ret = true;
}
return ret;
}
}

View File

@@ -0,0 +1,43 @@
package com.baeldung.param;
/**
* Very simple Person entity.
* Use the Fluent-style interface to set properties.
*
* @author J Steven Perry
*
*/
public class Person {
private Long id;
private String lastName;
private String firstName;
public Long getId() {
return id;
}
public Person setId(Long id) {
this.id = id;
return this;
}
public String getLastName() {
return lastName;
}
public Person setLastName(String lastName) {
this.lastName = lastName;
return this;
}
public String getFirstName() {
return firstName;
}
public Person setFirstName(String firstName) {
this.firstName = firstName;
return this;
}
}

View File

@@ -0,0 +1,132 @@
package com.baeldung.param;
import java.util.Arrays;
/**
* Somewhat contrived validation class to illustrate unit test
* concepts.
*
* @author J Steven Perry
*
*/
public class PersonValidator {
/**
* Contrived checked exception to illustrate one possible
* way to handle validation errors (via a checked exception).
*
* @author J Steven Perry
*
*/
public static class ValidationException extends Exception {
/**
*
*/
private static final long serialVersionUID = -134518049431883102L;
// Probably should implement some more constructors, but don't want
/// to tarnish the lesson...
/**
* The one and only way to create this checked exception.
*
* @param message
* The message accompanying the exception. Should be meaningful.
*/
public ValidationException(String message) {
super(message);
}
}
private static final String[] ILLEGAL_NAME_CHARACTERS = { ",", "_", "{", "}", "!" };
/**
* Validate the first name of the specified Person object.
*
* @param person
* The Person object to validate.
*
* @return - returns true if the specified Person is valid
*
* @throws ValidationException
* - this Exception is thrown if any kind of validation error occurs.
*/
public static boolean validateFirstName(Person person) throws ValidationException {
boolean ret = true;
// The validation rules go here.
// Naive: use simple ifs
if (person == null) {
throw new ValidationException("Person is null (not allowed)!");
}
if (person.getFirstName() == null) {
throw new ValidationException("Person FirstName is null (not allowed)!");
}
if (person.getFirstName()
.isEmpty()) {
throw new ValidationException("Person FirstName is an empty String (not allowed)!");
}
if (!isStringValid(person.getFirstName(), ILLEGAL_NAME_CHARACTERS)) {
throw new ValidationException("Person FirstName (" + person.getFirstName() + ") may not contain any of the following characters: " + Arrays.toString(ILLEGAL_NAME_CHARACTERS) + "!");
}
return ret;
}
/**
* Validate the last name of the specified Person object. Looks the same as first
* name? Look closer. Just kidding. It's the same. But real world code can (and will) diverge.
*
* @param person
* The Person object to validate.
*
* @return - returns true if the specified Person is valid
*
* @throws ValidationException
* - this Exception is thrown if any kind of validation error occurs.
*/
public static boolean validateLastName(Person person) throws ValidationException {
boolean ret = true;
// The validation rules go here.
// Naive: use simple ifs
if (person == null) {
throw new ValidationException("Person is null (not allowed)!");
}
if (person.getFirstName() == null) {
throw new ValidationException("Person FirstName is null (not allowed)!");
}
if (person.getFirstName()
.isEmpty()) {
throw new ValidationException("Person FirstName is an empty String (not allowed)!");
}
if (!isStringValid(person.getFirstName(), ILLEGAL_NAME_CHARACTERS)) {
throw new ValidationException("Person LastName (" + person.getLastName() + ") may not contain any of the following characters: " + Arrays.toString(ILLEGAL_NAME_CHARACTERS) + "!");
}
return ret;
}
/**
* Validates the specified name. If it contains any of the illegalCharacters,
* this method returns false (indicating the name is illegal). Otherwise it returns true.
*
* @param candidate
* The candidate String to validate
*
* @param illegalCharacters
* The characters the String is not allowed to have
*
* @return - boolean - true if the name is valid, false otherwise.
*/
private static boolean isStringValid(String candidate, String[] illegalCharacters) {
boolean ret = true;
for (String illegalChar : illegalCharacters) {
if (candidate.contains(illegalChar)) {
ret = false;
break;
}
}
return ret;
}
}

View File

@@ -0,0 +1,102 @@
package com.baeldung.param;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
@RunWith(JUnitPlatform.class)
@DisplayName("Testing PersonValidator")
public class PersonValidatorUnitTest {
/**
* Nested class, uses ExtendWith
* {@link com.baeldung.param.ValidPersonParameterResolver ValidPersonParameterResolver}
* to feed Test methods with "good" data.
*/
@Nested
@DisplayName("When using Valid data")
@ExtendWith(ValidPersonParameterResolver.class)
public class ValidData {
/**
* Repeat the test ten times, that way we have a good shot at
* running all of the data through at least once.
*
* @param person
* A valid Person object to validate.
*/
@RepeatedTest(value = 10)
@DisplayName("All first names are valid")
public void validateFirstName(Person person) {
try {
assertTrue(PersonValidator.validateFirstName(person));
} catch (PersonValidator.ValidationException e) {
fail("Exception not expected: " + e.getLocalizedMessage());
}
}
/**
* Repeat the test ten times, that way we have a good shot at
* running all of the data through at least once.
*
* @param person
* A valid Person object to validate.
*/
@RepeatedTest(value = 10)
@DisplayName("All last names are valid")
public void validateLastName(Person person) {
try {
assertTrue(PersonValidator.validateLastName(person));
} catch (PersonValidator.ValidationException e) {
fail("Exception not expected: " + e.getLocalizedMessage());
}
}
}
/**
* Nested class, uses ExtendWith
* {@link com.baeldung.param.InvalidPersonParameterResolver InvalidPersonParameterResolver}
* to feed Test methods with "bad" data.
*/
@Nested
@DisplayName("When using Invalid data")
@ExtendWith(InvalidPersonParameterResolver.class)
public class InvalidData {
/**
* Repeat the test ten times, that way we have a good shot at
* running all of the data through at least once.
*
* @param person
* An invalid Person object to validate.
*/
@RepeatedTest(value = 10)
@DisplayName("All first names are invalid")
public void validateFirstName(Person person) {
assertThrows(PersonValidator.ValidationException.class, () -> PersonValidator.validateFirstName(person));
}
/**
* Repeat the test ten times, that way we have a good shot at
* running all of the data through at least once.
*
* @param person
* An invalid Person object to validate.
*/
@RepeatedTest(value = 10)
@DisplayName("All first names are invalid")
public void validateLastName(Person person) {
assertThrows(PersonValidator.ValidationException.class, () -> PersonValidator.validateLastName(person));
}
}
}

View File

@@ -0,0 +1,61 @@
package com.baeldung.param;
import java.util.Random;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
public class ValidPersonParameterResolver implements ParameterResolver {
/**
* The "good" (valid) data for testing purposes has to go somewhere, right?
*/
public static Person[] VALID_PERSONS = { new Person().setId(1L)
.setLastName("Adams")
.setFirstName("Jill"),
new Person().setId(2L)
.setLastName("Baker")
.setFirstName("James"),
new Person().setId(3L)
.setLastName("Carter")
.setFirstName("Samanta"),
new Person().setId(4L)
.setLastName("Daniels")
.setFirstName("Joseph"),
new Person().setId(5L)
.setLastName("English")
.setFirstName("Jane"),
new Person().setId(6L)
.setLastName("Fontana")
.setFirstName("Enrique"),
// TODO: ADD MORE DATA HERE
};
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
Object ret = null;
//
// Return a random, valid Person object if Person.class is the type of Parameter
/// to be resolved. Otherwise return null.
if (parameterContext.getParameter()
.getType() == Person.class) {
ret = VALID_PERSONS[new Random().nextInt(VALID_PERSONS.length)];
}
return ret;
}
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
boolean ret = false;
//
// If the Parameter.type == Person.class, then we support it, otherwise, get outta here!
if (parameterContext.getParameter()
.getType() == Person.class) {
ret = true;
}
return ret;
}
}

View File

@@ -0,0 +1,19 @@
package com.baeldung.parameterized;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import java.util.stream.Stream;
class BlankStringsArgumentsProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
Arguments.of((String) null),
Arguments.of(""),
Arguments.of(" ")
);
}
}

View File

@@ -0,0 +1,50 @@
package com.baeldung.parameterized;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.EnumSource;
import java.time.Month;
import java.util.EnumSet;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class EnumsUnitTest {
@ParameterizedTest
@EnumSource(Month.class)
void getValueForAMonth_IsAlwaysBetweenOneAndTwelve(Month month) {
int monthNumber = month.getValue();
assertTrue(monthNumber >= 1 && monthNumber <= 12);
}
@ParameterizedTest(name = "{index} {0} is 30 days long")
@EnumSource(value = Month.class, names = {"APRIL", "JUNE", "SEPTEMBER", "NOVEMBER"})
void someMonths_Are30DaysLong(Month month) {
final boolean isALeapYear = false;
assertEquals(30, month.length(isALeapYear));
}
@ParameterizedTest
@EnumSource(value = Month.class, names = {"APRIL", "JUNE", "SEPTEMBER", "NOVEMBER", "FEBRUARY"}, mode = EnumSource.Mode.EXCLUDE)
void exceptFourMonths_OthersAre31DaysLong(Month month) {
final boolean isALeapYear = false;
assertEquals(31, month.length(isALeapYear));
}
@ParameterizedTest
@EnumSource(value = Month.class, names = ".+BER", mode = EnumSource.Mode.MATCH_ANY)
void fourMonths_AreEndingWithBer(Month month) {
EnumSet<Month> months = EnumSet.of(Month.SEPTEMBER, Month.OCTOBER, Month.NOVEMBER, Month.DECEMBER);
assertTrue(months.contains(month));
}
@ParameterizedTest
@CsvSource({"APRIL", "JUNE", "SEPTEMBER", "NOVEMBER"})
void someMonths_Are30DaysLongCsv(Month month) {
final boolean isALeapYear = false;
assertEquals(30, month.length(isALeapYear));
}
}

View File

@@ -0,0 +1,18 @@
package com.baeldung.parameterized;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.converter.ConvertWith;
import org.junit.jupiter.params.provider.CsvSource;
import java.time.LocalDate;
import static org.junit.jupiter.api.Assertions.assertEquals;
class LocalDateUnitTest {
@ParameterizedTest
@CsvSource({"2018/12/25,2018", "2019/02/11,2019"})
void getYear_ShouldWorkAsExpected(@ConvertWith(SlashyDateConverter.class) LocalDate date, int expected) {
assertEquals(expected, date.getYear());
}
}

View File

@@ -0,0 +1,8 @@
package com.baeldung.parameterized;
public class Numbers {
public static boolean isOdd(int number) {
return number % 2 != 0;
}
}

View File

@@ -0,0 +1,15 @@
package com.baeldung.parameterized;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertTrue;
class NumbersUnitTest {
@ParameterizedTest
@ValueSource(ints = {1, 3, 5, -3, 15, Integer.MAX_VALUE})
void isOdd_ShouldReturnTrueForOddNumbers(int number) {
assertTrue(Numbers.isOdd(number));
}
}

View File

@@ -0,0 +1,20 @@
package com.baeldung.parameterized;
class Person {
private final String firstName;
private final String middleName;
private final String lastName;
public Person(String firstName, String middleName, String lastName) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
}
public String fullName() {
if (middleName == null || middleName.trim().isEmpty()) return String.format("%s %s", firstName, lastName);
return String.format("%s %s %s", firstName, middleName, lastName);
}
}

View File

@@ -0,0 +1,15 @@
package com.baeldung.parameterized;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.params.aggregator.ArgumentsAccessor;
import org.junit.jupiter.params.aggregator.ArgumentsAggregationException;
import org.junit.jupiter.params.aggregator.ArgumentsAggregator;
class PersonAggregator implements ArgumentsAggregator {
@Override
public Object aggregateArguments(ArgumentsAccessor accessor, ParameterContext context)
throws ArgumentsAggregationException {
return new Person(accessor.getString(1), accessor.getString(2), accessor.getString(3));
}
}

View File

@@ -0,0 +1,31 @@
package com.baeldung.parameterized;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.aggregator.AggregateWith;
import org.junit.jupiter.params.aggregator.ArgumentsAccessor;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
class PersonUnitTest {
@ParameterizedTest
@CsvSource({"Isaac,,Newton, Isaac Newton", "Charles,Robert,Darwin,Charles Robert Darwin"})
void fullName_ShouldGenerateTheExpectedFullName(ArgumentsAccessor argumentsAccessor) {
String firstName = argumentsAccessor.getString(0);
String middleName = (String) argumentsAccessor.get(1);
String lastName = argumentsAccessor.get(2, String.class);
String expectedFullName = argumentsAccessor.getString(3);
Person person = new Person(firstName, middleName, lastName);
assertEquals(expectedFullName, person.fullName());
}
@ParameterizedTest
@CsvSource({"Isaac Newton,Isaac,,Newton", "Charles Robert Darwin,Charles,Robert,Darwin"})
void fullName_ShouldGenerateTheExpectedFullName(String expectedFullName,
@AggregateWith(PersonAggregator.class) Person person) {
assertEquals(expectedFullName, person.fullName());
}
}

View File

@@ -0,0 +1,27 @@
package com.baeldung.parameterized;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.params.converter.ArgumentConversionException;
import org.junit.jupiter.params.converter.ArgumentConverter;
import java.time.LocalDate;
class SlashyDateConverter implements ArgumentConverter {
@Override
public Object convert(Object source, ParameterContext context) throws ArgumentConversionException {
if (!(source instanceof String))
throw new IllegalArgumentException("The argument should be a string: " + source);
try {
String[] parts = ((String) source).split("/");
int year = Integer.parseInt(parts[0]);
int month = Integer.parseInt(parts[1]);
int day = Integer.parseInt(parts[2]);
return LocalDate.of(year, month, day);
} catch (Exception e) {
throw new IllegalArgumentException("Failed to convert", e);
}
}
}

View File

@@ -0,0 +1,10 @@
package com.baeldung.parameterized;
import java.util.stream.Stream;
public class StringParams {
static Stream<String> blankStrings() {
return Stream.of(null, "", " ");
}
}

View File

@@ -0,0 +1,8 @@
package com.baeldung.parameterized;
class Strings {
static boolean isBlank(String input) {
return input == null || input.trim().isEmpty();
}
}

View File

@@ -0,0 +1,131 @@
package com.baeldung.parameterized;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class StringsUnitTest {
static Stream<Arguments> arguments = Stream.of(
Arguments.of(null, true), // null strings should be considered blank
Arguments.of("", true),
Arguments.of(" ", true),
Arguments.of("not blank", false)
);
@ParameterizedTest
@VariableSource("arguments")
void isBlank_ShouldReturnTrueForNullOrBlankStringsVariableSource(String input, boolean expected) {
assertEquals(expected, Strings.isBlank(input));
}
@ParameterizedTest
@ValueSource(strings = {"", " "})
void isBlank_ShouldReturnTrueForNullOrBlankStrings(String input) {
assertTrue(Strings.isBlank(input));
}
@ParameterizedTest
@MethodSource("provideStringsForIsBlank")
void isBlank_ShouldReturnTrueForNullOrBlankStrings(String input, boolean expected) {
assertEquals(expected, Strings.isBlank(input));
}
@ParameterizedTest
@MethodSource // Please note method name is not provided
void isBlank_ShouldReturnTrueForNullOrBlankStringsOneArgument(String input) {
assertTrue(Strings.isBlank(input));
}
@ParameterizedTest
@MethodSource("com.baeldung.parameterized.StringParams#blankStrings")
void isBlank_ShouldReturnTrueForNullOrBlankStringsExternalSource(String input) {
assertTrue(Strings.isBlank(input));
}
@ParameterizedTest
@ArgumentsSource(BlankStringsArgumentsProvider.class)
void isBlank_ShouldReturnTrueForNullOrBlankStringsArgProvider(String input) {
assertTrue(Strings.isBlank(input));
}
private static Stream<String> isBlank_ShouldReturnTrueForNullOrBlankStringsOneArgument() {
return Stream.of(null, "", " ");
}
@ParameterizedTest
@MethodSource("provideStringsForIsBlankList")
void isBlank_ShouldReturnTrueForNullOrBlankStringsList(String input, boolean expected) {
assertEquals(expected, Strings.isBlank(input));
}
@ParameterizedTest
@CsvSource({"test,TEST", "tEst,TEST", "Java,JAVA"}) // Passing a CSV pair per test execution
void toUpperCase_ShouldGenerateTheExpectedUppercaseValue(String input, String expected) {
String actualValue = input.toUpperCase();
assertEquals(expected, actualValue);
}
@ParameterizedTest
@CsvSource(value = {"test:test", "tEst:test", "Java:java"}, delimiter =':') // Using : as the column separator.
void toLowerCase_ShouldGenerateTheExpectedLowercaseValue(String input, String expected) {
String actualValue = input.toLowerCase();
assertEquals(expected, actualValue);
}
@ParameterizedTest
@CsvFileSource(resources = "/data.csv", numLinesToSkip = 1)
void toUpperCase_ShouldGenerateTheExpectedUppercaseValueCSVFile(String input, String expected) {
String actualValue = input.toUpperCase();
assertEquals(expected, actualValue);
}
@ParameterizedTest
@NullSource
void isBlank_ShouldReturnTrueForNullInputs(String input) {
assertTrue(Strings.isBlank(input));
}
@ParameterizedTest
@EmptySource
void isBlank_ShouldReturnTrueForEmptyStrings(String input) {
assertTrue(Strings.isBlank(input));
}
@ParameterizedTest
@NullAndEmptySource
void isBlank_ShouldReturnTrueForNullAndEmptyStrings(String input) {
assertTrue(Strings.isBlank(input));
}
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {" ", "\t", "\n"})
void isBlank_ShouldReturnTrueForAllTypesOfBlankStrings(String input) {
assertTrue(Strings.isBlank(input));
}
private static Stream<Arguments> provideStringsForIsBlank() {
return Stream.of(
Arguments.of(null, true), // null strings should be considered blank
Arguments.of("", true),
Arguments.of(" ", true),
Arguments.of("not blank", false)
);
}
private static List<Arguments> provideStringsForIsBlankList() {
return Arrays.asList(
Arguments.of(null, true), // null strings should be considered blank
Arguments.of("", true),
Arguments.of(" ", true),
Arguments.of("not blank", false)
);
}
}

View File

@@ -0,0 +1,45 @@
package com.baeldung.parameterized;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.support.AnnotationConsumer;
import java.lang.reflect.Field;
import java.util.stream.Stream;
class VariableArgumentsProvider implements ArgumentsProvider, AnnotationConsumer<VariableSource> {
private String variableName;
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return context.getTestClass()
.map(this::getField)
.map(this::getValue)
.orElseThrow(() -> new IllegalArgumentException("Failed to load test arguments"));
}
@Override
public void accept(VariableSource variableSource) {
variableName = variableSource.value();
}
private Field getField(Class<?> clazz) {
try {
return clazz.getDeclaredField(variableName);
} catch (Exception e) {
return null;
}
}
@SuppressWarnings("unchecked")
private Stream<Arguments> getValue(Field field) {
Object value = null;
try {
value = field.get(null);
} catch (Exception ignored) {}
return value == null ? null : (Stream<Arguments>) value;
}
}

View File

@@ -0,0 +1,19 @@
package com.baeldung.parameterized;
import org.junit.jupiter.params.provider.ArgumentsSource;
import java.lang.annotation.*;
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@ArgumentsSource(VariableArgumentsProvider.class)
public @interface VariableSource {
/**
* Represents the name of the static variable to load the test arguments from.
*
* @return Static variable name.
*/
String value();
}

View File

@@ -0,0 +1,24 @@
package com.baeldung.runfromjava;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
class FirstUnitTest {
@Test
void whenThis_thenThat() {
assertTrue(true);
}
@Test
void whenSomething_thenSomething() {
assertTrue(true);
}
@Test
void whenSomethingElse_thenSomethingElse() {
assertTrue(true);
}
}

View File

@@ -0,0 +1,59 @@
package com.baeldung.runfromjava;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.TestPlan;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
import org.junit.platform.launcher.listeners.TestExecutionSummary;
import java.io.PrintWriter;
import static org.junit.platform.engine.discovery.ClassNameFilter.includeClassNamePatterns;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectPackage;
public class RunJUnit5TestsFromJava {
SummaryGeneratingListener listener = new SummaryGeneratingListener();
public void runOne() {
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder
.request()
.selectors(selectClass(FirstUnitTest.class))
.build();
Launcher launcher = LauncherFactory.create();
TestPlan testPlan = launcher.discover(request);
launcher.registerTestExecutionListeners(listener);
launcher.execute(request);
}
public void runAll() {
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder
.request()
.selectors(selectPackage("com.baeldung.runfromjava"))
.filters(includeClassNamePatterns(".*Test"))
.build();
Launcher launcher = LauncherFactory.create();
TestPlan testPlan = launcher.discover(request);
launcher.registerTestExecutionListeners(listener);
launcher.execute(request);
}
public static void main(String[] args) {
RunJUnit5TestsFromJava runner = new RunJUnit5TestsFromJava();
runner.runAll();
TestExecutionSummary summary = runner.listener.getSummary();
summary.printTo(new PrintWriter(System.out));
runner.runOne();
summary = runner.listener.getSummary();
summary.printTo(new PrintWriter(System.out));
}
}

View File

@@ -0,0 +1,18 @@
package com.baeldung.runfromjava;
import org.junit.jupiter.api.RepeatedTest;
import static org.junit.jupiter.api.Assertions.assertTrue;
class SecondUnitTest {
@RepeatedTest(10)
void whenSomething_thenSomething() {
assertTrue(true);
}
@RepeatedTest(5)
void whenSomethingElse_thenSomethingElse() {
assertTrue(true);
}
}

View File

@@ -0,0 +1 @@
com.baeldung.extensions.EmployeeDaoParameterResolver

View File

@@ -0,0 +1,5 @@
#h2 db
jdbc.driver=org.h2.Driver
jdbc.url=jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1
jdbc.user=sa
jdbc.password=

View File

@@ -0,0 +1,4 @@
input,expected
test,TEST
tEst,TEST
Java,JAVA
1 input expected
2 test TEST
3 tEst TEST
4 Java JAVA