Skip to content

Commit

Permalink
Add TestFactory TableData example & Draft update to JDK 12
Browse files Browse the repository at this point in the history
  • Loading branch information
drandarov-io committed Nov 4, 2019
1 parent 7edb722 commit 46de2e8
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 20 deletions.
1 change: 1 addition & 0 deletions 01-junit-5/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dependencies {

// Lagacy support for JUnit 4
testCompile "org.junit.vintage:junit-vintage-engine:$junit5_vintage4_version"
testCompile "junit:junit:$junit4_version"


// Commons
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void expectThrowsTest() {
ArrayIndexOutOfBoundsException.class,
() -> (new String[1])[2] = "I will throw an Exception :)");

assertEquals(exc.getMessage(), "2");
assertEquals(exc.getMessage(), "Index 2 out of bounds for length 1");
}

private int i = 10;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package com.dmitrijdrandarov.junit5;

import com.dmitrijdrandarov.entities.DummyFruit;
import com.dmitrijdrandarov.entities.DummyFruit.TYPE;
import com.dmitrijdrandarov.junit5.utils.parameterresolver.ClassName_ParameterResolver;
import com.dmitrijdrandarov.junit5.utils.parameterresolver.ParameterIndex_ParameterResolver;
import com.dmitrijdrandarov.junit5.utils.simpleextension.DisabledOnMonday;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.*;
import org.junit.jupiter.api.function.ThrowingConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Iterator;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
Expand All @@ -25,6 +24,16 @@
*/
class JUnit5_03_NewFeaturesAdvanced {

private Map<Integer, DummyFruit> dummyFruits;

@BeforeEach
void dummy() {
dummyFruits = new HashMap<>();
dummyFruits.put(1, new DummyFruit(1L, TYPE.BANANA, "Baby Banana", "It's yellow!", 20.0));
dummyFruits.put(2, new DummyFruit(2L, TYPE.APPLE, "Granny Smith Apple", "Delicious!", 10.5));
dummyFruits.put(3, new DummyFruit(3L, TYPE.ORANGE, "Grapefruit", "It's totally an orange, baka!", 8.5));
}

private static final Logger LOG = LoggerFactory.getLogger(JUnit5_03_NewFeaturesAdvanced.class);


Expand Down Expand Up @@ -69,6 +78,24 @@ Stream<DynamicTest> testStreamFactoryTest() {
Assertions::assertNotNull); // Providing an Executable on which the test is based
}

/**
* You can also pass table data
*
* @return A stream of dynamic tests with table data
*/
@TestFactory
Stream<DynamicTest> testStreamFactoryTableDataTest() {
List<String[]> testData = Arrays.stream(new Object[][]{
{"Col 1, Row 1", "Col 2, Row 1", dummyFruits.get(1).getType().toString()},
{"Col 1, Row 2", "Col 2, Row 2", dummyFruits.get(2).getType().toString()}
})
.map(objects -> Arrays.stream(objects).map(Object::toString).toArray(String[]::new))
.collect(Collectors.toList());

return testData.stream()
.map(strings -> DynamicTest.dynamicTest(testData.toString(), () -> Assertions.assertNotNull(testData)));
}


//------------------------------------------------------------------------------------------------------------------------
// Test-Extensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.dmitrijdrandarov.junit5.utils.simpleextension.DisabledWeekdays;
import com.dmitrijdrandarov.junit5.utils.testannotationextension.UITest;
import javafx.scene.layout.Pane;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
Expand Down Expand Up @@ -51,6 +52,7 @@ void disabledOnWeekdaysTest() {}
*#
* This of course could be also possible for the examples above.
*/
@Disabled("Incompatible with JDK 12")
@UITest("/fxml/sample.fxml")
void userInterfaceTest(Pane root) {
LOG.info(String.valueOf(root.getPrefWidth())); // 555.0 (defined in FXML-File)
Expand Down
9 changes: 0 additions & 9 deletions 01-junit-5/src/test/java/module-info.java

This file was deleted.

1 change: 1 addition & 0 deletions 03-spring/03-01-spring-boot-4/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dependencies {

// Lagacy support for JUnit 4
testCompile "org.junit.vintage:junit-vintage-engine:$junit5_vintage4_version"
testCompile "junit:junit:$junit4_version"

// Spring Boot 4
compile "org.springframework.boot:spring-boot-starter-data-jpa:$spring4_boot_version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.dmitrijdrandarov.entities.DummyFruit;
import com.dmitrijdrandarov.repositories.DummyFruitRepository;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
Expand All @@ -20,6 +21,7 @@
@RunWith(SpringRunner.class)
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.HSQL)
@Transactional
@Ignore("Incompatible with JDK 12")
public class SpringBoot4_01_JUnit4Test {

private static final Logger LOG = LoggerFactory.getLogger(SpringBoot4_01_JUnit4Test.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.dmitrijdrandarov.entities.DummyFruit;
import com.dmitrijdrandarov.repositories.DummyFruitRepository;
import org.junit.Assert;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
Expand All @@ -20,6 +21,7 @@
@ExtendWith(SpringExtension.class)
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.HSQL)
@Transactional
@Disabled("Incompatible with JDK 12")
class SpringBoot4_02_JUnit5Test {

private static final Logger LOG = LoggerFactory.getLogger(SpringBoot4_02_JUnit5Test.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.dmitrijdrandarov.entities.DummyFruit;
import com.dmitrijdrandarov.repositories.DummyFruitRepository;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
Expand All @@ -20,6 +21,7 @@
@RunWith(SpringRunner.class)
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.HSQL)
@Transactional
@Ignore
public class SpringBoot5_01_JUnit4Test {

private static final Logger LOG = LoggerFactory.getLogger(SpringBoot5_01_JUnit4Test.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.dmitrijdrandarov.entities.DummyFruit;
import com.dmitrijdrandarov.repositories.DummyFruitRepository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
Expand All @@ -20,6 +21,7 @@
@ExtendWith(SpringExtension.class)
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.HSQL)
@Transactional
@Disabled("Right now incompatible with JDK 12")
class SpringBoot5_02_JUnit5Test {

private static final Logger LOG = LoggerFactory.getLogger(SpringBoot5_02_JUnit5Test.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.dmitrijdrandarov.entities.DummyFruit;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

Expand All @@ -11,6 +12,7 @@
class Mockito_Basics {

@Test
@Disabled("Incompatible with JDK 12")
void basic() {
DummyFruit redBanana = Mockito.mock(DummyFruit.class);
redBanana.setName("Red Banana");
Expand Down
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ext {
junit5_version = '5.4.0'
junit5_vintage4_version = '5.4.0'
junit5_spring4_test_version = '1.0.0'
junit4_version = '4.12'

// Spring 5
spring5_version = '5.1.1.RELEASE'
Expand Down Expand Up @@ -77,8 +78,8 @@ configure(subprojects - project(":03-spring")) {
useJUnitPlatform()
}

sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
sourceCompatibility = JavaVersion.VERSION_12
targetCompatibility = JavaVersion.VERSION_12

dependencies {
testRuntime "org.junit.jupiter:junit-jupiter-engine:$junit5_version"
Expand All @@ -94,5 +95,5 @@ configure(subprojects - project(":03-spring")) {

wrapper {
distributionType = Wrapper.DistributionType.ALL
gradleVersion "5.2.1"
gradleVersion "5.6.3"
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.3-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

0 comments on commit 46de2e8

Please sign in to comment.