├── .gitignore ├── README.md ├── junit5-assertions-examples ├── .gitignore ├── README.md ├── build.gradle ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── howtoprogram │ │ └── junit5 │ │ └── StringUtils.java │ └── test │ └── java │ └── com │ └── howtoprogram │ └── junit5 │ ├── StringUtilsTestUnit5.java │ └── StringUtilsTestUnit5Disabled.java ├── junit5-assumptions-example ├── .gitignore ├── README.md ├── build.gradle ├── pom.xml └── src │ ├── main │ └── java │ │ └── xyz │ │ └── howtoprogram │ │ └── junit5 │ │ └── assumptions │ │ └── ScheduleService.java │ └── test │ └── java │ └── xyz │ └── howtoprogram │ └── junit5 │ └── assumptions │ └── ScheduleServiceTest.java ├── junit5-disable-test-example ├── .gitignore ├── README.md ├── build.gradle ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── howtoprogram │ │ └── junit5 │ │ └── StringUtils.java │ └── test │ └── java │ └── com │ └── howtoprogram │ └── junit5 │ ├── StringUtilsTestUnit5.java │ └── StringUtilsTestUnit5Disabled.java ├── junit5-dynamic-test-example ├── .gitignore ├── README.md ├── build.gradle ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── howtoprogram │ │ └── junit5 │ │ └── TranslatorEngine.java │ └── test │ └── java │ └── com │ └── howtoprogram │ └── junit5 │ ├── TranslationEngineDynamicTest.java │ └── TranslationEngineTest.java ├── junit5-exception-testing-example ├── .gitignore ├── README.md ├── build.gradle ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── howtoprogram │ │ └── junit5 │ │ └── StringUtils.java │ └── test │ └── java │ └── com │ └── howtoprogram │ └── junit5 │ └── StringUtilsTestUnit5Exception.java ├── junit5-gradle-example ├── README.md ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ ├── main │ └── java │ │ └── com │ │ └── howtoprogram │ │ └── junit5 │ │ └── BasicSalaryCalculator.java │ └── test │ └── java │ └── com │ └── howtoprogram │ └── junit5 │ └── BasicSalaryCalculatorTest.java ├── junit5-maven-example ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── howtoprogram │ │ └── junit5 │ │ └── BasicSalaryCalculator.java │ └── test │ └── java │ └── com │ └── howtoprogram │ └── junit5 │ └── BasicSalaryCalculatorTest.java ├── junit5-maven-jacoco-example ├── .gitignore ├── .project ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── howtoprogram │ │ └── junit5 │ │ └── BasicSalaryCalculator.java │ └── test │ └── java │ └── com │ └── howtoprogram │ └── junit5 │ └── BasicSalaryCalculatorTest.java ├── junit5-mockito-example ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── howtoprogram │ │ └── junit5 │ │ ├── UserManager.java │ │ └── UserService.java │ └── test │ └── java │ └── com │ └── howtoprogram │ └── junit5 │ └── UserServiceTest.java ├── junit5-nested-test-example ├── .gitignore ├── .project ├── README.md ├── build.gradle ├── pom.xml └── src │ ├── main │ └── java │ │ └── xyz │ │ └── howtoprogram │ │ └── junit5 │ │ └── nested │ │ └── UserService.java │ └── test │ └── java │ └── xyz │ └── howtoprogram │ └── junit5 │ └── nested │ └── TestUserService.java ├── junit5-parameter-resolution-example ├── .gitignore ├── README.md ├── build.gradle ├── gradle.properties ├── pom.xml └── src │ └── test │ └── java │ └── xyz │ └── howtoprogram │ └── junit5 │ └── order │ ├── AuditService.java │ ├── AuditServiceParameterResolver.java │ ├── EnvironmentInfo.java │ ├── EnvironmentNameParameterResolver.java │ ├── Report.java │ ├── ReportAnnotationParameterResolver.java │ ├── ReportService.java │ └── TestOrderService.java ├── junit5-spring-boot-example ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── README.md ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── howtoprogram │ │ └── unit5 │ │ ├── Application.java │ │ └── HelloController.java │ └── test │ └── java │ └── com │ └── howtoprogram │ └── unit5 │ └── ApplicationTests.java ├── junit5-tag-filter-example ├── .gradle │ └── 3.0 │ │ └── taskArtifacts │ │ ├── cache.properties │ │ ├── cache.properties.lock │ │ ├── fileHashes.bin │ │ ├── fileSnapshots.bin │ │ ├── fileSnapshotsToTreeSnapshotsIndex.bin │ │ └── taskArtifacts.bin ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── pom.xml └── src │ └── test │ └── java │ └── com │ └── howtoprogram │ └── junit5 │ └── OrderServiceTest.java ├── junit5-test-suite-example ├── .gitignore ├── README.md ├── build.gradle ├── pom.xml └── src │ └── test │ └── java │ └── xyz │ └── howtoprogram │ └── junit5 │ ├── order │ └── TestOrderService.java │ ├── payment │ └── TestPaymentService.java │ ├── suite │ ├── PlayOrderFeatureSuite.java │ └── UserFeatureSuite.java │ └── user │ ├── TestPasswordService.java │ └── TestUserService.java └── junit5-timeout-example ├── README.md ├── build.gradle ├── pom.xml └── src ├── main └── java │ └── xyz │ └── howtoprogram │ └── junit5 │ └── timeout │ └── OrderService.java └── test └── java └── xyz └── howtoprogram └── junit5 └── timeout └── TestOrderService.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Eclipse 2 | .project 3 | .classpath 4 | .settings/ 5 | 6 | # Idea 7 | .idea/ 8 | *.iml 9 | *.iws 10 | *.ipr 11 | 12 | # OS 13 | Thumbs.db 14 | .DS_Store 15 | 16 | # Maven 17 | target/ 18 | 19 | # Build 20 | out/ 21 | build/ 22 | bin/ 23 | 24 | 25 | # Others 26 | *.log 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #JUnit 5 Examples 2 | 3 | Contains all JUnit 5 Examples and Tutorial 4 | 5 | Go to each sub project. 6 | 7 | ## 1. Import source code into Eclipse 8 | ### Maven 9 | 10 | Menu **File –> Import –> Maven –> Existing Maven Projects** 11 | 12 | Browse to your source code location 13 | 14 | Click **Finish** button to finish the importing 15 | 16 | ### Gradle 17 | Menu **File –> Import –> Gradle Project –> Select Project --> Next until finish** 18 | ## 2. Sub Modules 19 | [JUnit 5 Assertions Example](http://howtoprogram.xyz/2016/08/12/junit-5-assertions-example/) 20 | ### junit5-assertions-examples 21 | 22 | [JUnit 5 Disable or Ignore A Test](http://howtoprogram.xyz/2016/08/14/junit-5-disable-ignore-tests/) 23 | ### junit5-disable-test-example 24 | 25 | [JUnit 5 Exception Testing](http://howtoprogram.xyz/2016/08/15/junit-5-exception-testing/) 26 | ### junit5-exception-testing-example 27 | 28 | [JUnit 5 Test Suite ](http://howtoprogram.xyz/2016/08/16/junit-5-test-suite/) 29 | ### junit-5-test-suite-example 30 | 31 | [JUnit 5 Assumptions With Assume](http://howtoprogram.xyz/2016/08/17/junit-5-assumptions-assume/) 32 | ### junit-5-assumptions 33 | 34 | [JUnit 5 Nested Tests Examples](http://howtoprogram.xyz/2016/08/19/junit-5-nested-tests-examples/) 35 | ### junit5-nested-test-example 36 | 37 | [JUnit 5 Dynamic Tests - Generate Tests at Run-time](http://howtoprogram.xyz/2016/08/21/junit-5-dynamic-tests/) 38 | ### junit5-dynamic-test-example 39 | 40 | [JUnit 5 Maven Example](http://howtoprogram.xyz/2016/09/09/junit-5-maven-example/) 41 | ### junit5-maven-example 42 | 43 | ## 3. Related Posts 44 | ## [JUnit 5 Tutorial](http://howtoprogram.xyz/java-technologies/junit-5-tutorial/) 45 | ## [JUnit 5 vs JUnit 4](http://howtoprogram.xyz/2016/08/10/junit-5-vs-junit-4/) 46 | ## [JUnit 5 Basic Introduction](http://howtoprogram.xyz/2016/08/07/junit-5-basic-introduction/) 47 | ## [JUnit 5 and Spring Boot Example](https://howtoprogram.xyz/2017/09/12/junit-5-spring-boot-example/) 48 | -------------------------------------------------------------------------------- /junit5-assertions-examples/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | /target/ 3 | /.classpath/ 4 | /.project/ 5 | /.settings/ -------------------------------------------------------------------------------- /junit5-assertions-examples/README.md: -------------------------------------------------------------------------------- 1 | #JUnit 5 Assertions Example 2 | 3 | 4 | ## 1. Import source code into Eclipse 5 | ### Maven 6 | 7 | Menu **File –> Import –> Maven –> Existing Maven Projects** 8 | 9 | Browse to your source code location 10 | 11 | Click **Finish** button to finish the importing 12 | 13 | ### Gradle 14 | Menu **File –> Import –> Gradle Project –> Select Project --> Next until finish** 15 | 16 | ## 3. Run the example 17 | 18 | 19 | Open the **StringUtilsTestUnit5.java** 20 | 21 | **Right click -> Run As -> JUnit Test** or use the shortcut: **Alt+Shift+x, T** to start the main method 22 | 23 | All the source code are described in: [JUnit 5 Assertions Example](http://howtoprogram.xyz/2016/08/12/junit-5-assertions-example/) 24 | 25 | ## 4. Related Posts 26 | ## [JUnit 5 vs JUnit 4](http://howtoprogram.xyz/2016/08/10/junit-5-vs-junit-4/) 27 | ## [JUnit 5 Basic Introduction](http://howtoprogram.xyz/2016/08/07/junit-5-basic-introduction/) 28 | ## [JUnit 5 Disable or Ignore A Test](http://howtoprogram.xyz/2016/08/14/junit-5-disable-ignore-tests/) 29 | ## [JUnit 5 Test Suite ](http://howtoprogram.xyz/2016/08/16/junit-5-test-suite/) 30 | ## [JUnit 5 Exception Testing](http://howtoprogram.xyz/2016/08/15/junit-5-exception-testing/) 31 | ## [JUnit 5 Assumptions With Assume](http://howtoprogram.xyz/2016/08/17/junit-5-assumptions-assume/) 32 | ## [JUnit 5 Nested Tests Examples] (http://howtoprogram.xyz/2016/08/19/junit-5-nested-tests-examples/) 33 | ## [JUnit 5 Dynamic Tests - Generate Tests at Run-time](http://howtoprogram.xyz/2016/08/21/junit-5-dynamic-tests/) 34 | ## [JUnit 5 Maven Example](http://howtoprogram.xyz/2016/09/09/junit-5-maven-example/) -------------------------------------------------------------------------------- /junit5-assertions-examples/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | } 5 | dependencies { 6 | classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0' 7 | } 8 | } 9 | 10 | repositories { 11 | mavenCentral() 12 | } 13 | 14 | ext.junitVintageVersion = '4.12.0' 15 | ext.junitPlatformVersion = '1.0.0' 16 | ext.junitJupiterVersion = '5.0.0' 17 | 18 | apply plugin: 'java' 19 | apply plugin: 'eclipse' 20 | apply plugin: 'idea' 21 | apply plugin: 'org.junit.platform.gradle.plugin' 22 | 23 | jar { 24 | baseName = 'junit5-assertions-example' 25 | version = '1.0.0-SNAPSHOT' 26 | } 27 | 28 | compileTestJava { 29 | sourceCompatibility = 1.8 30 | targetCompatibility = 1.8 31 | options.compilerArgs += '-parameters' 32 | } 33 | 34 | dependencies { 35 | // JUnit Jupiter API and TestEngine implementation 36 | testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}") 37 | testCompile("org.junit.platform:junit-platform-runner:${junitPlatformVersion}") 38 | testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}") 39 | 40 | } -------------------------------------------------------------------------------- /junit5-assertions-examples/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.example 7 | junit5-assertions-example 8 | 1.0-SNAPSHOT 9 | 10 | 11 | UTF-8 12 | 1.8 13 | ${maven.compiler.source} 14 | 15 | 5.3.0 16 | 17 | 18 | 19 | 20 | org.junit.jupiter 21 | junit-jupiter-api 22 | ${junit.jupiter.version} 23 | test 24 | 25 | 26 | org.junit.jupiter 27 | junit-jupiter-params 28 | ${junit.jupiter.version} 29 | test 30 | 31 | 32 | org.junit.jupiter 33 | junit-jupiter-engine 34 | ${junit.jupiter.version} 35 | test 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | maven-surefire-plugin 44 | 2.22.0 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /junit5-assertions-examples/src/main/java/com/howtoprogram/junit5/StringUtils.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.junit5; 2 | 3 | public final class StringUtils { 4 | 5 | 6 | public static Double convertToDouble(String str) { 7 | if (str == null) { 8 | return null; 9 | } 10 | return Double.valueOf(str); 11 | } 12 | 13 | public static boolean isNullOrBlank(String st) { 14 | return st == null || st.trim().length() == 0; 15 | } 16 | 17 | public static String getDefaultIfNull(final String st, final String defaultSt) { 18 | return st == null ? defaultSt : st; 19 | } 20 | 21 | public static String concat(String... sts) { 22 | String retVal = null; 23 | if (sts != null && sts.length > 0) { 24 | StringBuilder sb = new StringBuilder(); 25 | for (String st : sts) { 26 | if (st != null) { 27 | sb.append(st); 28 | } 29 | } 30 | retVal = sb.toString(); 31 | } 32 | return retVal; 33 | } 34 | 35 | } 36 | 37 | -------------------------------------------------------------------------------- /junit5-assertions-examples/src/test/java/com/howtoprogram/junit5/StringUtilsTestUnit5.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.junit5; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Disabled; 6 | import org.junit.jupiter.api.Test; 7 | 8 | public class StringUtilsTestUnit5 { 9 | 10 | @Test 11 | public void testConvertToDoubleOK() { 12 | // Test case with the age is a numeric string 13 | String age = "1990"; 14 | Double expAge = Double.valueOf(age); 15 | Double actual = StringUtils.convertToDouble(age); 16 | 17 | assertAll("Do many assertions.", () -> { 18 | assertNotNull(actual); 19 | assertEquals(expAge, actual); 20 | }); 21 | 22 | // Or Java 8 Lambdas style 23 | 24 | assertAll("Do many assertions.Java 8 Lambdas style", () -> { 25 | assertNotNull(actual, () -> "The actual is NULL"); 26 | assertEquals(expAge, actual, 27 | () -> "The expected is: " + expAge + " while the actual is:" + actual); 28 | }); 29 | 30 | } 31 | 32 | @Test 33 | public void testConvertToDoubleWithNullArgument() { 34 | // Test case with the age is null 35 | String age = null; 36 | Double actual = StringUtils.convertToDouble(age); 37 | assertNull(actual, "The actual is not null"); 38 | // Java 8 Style 39 | assertNull(actual, () -> "The actual is not null"); 40 | } 41 | 42 | 43 | 44 | @Test 45 | public void testConvertToDoubleThrowException() { 46 | // Test with the age is a non numeric string 47 | String age = "N/A"; 48 | assertThrows(NumberFormatException.class, () -> { 49 | StringUtils.convertToDouble(age); 50 | }); 51 | 52 | assertThrows(NumberFormatException.class, () -> { 53 | StringUtils.convertToDouble(age); 54 | }); 55 | } 56 | 57 | @Test 58 | public void testIsNullOrBlankOK() { 59 | // Test the case that the input is NULL 60 | String input = null; 61 | 62 | assertTrue(StringUtils.isNullOrBlank(input)); 63 | // Java 8 Lambdas Style 64 | assertTrue(StringUtils.isNullOrBlank(input), () -> "The string is not null or blank"); 65 | 66 | // Test case with the input is empty 67 | input = " "; 68 | assertTrue(StringUtils.isNullOrBlank(input)); 69 | 70 | // Test case with the input is not empty 71 | input = "abc"; 72 | 73 | assertFalse(StringUtils.isNullOrBlank(input)); 74 | 75 | } 76 | 77 | @Test 78 | public void testGetDefaultIfNull() { 79 | // Test case with input string is null 80 | String st = null; 81 | String defaultSt = "abc"; 82 | 83 | String actual = StringUtils.getDefaultIfNull(st, defaultSt); 84 | assertSame(defaultSt, actual); 85 | // Java 8 Lambdas Style 86 | assertSame(defaultSt, actual, () -> "Expected ouput is not same with actual"); 87 | 88 | // Test case with input string is not null 89 | st = "def"; 90 | 91 | actual = StringUtils.getDefaultIfNull(st, defaultSt); 92 | assertNotSame(defaultSt, actual); 93 | // Java 8 Lambdas Style 94 | assertNotSame(defaultSt, actual, () -> "Expected ouput is same with actual"); 95 | 96 | // Test case with input string is empty 97 | st = ""; 98 | actual = StringUtils.getDefaultIfNull(st, defaultSt); 99 | if (actual.equals(defaultSt)) { 100 | fail("The actual should be empty"); 101 | 102 | // Java 8 Lambdas Style 103 | fail(() -> "The actual should be empty"); 104 | } 105 | 106 | } 107 | 108 | @Test 109 | public void testConcatWithRegularInput() { 110 | String st1 = "Hello"; 111 | String st2 = "World"; 112 | String st3 = "!"; 113 | String expect = st1 + st2 + st3; 114 | String actual = StringUtils.concat(st1, st2, st3); 115 | assertEquals(expect, actual); 116 | } 117 | 118 | @Test 119 | public void testConcatWithNullInput() { 120 | String st1 = "Hello"; 121 | String st2 = "World"; 122 | String st3 = null; 123 | String expect = st1 + st2; 124 | String actual = StringUtils.concat(st1, st2, st3); 125 | assertEquals(expect, actual); 126 | } 127 | 128 | @Disabled 129 | @Test 130 | public void testConcatWithAllNullInput() { 131 | String actual = StringUtils.concat(); 132 | assertNull(actual); 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /junit5-assertions-examples/src/test/java/com/howtoprogram/junit5/StringUtilsTestUnit5Disabled.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.junit5; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | import static org.junit.jupiter.api.Assertions.assertNull; 5 | 6 | import org.junit.jupiter.api.Disabled; 7 | import org.junit.jupiter.api.Test; 8 | 9 | public class StringUtilsTestUnit5Disabled { 10 | 11 | @Test 12 | public void testConcatWithRegularInput() { 13 | String st1 = "Hello"; 14 | String st2 = "World"; 15 | String st3 = "!"; 16 | String expect = st1 + st2 + st3; 17 | String actual = StringUtils.concat(st1, st2, st3); 18 | assertEquals(expect, actual); 19 | } 20 | 21 | @Disabled 22 | @Test 23 | public void testConcatWithNullInput() { 24 | String st1 = "Hello"; 25 | String st2 = "World"; 26 | String st3 = null; 27 | String expect = st1 + st2; 28 | String actual = StringUtils.concat(st1, st2, st3); 29 | assertEquals(expect, actual); 30 | } 31 | 32 | @Test 33 | public void testConcatWithAllNullInput() { 34 | String actual = StringUtils.concat(); 35 | assertNull(actual); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /junit5-assumptions-example/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | /target/ 3 | /.classpath/ 4 | /.project/ 5 | /.settings/ -------------------------------------------------------------------------------- /junit5-assumptions-example/README.md: -------------------------------------------------------------------------------- 1 | #JUnit 5 Assumptions Example 2 | 3 | 4 | ## 1. Import source code into Eclipse 5 | ### Maven 6 | 7 | Menu **File –> Import –> Maven –> Existing Maven Projects** 8 | 9 | Browse to your source code location 10 | 11 | Click **Finish** button to finish the importing 12 | 13 | ### Gradle 14 | Menu **File –> Import –> Gradle Project –> Select Project --> Next until finish** 15 | 16 | ## 3. Run the example 17 | 18 | 19 | Open the **ScheduleServiceTest.java** 20 | 21 | **Right click -> Run As -> JUnit Test** or use the shortcut: **Alt+Shift+x, T** to start the main method 22 | 23 | All the source code are described in: [JUnit 5 Assumptions With Assume](http://howtoprogram.xyz/2016/08/17/junit-5-assumptions-assume/) 24 | 25 | ## 4. Related Posts 26 | ### [JUnit 5 Tutorial](http://howtoprogram.xyz/java-technologies/junit-5-tutorial/) 27 | ### [JUnit 5 vs JUnit 4](http://howtoprogram.xyz/2016/08/10/junit-5-vs-junit-4/) 28 | ### [JUnit 5 Basic Introduction](http://howtoprogram.xyz/2016/08/07/junit-5-basic-introduction/) 29 | ### [JUnit 5 Assertions Example](http://howtoprogram.xyz/2016/08/12/junit-5-assertions-example/) 30 | ### [JUnit 5 Exception Testing](http://howtoprogram.xyz/2016/08/15/junit-5-exception-testing/) 31 | ### [JUnit 5 Disable or Ignore A Test](http://howtoprogram.xyz/2016/08/14/junit-5-disable-ignore-tests/) 32 | ### [JUnit 5 Test Suite ](http://howtoprogram.xyz/2016/08/16/junit-5-test-suite/) 33 | ### [JUnit 5 Nested Tests Examples] (http://howtoprogram.xyz/2016/08/19/junit-5-nested-tests-examples/) 34 | ### [JUnit 5 Dynamic Tests - Generate Tests at Run-time](http://howtoprogram.xyz/2016/08/21/junit-5-dynamic-tests/) 35 | ### [JUnit 5 Maven Example](http://howtoprogram.xyz/2016/09/09/junit-5-maven-example/) -------------------------------------------------------------------------------- /junit5-assumptions-example/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | } 5 | dependencies { 6 | classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0' 7 | } 8 | } 9 | 10 | repositories { 11 | mavenCentral() 12 | } 13 | 14 | ext.junitVintageVersion = '4.12.0' 15 | ext.junitPlatformVersion = '1.0.0' 16 | ext.junitJupiterVersion = '5.0.0' 17 | 18 | apply plugin: 'java' 19 | apply plugin: 'eclipse' 20 | apply plugin: 'idea' 21 | apply plugin: 'org.junit.platform.gradle.plugin' 22 | 23 | jar { 24 | baseName = 'junit5-sssumptions-example' 25 | version = '1.0.0-SNAPSHOT' 26 | } 27 | 28 | compileTestJava { 29 | sourceCompatibility = 1.8 30 | targetCompatibility = 1.8 31 | options.compilerArgs += '-parameters' 32 | } 33 | 34 | dependencies { 35 | // JUnit Jupiter API and TestEngine implementation 36 | testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}") 37 | testCompile("org.junit.platform:junit-platform-runner:${junitPlatformVersion}") 38 | testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}") 39 | 40 | } -------------------------------------------------------------------------------- /junit5-assumptions-example/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.howtoprogram 5 | junit5-assumptions-example 6 | 0.0.1-SNAPSHOT 7 | jar 8 | 9 | junit5-assumptions-example 10 | http://howtoprogram.xyz 11 | 12 | 13 | UTF-8 14 | 1.8 15 | 4.12 16 | 5.1.1 17 | ${junit.version}.0 18 | 5.1.1 19 | 1.1.1 20 | 21 | 22 | 23 | 24 | 25 | maven-compiler-plugin 26 | 3.1 27 | 28 | ${java.version} 29 | ${java.version} 30 | 31 | 32 | 33 | maven-surefire-plugin 34 | 2.19 35 | 36 | 37 | org.junit.platform 38 | junit-platform-surefire-provider 39 | ${junit.platform.version} 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.junit.jupiter 51 | junit-jupiter-engine 52 | ${junit.jupiter.version} 53 | test 54 | 55 | 56 | 57 | junit 58 | junit 59 | ${junit.version} 60 | test 61 | 62 | 63 | org.junit.platform 64 | junit-platform-runner 65 | ${junit.platform.version} 66 | test 67 | 68 | 69 | org.junit.vintage 70 | junit-vintage-engine 71 | ${junit.vintage.version} 72 | test 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /junit5-assumptions-example/src/main/java/xyz/howtoprogram/junit5/assumptions/ScheduleService.java: -------------------------------------------------------------------------------- 1 | package xyz.howtoprogram.junit5.assumptions; 2 | 3 | public class ScheduleService { 4 | 5 | public boolean doSchedule() { 6 | //TODO: put logic here 7 | return true; 8 | } 9 | 10 | public boolean backupCalendar() { 11 | // TODO: put logic here 12 | return true; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /junit5-assumptions-example/src/test/java/xyz/howtoprogram/junit5/assumptions/ScheduleServiceTest.java: -------------------------------------------------------------------------------- 1 | package xyz.howtoprogram.junit5.assumptions; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertTrue; 4 | import static org.junit.jupiter.api.Assumptions.*; 5 | import java.util.Locale; 6 | import java.util.TimeZone; 7 | 8 | 9 | import org.junit.jupiter.api.Test; 10 | import org.junit.platform.runner.JUnitPlatform; 11 | import org.junit.runner.RunWith; 12 | 13 | @RunWith(JUnitPlatform.class) 14 | public class ScheduleServiceTest { 15 | 16 | 17 | @Test 18 | public void doScheduleSingleTimeZone() { 19 | 20 | TimeZone tzone = TimeZone.getDefault(); 21 | // Assume that the timezone is US/Mountain 22 | assumeTrue(tzone.getDisplayName().equals("US/Mountain")); 23 | 24 | // Test doSchedule method 25 | ScheduleService scheduleService = new ScheduleService(); 26 | assertTrue(scheduleService.doSchedule()); 27 | 28 | } 29 | 30 | @Test 31 | public void doScheduleLocaleNonUS() { 32 | 33 | // Assume that the current locale is US 34 | Locale currentLocale = Locale.getDefault(); 35 | assumeTrue(currentLocale.equals(Locale.US)); 36 | 37 | // Test doSchedule method 38 | ScheduleService scheduleService = new ScheduleService(); 39 | assertTrue(scheduleService.doSchedule()); 40 | 41 | } 42 | 43 | @Test 44 | public void backupCalendarWindows() { 45 | 46 | assumeTrue(System.getProperty("os.name").startsWith("Windows")); 47 | 48 | // Test doSchedule method 49 | ScheduleService scheduleService = new ScheduleService(); 50 | assertTrue(scheduleService.backupCalendar()); 51 | 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /junit5-disable-test-example/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | /target/ 3 | -------------------------------------------------------------------------------- /junit5-disable-test-example/README.md: -------------------------------------------------------------------------------- 1 | #JUnit 5 Disable Test Example 2 | 3 | 4 | ## 1. Import source code into Eclipse 5 | ### Maven 6 | 7 | Menu **File –> Import –> Maven –> Existing Maven Projects** 8 | 9 | Browse to your source code location 10 | 11 | Click **Finish** button to finish the importing 12 | 13 | ### Gradle 14 | Menu **File –> Import –> Gradle Project –> Select Project --> Next until finish** 15 | 16 | ## 3. Run the example 17 | 18 | 19 | Open the **StringUtilsTestUnit5Disabled.java** 20 | 21 | **Right click -> Run As -> JUnit Test** or use the shortcut: **Alt+Shift+x, T** to start the main method 22 | 23 | All the source code are described in: [JUnit 5 Disable or Ignore A Test](http://howtoprogram.xyz/2016/08/14/junit-5-disable-ignore-tests/) 24 | 25 | ## 4. Related Posts 26 | ### [JUnit 5 Tutorial](http://howtoprogram.xyz/java-technologies/junit-5-tutorial/) 27 | ### [JUnit 5 vs JUnit 4](http://howtoprogram.xyz/2016/08/10/junit-5-vs-junit-4/) 28 | ### [JUnit 5 Basic Introduction](http://howtoprogram.xyz/2016/08/07/junit-5-basic-introduction/) 29 | ### [JUnit 5 Assertions Example](http://howtoprogram.xyz/2016/08/12/junit-5-assertions-example/) 30 | ### [JUnit 5 Exception Testing](http://howtoprogram.xyz/2016/08/15/junit-5-exception-testing/) 31 | ### [JUnit 5 Test Suite ](http://howtoprogram.xyz/2016/08/16/junit-5-test-suite/) 32 | ### [JUnit 5 Assumptions With Assume](http://howtoprogram.xyz/2016/08/17/junit-5-assumptions-assume/) 33 | ### [JUnit 5 Nested Tests Examples] (http://howtoprogram.xyz/2016/08/19/junit-5-nested-tests-examples/) 34 | ### [JUnit 5 Dynamic Tests - Generate Tests at Run-time](http://howtoprogram.xyz/2016/08/21/junit-5-dynamic-tests/) 35 | ### [JUnit 5 Maven Example](http://howtoprogram.xyz/2016/09/09/junit-5-maven-example/) -------------------------------------------------------------------------------- /junit5-disable-test-example/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | } 5 | dependencies { 6 | classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0' 7 | } 8 | } 9 | 10 | repositories { 11 | mavenCentral() 12 | } 13 | 14 | ext.junitVintageVersion = '4.12.0' 15 | ext.junitPlatformVersion = '1.0.0' 16 | ext.junitJupiterVersion = '5.0.0' 17 | 18 | apply plugin: 'java' 19 | apply plugin: 'eclipse' 20 | apply plugin: 'idea' 21 | apply plugin: 'org.junit.platform.gradle.plugin' 22 | 23 | jar { 24 | baseName = 'junit5-disable-test-example' 25 | version = '1.0.0-SNAPSHOT' 26 | } 27 | 28 | compileTestJava { 29 | sourceCompatibility = 1.8 30 | targetCompatibility = 1.8 31 | options.compilerArgs += '-parameters' 32 | } 33 | 34 | dependencies { 35 | // JUnit Jupiter API and TestEngine implementation 36 | testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}") 37 | testCompile("org.junit.platform:junit-platform-runner:${junitPlatformVersion}") 38 | testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}") 39 | 40 | } -------------------------------------------------------------------------------- /junit5-disable-test-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.example 7 | junit5-disable-test-example 8 | 1.0-SNAPSHOT 9 | junit5-disable-test-example 10 | http://howtoprogram.xyz 11 | 12 | 13 | UTF-8 14 | 1.8 15 | ${maven.compiler.source} 16 | 17 | 5.3.0 18 | 19 | 20 | 21 | 22 | org.junit.jupiter 23 | junit-jupiter-api 24 | ${junit.jupiter.version} 25 | test 26 | 27 | 28 | org.junit.jupiter 29 | junit-jupiter-params 30 | ${junit.jupiter.version} 31 | test 32 | 33 | 34 | org.junit.jupiter 35 | junit-jupiter-engine 36 | ${junit.jupiter.version} 37 | test 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | maven-surefire-plugin 46 | 2.22.0 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /junit5-disable-test-example/src/main/java/com/howtoprogram/junit5/StringUtils.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.junit5; 2 | 3 | public final class StringUtils { 4 | 5 | 6 | public static Double convertToDouble(String str) { 7 | if (str == null) { 8 | return null; 9 | } 10 | return Double.valueOf(str); 11 | } 12 | 13 | public static boolean isNullOrBlank(String st) { 14 | return st == null || st.trim().length() == 0; 15 | } 16 | 17 | public static String getDefaultIfNull(final String st, final String defaultSt) { 18 | return st == null ? defaultSt : st; 19 | } 20 | 21 | public static String concat(String... sts) { 22 | String retVal = null; 23 | if (sts != null && sts.length > 0) { 24 | StringBuilder sb = new StringBuilder(); 25 | for (String st : sts) { 26 | if (st != null) { 27 | sb.append(st); 28 | } 29 | } 30 | retVal = sb.toString(); 31 | } 32 | return retVal; 33 | } 34 | 35 | } 36 | 37 | -------------------------------------------------------------------------------- /junit5-disable-test-example/src/test/java/com/howtoprogram/junit5/StringUtilsTestUnit5.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.junit5; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Disabled; 6 | import org.junit.jupiter.api.Test; 7 | 8 | @Disabled 9 | public class StringUtilsTestUnit5 { 10 | 11 | @Test 12 | public void testConvertToDoubleOK() { 13 | // Test case with the age is a numeric string 14 | String age = "1990"; 15 | Double expAge = Double.valueOf(age); 16 | Double actual = StringUtils.convertToDouble(age); 17 | 18 | assertAll("Do many assertions.", () -> { 19 | assertNotNull(actual); 20 | assertEquals(expAge, actual); 21 | }); 22 | 23 | // Or Java 8 Lambdas style 24 | 25 | assertAll("Do many assertions.Java 8 Lambdas style", () -> { 26 | assertNotNull(actual, () -> "The actual is NULL"); 27 | assertEquals(expAge, actual, 28 | () -> "The expected is: " + expAge + " while the actual is:" + actual); 29 | }); 30 | 31 | } 32 | 33 | @Test 34 | public void testConvertToDoubleWithNullArgument() { 35 | // Test case with the age is null 36 | String age = null; 37 | Double actual = StringUtils.convertToDouble(age); 38 | assertNull(actual, "The actual is not null"); 39 | // Java 8 Style 40 | assertNull(actual, () -> "The actual is not null"); 41 | } 42 | 43 | 44 | 45 | @Test 46 | public void testConvertToDoubleThrowException() { 47 | // Test with the age is a non numeric string 48 | String age = "N/A"; 49 | assertThrows(NumberFormatException.class, () -> { 50 | StringUtils.convertToDouble(age); 51 | }); 52 | 53 | assertThrows(NumberFormatException.class, () -> { 54 | StringUtils.convertToDouble(age); 55 | }); 56 | } 57 | 58 | @Test 59 | public void testIsNullOrBlankOK() { 60 | // Test the case that the input is NULL 61 | String input = null; 62 | 63 | assertTrue(StringUtils.isNullOrBlank(input)); 64 | // Java 8 Lambdas Style 65 | assertTrue(StringUtils.isNullOrBlank(input), () -> "The string is not null or blank"); 66 | 67 | // Test case with the input is empty 68 | input = " "; 69 | assertTrue(StringUtils.isNullOrBlank(input)); 70 | 71 | // Test case with the input is not empty 72 | input = "abc"; 73 | 74 | assertFalse(StringUtils.isNullOrBlank(input)); 75 | 76 | } 77 | 78 | @Test 79 | public void testGetDefaultIfNull() { 80 | // Test case with input string is null 81 | String st = null; 82 | String defaultSt = "abc"; 83 | 84 | String actual = StringUtils.getDefaultIfNull(st, defaultSt); 85 | assertSame(defaultSt, actual); 86 | // Java 8 Lambdas Style 87 | assertSame(defaultSt, actual, () -> "Expected ouput is not same with actual"); 88 | 89 | // Test case with input string is not null 90 | st = "def"; 91 | 92 | actual = StringUtils.getDefaultIfNull(st, defaultSt); 93 | assertNotSame(defaultSt, actual); 94 | // Java 8 Lambdas Style 95 | assertNotSame(defaultSt, actual, () -> "Expected ouput is same with actual"); 96 | 97 | // Test case with input string is empty 98 | st = ""; 99 | actual = StringUtils.getDefaultIfNull(st, defaultSt); 100 | if (actual.equals(defaultSt)) { 101 | fail("The actual should be empty"); 102 | 103 | // Java 8 Lambdas Style 104 | fail(() -> "The actual should be empty"); 105 | } 106 | 107 | } 108 | 109 | @Test 110 | public void testConcatWithRegularInput() { 111 | String st1 = "Hello"; 112 | String st2 = "World"; 113 | String st3 = "!"; 114 | String expect = st1 + st2 + st3; 115 | String actual = StringUtils.concat(st1, st2, st3); 116 | assertEquals(expect, actual); 117 | } 118 | 119 | @Test 120 | public void testConcatWithNullInput() { 121 | String st1 = "Hello"; 122 | String st2 = "World"; 123 | String st3 = null; 124 | String expect = st1 + st2; 125 | String actual = StringUtils.concat(st1, st2, st3); 126 | assertEquals(expect, actual); 127 | } 128 | 129 | @Disabled 130 | @Test 131 | public void testConcatWithAllNullInput() { 132 | String actual = StringUtils.concat(); 133 | assertNull(actual); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /junit5-disable-test-example/src/test/java/com/howtoprogram/junit5/StringUtilsTestUnit5Disabled.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.junit5; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | import static org.junit.jupiter.api.Assertions.assertNull; 5 | 6 | import org.junit.jupiter.api.Disabled; 7 | import org.junit.jupiter.api.Test; 8 | public class StringUtilsTestUnit5Disabled { 9 | 10 | @Test 11 | public void testConcatWithRegularInput() { 12 | String st1 = "Hello"; 13 | String st2 = "World"; 14 | String st3 = "!"; 15 | String expect = st1 + st2 + st3; 16 | String actual = StringUtils.concat(st1, st2, st3); 17 | assertEquals(expect, actual); 18 | } 19 | 20 | @Disabled 21 | @Test 22 | public void testConcatWithNullInput() { 23 | String st1 = "Hello"; 24 | String st2 = "World"; 25 | String st3 = null; 26 | String expect = st1 + st2; 27 | String actual = StringUtils.concat(st1, st2, st3); 28 | assertEquals(expect, actual); 29 | } 30 | 31 | @Test 32 | public void testConcatWithAllNullInput() { 33 | String actual = StringUtils.concat(); 34 | assertNull(actual); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /junit5-dynamic-test-example/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | /target/ 3 | -------------------------------------------------------------------------------- /junit5-dynamic-test-example/README.md: -------------------------------------------------------------------------------- 1 | #JUnit 5 Dynamic Tests Example 2 | 3 | 4 | ## 1. Import source code into Eclipse 5 | ### Maven 6 | 7 | Menu **File –> Import –> Maven –> Existing Maven Projects** 8 | 9 | Browse to your source code location 10 | 11 | Click **Finish** button to finish the importing 12 | 13 | ### Gradle 14 | Menu **File –> Import –> Gradle Project –> Select Project --> Next until finish** 15 | 16 | ## 3. Run the example 17 | 18 | 19 | Open the **TranslationEngineDynamicTest.java** 20 | 21 | **Right click -> Run As -> JUnit Test** or use the shortcut: **Alt+Shift+x, T** to start the main method 22 | 23 | All the source code are described in: [JUnit 5 Dynamic Tests - Generate Tests at Run-time](http://howtoprogram.xyz/2016/08/21/junit-5-dynamic-tests/) 24 | 25 | ## 4. Related Posts 26 | ### [JUnit 5 Tutorial](http://howtoprogram.xyz/java-technologies/junit-5-tutorial/) 27 | ### [JUnit 5 vs JUnit 4](http://howtoprogram.xyz/2016/08/10/junit-5-vs-junit-4/) 28 | ### [JUnit 5 Basic Introduction](http://howtoprogram.xyz/2016/08/07/junit-5-basic-introduction/) 29 | ### [JUnit 5 Assertions Example](http://howtoprogram.xyz/2016/08/12/junit-5-assertions-example/) 30 | ### [JUnit 5 Exception Testing](http://howtoprogram.xyz/2016/08/15/junit-5-exception-testing/) 31 | ### [JUnit 5 Disable or Ignore A Test](http://howtoprogram.xyz/2016/08/14/junit-5-disable-ignore-tests/) 32 | ### [JUnit 5 Assumptions With Assume](http://howtoprogram.xyz/2016/08/17/junit-5-assumptions-assume/) 33 | ### [JUnit 5 Test Suite ](http://howtoprogram.xyz/2016/08/16/junit-5-test-suite/) 34 | ### [JUnit 5 Nested Tests Examples] (http://howtoprogram.xyz/2016/08/19/junit-5-nested-tests-examples/) 35 | ### [JUnit 5 Maven Example](http://howtoprogram.xyz/2016/09/09/junit-5-maven-example/) 36 | -------------------------------------------------------------------------------- /junit5-dynamic-test-example/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | } 5 | dependencies { 6 | classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0' 7 | } 8 | } 9 | 10 | repositories { 11 | mavenCentral() 12 | } 13 | 14 | ext.junitVintageVersion = '4.12.0' 15 | ext.junitPlatformVersion = '1.0.0' 16 | ext.junitJupiterVersion = '5.0.0' 17 | 18 | apply plugin: 'java' 19 | apply plugin: 'eclipse' 20 | apply plugin: 'idea' 21 | apply plugin: 'org.junit.platform.gradle.plugin' 22 | 23 | jar { 24 | baseName = 'junit5-dynamic-test-example' 25 | version = '1.0.0-SNAPSHOT' 26 | } 27 | 28 | compileTestJava { 29 | sourceCompatibility = 1.8 30 | targetCompatibility = 1.8 31 | options.compilerArgs += '-parameters' 32 | } 33 | 34 | dependencies { 35 | // JUnit Jupiter API and TestEngine implementation 36 | testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}") 37 | testCompile("org.junit.platform:junit-platform-runner:${junitPlatformVersion}") 38 | testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}") 39 | 40 | } -------------------------------------------------------------------------------- /junit5-dynamic-test-example/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.howtoprogram 5 | junit5-dynamic-test-example 6 | 0.0.1-SNAPSHOT 7 | jar 8 | 9 | junit5-dynamic-test-example 10 | http://howtoprogram.xyz 11 | 12 | 13 | UTF-8 14 | 1.8 15 | 1.8 16 | 5.1.1 17 | 5.1.1 18 | 1.1.1 19 | 20 | 21 | 22 | 23 | 24 | maven-compiler-plugin 25 | 3.1 26 | 27 | 28 | maven-surefire-plugin 29 | 2.19 30 | 31 | 32 | org.junit.platform 33 | junit-platform-surefire-provider 34 | ${junit.platform.version} 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.junit.platform 44 | junit-platform-runner 45 | ${junit.platform.version} 46 | test 47 | 48 | 49 | org.junit.jupiter 50 | junit-jupiter-engine 51 | ${junit.jupiter.version} 52 | test 53 | 54 | 55 | org.junit.vintage 56 | junit-vintage-engine 57 | ${junit.vintage.version} 58 | test 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /junit5-dynamic-test-example/src/main/java/com/howtoprogram/junit5/TranslatorEngine.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.junit5; 2 | 3 | import org.junit.platform.commons.util.StringUtils; 4 | 5 | public class TranslatorEngine { 6 | 7 | public String tranlate(String text) { 8 | if (StringUtils.isBlank(text)) { 9 | throw new IllegalArgumentException("Translated text must not be empty."); 10 | } 11 | if ("Hello".equalsIgnoreCase(text)) { 12 | return "Bonjour"; 13 | 14 | } else if ("Yes".equalsIgnoreCase(text)) { 15 | return "Oui"; 16 | 17 | } else if ("No".equalsIgnoreCase(text)) { 18 | return "Non"; 19 | 20 | } else if ("Goodbye".equalsIgnoreCase(text)) { 21 | return "Au revoir"; 22 | 23 | } else if ("Good night".equalsIgnoreCase(text)) { 24 | return "Bonne nuit"; 25 | 26 | } else if ("Thank you".equalsIgnoreCase(text)) { 27 | return "Merci"; 28 | } else { 29 | return "Not found"; 30 | } 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /junit5-dynamic-test-example/src/test/java/com/howtoprogram/junit5/TranslationEngineDynamicTest.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.junit5; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import java.util.ArrayList; 6 | import java.util.Arrays; 7 | import java.util.Collection; 8 | import java.util.Iterator; 9 | import java.util.List; 10 | import java.util.stream.Collectors; 11 | import java.util.stream.Stream; 12 | 13 | import org.junit.jupiter.api.BeforeEach; 14 | import org.junit.jupiter.api.Disabled; 15 | import org.junit.jupiter.api.DynamicTest; 16 | import org.junit.jupiter.api.TestFactory; 17 | import org.junit.jupiter.api.function.Executable; 18 | 19 | public class TranslationEngineDynamicTest { 20 | 21 | private TranslatorEngine translatorEngine; 22 | 23 | @BeforeEach 24 | public void setUp() { 25 | translatorEngine = new TranslatorEngine(); 26 | } 27 | 28 | public void translateDynamicTestsRaw() { 29 | 30 | List inPhrases = new ArrayList<>(Arrays.asList("Hello", "Yes", "No")); 31 | List outPhrases = new ArrayList<>(Arrays.asList("Bonjour", "Oui", "Non")); 32 | Collection dynamicTests = new ArrayList<>(); 33 | 34 | for (int i = 0; i < inPhrases.size(); i++) { 35 | 36 | String phr = inPhrases.get(i); 37 | String outPhr = outPhrases.get(i); 38 | 39 | // create an test execution 40 | Executable exec = () -> assertEquals(outPhr, translatorEngine.tranlate(phr)); 41 | 42 | // create a test display name 43 | String testName = "Test tranlate " + phr; 44 | // create dynamic test 45 | DynamicTest dTest = DynamicTest.dynamicTest(testName, exec); 46 | 47 | // add the dynamic test to collection 48 | dynamicTests.add(dTest); 49 | } 50 | } 51 | 52 | @Disabled 53 | // @TestFactory 54 | public Collection translateDynamicTests() { 55 | 56 | List inPhrases = 57 | new ArrayList<>(Arrays.asList("Hello", "Yes", "No", "Goodbye", "Good night", "Thank you")); 58 | List outPhrases = 59 | new ArrayList<>(Arrays.asList("Bonjour", "Oui", "Non", "Au revoir", "Bonne nuit", "Merci")); 60 | 61 | Collection dynamicTests = new ArrayList<>(); 62 | 63 | for (int i = 0; i < inPhrases.size(); i++) { 64 | 65 | String phr = inPhrases.get(i); 66 | String outPhr = outPhrases.get(i); 67 | 68 | // create an test execution 69 | Executable exec = () -> assertEquals(outPhr, translatorEngine.tranlate(phr)); 70 | 71 | // create a test display name 72 | String testName = "Test tranlate " + phr; 73 | // create dynamic test 74 | DynamicTest dTest = DynamicTest.dynamicTest(testName, exec); 75 | 76 | // add the dynamic test to collection 77 | dynamicTests.add(dTest); 78 | } 79 | return dynamicTests; 80 | } 81 | 82 | // @Disabled 83 | @TestFactory 84 | public Stream translateDynamicTestsFromStream() { 85 | 86 | List inPhrases = 87 | new ArrayList<>(Arrays.asList("Hello", "Yes", "No", "Goodbye", "Good night", "Thank you")); 88 | List outPhrases = 89 | new ArrayList<>(Arrays.asList("Bonjour", "Oui", "Non", "Au revoir", "Bonne nuit", "Merci")); 90 | 91 | return inPhrases.stream().map(phrs -> DynamicTest.dynamicTest("Test translate " + phrs, () -> { 92 | int idx = inPhrases.indexOf(phrs); 93 | assertEquals(outPhrases.get(idx), translatorEngine.tranlate(phrs)); 94 | })); 95 | } 96 | 97 | @Disabled 98 | // @TestFactory 99 | public Iterator translateDynamicTestsFromIterator() { 100 | 101 | List inPhrases = 102 | new ArrayList<>(Arrays.asList("Hello", "Yes", "No", "Goodbye", "Good night", "Thank you")); 103 | List outPhrases = 104 | new ArrayList<>(Arrays.asList("Bonjour", "Oui", "Non", "Au revoir", "Bonne nuit", "Merci")); 105 | 106 | return inPhrases.stream().map(phrs -> DynamicTest.dynamicTest("Test translate " + phrs, () -> { 107 | int idx = inPhrases.indexOf(phrs); 108 | assertEquals(outPhrases.get(idx), translatorEngine.tranlate(phrs)); 109 | })).iterator(); 110 | } 111 | 112 | // @TestFactory 113 | public Iterable translateDynamicTestsFromIterate() { 114 | 115 | List inPhrases = 116 | new ArrayList<>(Arrays.asList("Hello", "Yes", "No", "Goodbye", "Good night", "Thank you")); 117 | List outPhrases = 118 | new ArrayList<>(Arrays.asList("Bonjour", "Oui", "Non", "Au revoir", "Bonne nuit", "Merci")); 119 | 120 | return inPhrases.stream().map(phrs -> DynamicTest.dynamicTest("Test translate " + phrs, () -> { 121 | int idx = inPhrases.indexOf(phrs); 122 | assertEquals(outPhrases.get(idx), translatorEngine.tranlate(phrs)); 123 | })).collect(Collectors.toList()); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /junit5-dynamic-test-example/src/test/java/com/howtoprogram/junit5/TranslationEngineTest.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.junit5; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import org.junit.jupiter.api.BeforeEach; 6 | import org.junit.jupiter.api.Test; 7 | import org.junit.platform.runner.JUnitPlatform; 8 | import org.junit.runner.RunWith; 9 | 10 | @RunWith(JUnitPlatform.class) 11 | public class TranslationEngineTest { 12 | 13 | private TranslatorEngine translatorEngine; 14 | 15 | @BeforeEach 16 | public void setUp() { 17 | translatorEngine = new TranslatorEngine(); 18 | } 19 | 20 | @Test 21 | public void testTranlsateHello() { 22 | assertEquals("Bonjour", translatorEngine.tranlate("Hello")); 23 | } 24 | 25 | @Test 26 | public void testTranlsateYes() { 27 | assertEquals("Oui", translatorEngine.tranlate("Yes")); 28 | } 29 | 30 | @Test 31 | public void testTranlsateNo() { 32 | assertEquals("Non", translatorEngine.tranlate("No")); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /junit5-exception-testing-example/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /junit5-exception-testing-example/README.md: -------------------------------------------------------------------------------- 1 | #JUnit 5 Exception Testing Example 2 | 3 | 4 | ## 1. Import source code into Eclipse 5 | ### Maven 6 | 7 | Menu **File –> Import –> Maven –> Existing Maven Projects** 8 | 9 | Browse to your source code location 10 | 11 | Click **Finish** button to finish the importing 12 | 13 | ### Gradle 14 | Menu **File –> Import –> Gradle Project –> Select Project --> Next until finish** 15 | 16 | ## 3. Run the example 17 | 18 | 19 | Open the **StringUtilsTestUnit5Exception.java** 20 | 21 | **Right click -> Run As -> JUnit Test** or use the shortcut: **Alt+Shift+x, T** to start the main method 22 | 23 | All the source code are described in: [JUnit 5 Exception Testing](http://howtoprogram.xyz/2016/08/15/junit-5-exception-testing/) 24 | 25 | ## 4. Related Posts 26 | ### [JUnit 5 Tutorial](http://howtoprogram.xyz/java-technologies/junit-5-tutorial/) 27 | ### [JUnit 5 vs JUnit 4](http://howtoprogram.xyz/2016/08/10/junit-5-vs-junit-4/) 28 | ### [JUnit 5 Basic Introduction](http://howtoprogram.xyz/2016/08/07/junit-5-basic-introduction/) 29 | ### [JUnit 5 Assertions Example](http://howtoprogram.xyz/2016/08/12/junit-5-assertions-example/) 30 | ### [JUnit 5 Disable or Ignore A Test](http://howtoprogram.xyz/2016/08/14/junit-5-disable-ignore-tests/) 31 | ### [JUnit 5 Test Suite ](http://howtoprogram.xyz/2016/08/16/junit-5-test-suite/) 32 | ### [JUnit 5 Assumptions With Assume](http://howtoprogram.xyz/2016/08/17/junit-5-assumptions-assume/) 33 | ### [JUnit 5 Nested Tests Examples] (http://howtoprogram.xyz/2016/08/19/junit-5-nested-tests-examples/) 34 | ### [JUnit 5 Dynamic Tests - Generate Tests at Run-time](http://howtoprogram.xyz/2016/08/21/junit-5-dynamic-tests/) 35 | ### [JUnit 5 Maven Example](http://howtoprogram.xyz/2016/09/09/junit-5-maven-example/) -------------------------------------------------------------------------------- /junit5-exception-testing-example/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | } 5 | dependencies { 6 | classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0' 7 | } 8 | } 9 | 10 | repositories { 11 | mavenCentral() 12 | } 13 | 14 | ext.junitVintageVersion = '4.12.0' 15 | ext.junitPlatformVersion = '1.0.0' 16 | ext.junitJupiterVersion = '5.0.0' 17 | 18 | apply plugin: 'java' 19 | apply plugin: 'eclipse' 20 | apply plugin: 'idea' 21 | apply plugin: 'org.junit.platform.gradle.plugin' 22 | 23 | jar { 24 | baseName = 'junit5-exception-testing-example' 25 | version = '1.0.0-SNAPSHOT' 26 | } 27 | 28 | compileTestJava { 29 | sourceCompatibility = 1.8 30 | targetCompatibility = 1.8 31 | options.compilerArgs += '-parameters' 32 | } 33 | 34 | dependencies { 35 | // JUnit Jupiter API and TestEngine implementation 36 | testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}") 37 | testCompile("org.junit.platform:junit-platform-runner:${junitPlatformVersion}") 38 | testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}") 39 | 40 | } -------------------------------------------------------------------------------- /junit5-exception-testing-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.example 7 | junit5-exception-testing-example 8 | 1.0-SNAPSHOT 9 | junit5-exception-testing-example 10 | http://howtoprogram.xyz 11 | 12 | 13 | UTF-8 14 | 1.8 15 | ${maven.compiler.source} 16 | 17 | 5.3.0 18 | 19 | 20 | 21 | 22 | org.junit.jupiter 23 | junit-jupiter-api 24 | ${junit.jupiter.version} 25 | test 26 | 27 | 28 | org.junit.jupiter 29 | junit-jupiter-params 30 | ${junit.jupiter.version} 31 | test 32 | 33 | 34 | org.junit.jupiter 35 | junit-jupiter-engine 36 | ${junit.jupiter.version} 37 | test 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | maven-surefire-plugin 46 | 2.22.0 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /junit5-exception-testing-example/src/main/java/com/howtoprogram/junit5/StringUtils.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.junit5; 2 | 3 | public final class StringUtils { 4 | 5 | public static Integer convertToInt(String str) { 6 | if (str == null || str.trim().length() == 0) { 7 | throw new IllegalArgumentException("String must be not null or empty"); 8 | } 9 | return Integer.valueOf(str); 10 | } 11 | } 12 | 13 | -------------------------------------------------------------------------------- /junit5-exception-testing-example/src/test/java/com/howtoprogram/junit5/StringUtilsTestUnit5Exception.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.junit5; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | import static org.junit.jupiter.api.Assertions.assertThrows; 5 | import static org.junit.jupiter.api.Assertions.fail; 6 | import org.junit.jupiter.api.Test; 7 | 8 | public class StringUtilsTestUnit5Exception { 9 | 10 | 11 | @Test 12 | public void convertToIntNullParameterAssertThrows() { 13 | String st = null; 14 | assertThrows(IllegalArgumentException.class, () -> { 15 | StringUtils.convertToInt(st); 16 | }); 17 | 18 | } 19 | 20 | @Test 21 | public void convertToIntNullParameterExpectThrows() { 22 | String st = null; 23 | Throwable exception = assertThrows(IllegalArgumentException.class, () -> { 24 | StringUtils.convertToInt(st); 25 | }); 26 | assertEquals("String must be not null or empty", exception.getMessage()); 27 | } 28 | 29 | @Test 30 | public void convertToIntNullParameterTryCatchIdiom() { 31 | String st = null; 32 | try { 33 | StringUtils.convertToInt(st); 34 | fail("Expected an IllegalArgumentException to be thrown"); 35 | } catch (IllegalArgumentException e) { 36 | assertEquals("String must be not null or empty", e.getMessage()); 37 | } 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /junit5-gradle-example/README.md: -------------------------------------------------------------------------------- 1 | #JUnit 5 Gradle Exampls 2 | 3 | ## 1. Import source code into Eclipse 4 | ### Maven 5 | 6 | Menu **File –> Import –> Maven –> Existing Maven Projects** 7 | 8 | Browse to your source code location 9 | 10 | Click **Finish** button to finish the importing 11 | 12 | ### Gradle 13 | Menu **File –> Import –> Gradle Project –> Select Project --> Next until finish** 14 | 15 | [JUnit 5 Gradle Example](https://howtoprogram.xyz/2016/09/25/junit-5-gradle-example/) -------------------------------------------------------------------------------- /junit5-gradle-example/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'eclipse' // optional (to generate Eclipse project files) 4 | id 'idea' // optional (to generate IntelliJ IDEA project files) 5 | } 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | 11 | dependencies { 12 | testCompile('org.junit.jupiter:junit-jupiter-api:5.3.0') 13 | testCompile('org.junit.jupiter:junit-jupiter-params:5.3.0') 14 | testRuntime('org.junit.jupiter:junit-jupiter-engine:5.3.0') 15 | } 16 | 17 | test { 18 | useJUnitPlatform() 19 | testLogging { 20 | events "passed", "skipped", "failed" 21 | } 22 | } 23 | 24 | wrapper { 25 | gradleVersion = '4.8' 26 | } -------------------------------------------------------------------------------- /junit5-gradle-example/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/howtoprogram/junit5-examples/2d8a9fb1ac0ea15d089b51415127b62715acdc34/junit5-gradle-example/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /junit5-gradle-example/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Aug 28 19:04:45 CEST 2016 2 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.0-bin.zip 3 | distributionBase=GRADLE_USER_HOME 4 | distributionPath=wrapper/dists 5 | zipStorePath=wrapper/dists 6 | zipStoreBase=GRADLE_USER_HOME 7 | -------------------------------------------------------------------------------- /junit5-gradle-example/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn ( ) { 37 | echo "$*" 38 | } 39 | 40 | die ( ) { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 165 | if [[ "$(uname)" == "Darwin" ]] && [[ "$HOME" == "$PWD" ]]; then 166 | cd "$(dirname "$0")" 167 | fi 168 | 169 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 170 | -------------------------------------------------------------------------------- /junit5-gradle-example/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /junit5-gradle-example/settings.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * This file was generated by the Gradle 'init' task. 3 | * 4 | * The settings file is used to specify which projects to include in your build. 5 | * 6 | * Detailed information about configuring a multi-project build in Gradle can be found 7 | * in the user guide at https://docs.gradle.org/4.10/userguide/multi_project_builds.html 8 | */ 9 | 10 | rootProject.name = 'junit5-gradle-example' 11 | -------------------------------------------------------------------------------- /junit5-gradle-example/src/main/java/com/howtoprogram/junit5/BasicSalaryCalculator.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.junit5; 2 | 3 | public class BasicSalaryCalculator { 4 | private double basicSalary; 5 | 6 | public double getBasicSalary() { 7 | return basicSalary; 8 | } 9 | 10 | public void setBasicSalary(double basicSalary) { 11 | if (basicSalary < 0) { 12 | throw new IllegalArgumentException("Negative salary is invalid."); 13 | } 14 | this.basicSalary = basicSalary; 15 | } 16 | 17 | public double getGrossSalary() { 18 | return this.basicSalary + getSocialInsurance() + getAdditionalBonus(); 19 | } 20 | 21 | public double getSocialInsurance() { 22 | return this.basicSalary * 25 / 100; 23 | } 24 | 25 | public double getAdditionalBonus() { 26 | return this.basicSalary / 10; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /junit5-gradle-example/src/test/java/com/howtoprogram/junit5/BasicSalaryCalculatorTest.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.junit5; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | import org.junit.jupiter.api.AfterEach; 5 | import org.junit.jupiter.api.BeforeEach; 6 | import org.junit.jupiter.api.DisplayName; 7 | import org.junit.jupiter.api.Test; 8 | 9 | public class BasicSalaryCalculatorTest { 10 | private BasicSalaryCalculator basicSalaryCalculator; 11 | 12 | @BeforeEach 13 | void init() { 14 | basicSalaryCalculator = new BasicSalaryCalculator(); 15 | } 16 | 17 | @Test 18 | void testBasicSalaryWithValidSalary() { 19 | double basicSalary = 4000; 20 | basicSalaryCalculator.setBasicSalary(basicSalary); 21 | 22 | double expectedSocialInsurance = basicSalary * 0.25; 23 | assertEquals(expectedSocialInsurance, basicSalaryCalculator.getSocialInsurance()); 24 | 25 | double expectedAddionalBonus = basicSalary * 0.1; 26 | assertEquals(expectedAddionalBonus, basicSalaryCalculator.getAdditionalBonus()); 27 | 28 | double expectedGross = basicSalary + expectedSocialInsurance + expectedAddionalBonus; 29 | assertEquals(expectedGross, basicSalaryCalculator.getGrossSalary()); 30 | 31 | } 32 | 33 | @DisplayName("Test BasicSalaryCalculator with invalid salary") 34 | @Test 35 | void testBasicSalaryWithInValidSalary() { 36 | 37 | double basicSalary = -100; 38 | assertThrows(IllegalArgumentException.class, () -> { 39 | basicSalaryCalculator.setBasicSalary(basicSalary); 40 | }); 41 | 42 | } 43 | 44 | @AfterEach 45 | void tearDown() { 46 | basicSalaryCalculator = null; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /junit5-maven-example/README.md: -------------------------------------------------------------------------------- 1 | #JUnit 5 Maven Example 2 | 3 | ## 1. Run the example 4 | 5 | Open terminal 6 | CD to the source code directory 7 | Type: mvn test 8 | 9 | All the source code are described in: [JUnit 5 Maven Example](http://howtoprogram.xyz/2016/09/09/junit-5-maven-example/) 10 | 11 | ## 4. Related Posts 12 | ## [JUnit 5 vs JUnit 4](http://howtoprogram.xyz/2016/08/10/junit-5-vs-junit-4/) 13 | ## [JUnit 5 Basic Introduction](http://howtoprogram.xyz/2016/08/07/junit-5-basic-introduction/) 14 | ## [JUnit 5 Disable or Ignore A Test](http://howtoprogram.xyz/2016/08/14/junit-5-disable-ignore-tests/) 15 | ## [JUnit 5 Test Suite ](http://howtoprogram.xyz/2016/08/16/junit-5-test-suite/) 16 | ## [JUnit 5 Exception Testing](http://howtoprogram.xyz/2016/08/15/junit-5-exception-testing/) 17 | ## [JUnit 5 Assumptions With Assume](http://howtoprogram.xyz/2016/08/17/junit-5-assumptions-assume/) 18 | ## [JUnit 5 Nested Tests Examples](http://howtoprogram.xyz/2016/08/19/junit-5-nested-tests-examples/) 19 | ## [JUnit 5 Dynamic Tests - Generate Tests at Run-time](http://howtoprogram.xyz/2016/08/21/junit-5-dynamic-tests/) -------------------------------------------------------------------------------- /junit5-maven-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.example 7 | junit5-maven-example 8 | 1.0-SNAPSHOT 9 | 10 | 11 | UTF-8 12 | 1.8 13 | ${maven.compiler.source} 14 | 15 | 5.3.0 16 | 17 | 18 | 19 | 20 | org.junit.jupiter 21 | junit-jupiter-api 22 | ${junit.jupiter.version} 23 | test 24 | 25 | 26 | org.junit.jupiter 27 | junit-jupiter-params 28 | ${junit.jupiter.version} 29 | test 30 | 31 | 32 | org.junit.jupiter 33 | junit-jupiter-engine 34 | ${junit.jupiter.version} 35 | test 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | maven-surefire-plugin 44 | 2.22.0 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /junit5-maven-example/src/main/java/com/howtoprogram/junit5/BasicSalaryCalculator.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.junit5; 2 | 3 | public class BasicSalaryCalculator { 4 | private double basicSalary; 5 | 6 | public double getBasicSalary() { 7 | return basicSalary; 8 | } 9 | 10 | public void setBasicSalary(double basicSalary) { 11 | if (basicSalary < 0) { 12 | throw new IllegalArgumentException("Negative salary is invalid."); 13 | } 14 | this.basicSalary = basicSalary; 15 | } 16 | 17 | public double getGrossSalary() { 18 | return this.basicSalary + getSocialInsurance() + getAdditionalBonus(); 19 | } 20 | 21 | public double getSocialInsurance() { 22 | return this.basicSalary * 25 / 100; 23 | } 24 | 25 | public double getAdditionalBonus() { 26 | return this.basicSalary / 10; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /junit5-maven-example/src/test/java/com/howtoprogram/junit5/BasicSalaryCalculatorTest.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.junit5; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | import static org.junit.jupiter.api.Assertions.assertThrows; 5 | 6 | import org.junit.jupiter.api.AfterEach; 7 | import org.junit.jupiter.api.BeforeEach; 8 | import org.junit.jupiter.api.DisplayName; 9 | import org.junit.jupiter.api.Test; 10 | 11 | //@RunWith(JUnitPlatform.class) 12 | public class BasicSalaryCalculatorTest { 13 | private BasicSalaryCalculator basicSalaryCalculator; 14 | 15 | @BeforeEach 16 | void init() { 17 | basicSalaryCalculator = new BasicSalaryCalculator(); 18 | } 19 | 20 | @Test 21 | void testBasicSalaryWithValidSalary() { 22 | double basicSalary = 4000; 23 | basicSalaryCalculator.setBasicSalary(basicSalary); 24 | 25 | double expectedSocialInsurance = basicSalary * 0.25; 26 | assertEquals(expectedSocialInsurance, 27 | basicSalaryCalculator.getSocialInsurance()); 28 | 29 | double expectedAddionalBonus = basicSalary * 0.1; 30 | assertEquals(expectedAddionalBonus, 31 | basicSalaryCalculator.getAdditionalBonus()); 32 | 33 | double expectedGross = basicSalary + expectedSocialInsurance 34 | + expectedAddionalBonus; 35 | assertEquals(expectedGross, basicSalaryCalculator.getGrossSalary()); 36 | 37 | } 38 | 39 | @DisplayName("Test BasicSalaryCalculator with invalid salary") 40 | @Test 41 | void testBasicSalaryWithInValidSalary() { 42 | 43 | double basicSalary = -100; 44 | assertThrows(IllegalArgumentException.class, () -> { 45 | basicSalaryCalculator.setBasicSalary(basicSalary); 46 | }); 47 | 48 | } 49 | 50 | @AfterEach 51 | void tearDown() { 52 | basicSalaryCalculator = null; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /junit5-maven-jacoco-example/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | /target/ 3 | /.classpath/ 4 | /.project/ 5 | /.settings/ -------------------------------------------------------------------------------- /junit5-maven-jacoco-example/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | junit5-maven-example 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /junit5-maven-jacoco-example/README.md: -------------------------------------------------------------------------------- 1 | #JUnit 5 JaCoCo SonarQue Maven Example 2 | 3 | ## 1. Run the example 4 | 5 | Open terminal 6 | CD to the source code directory 7 | Type: mvn test 8 | 9 | All the source code are described in: [JUnit 5 Maven Example](http://howtoprogram.xyz/2016/09/09/junit-5-maven-example/) 10 | 11 | ## 4. Related Posts 12 | ### [JUnit 5 vs JUnit 4](http://howtoprogram.xyz/2016/08/10/junit-5-vs-junit-4/) 13 | ### [JUnit 5 Basic Introduction](http://howtoprogram.xyz/2016/08/07/junit-5-basic-introduction/) 14 | ### [JUnit 5 Disable or Ignore A Test](http://howtoprogram.xyz/2016/08/14/junit-5-disable-ignore-tests/) 15 | ### [JUnit 5 Test Suite ](http://howtoprogram.xyz/2016/08/16/junit-5-test-suite/) 16 | ### [JUnit 5 Exception Testing](http://howtoprogram.xyz/2016/08/15/junit-5-exception-testing/) 17 | ### [JUnit 5 Assumptions With Assume](http://howtoprogram.xyz/2016/08/17/junit-5-assumptions-assume/) 18 | ### [JUnit 5 Nested Tests Examples](http://howtoprogram.xyz/2016/08/19/junit-5-nested-tests-examples/) 19 | ### [JUnit 5 Dynamic Tests - Generate Tests at Run-time](http://howtoprogram.xyz/2016/08/21/junit-5-dynamic-tests/) -------------------------------------------------------------------------------- /junit5-maven-jacoco-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.howtoprogram 7 | junit5-maven-jacoco-example 8 | 1.0-SNAPSHOT 9 | jar 10 | junit-maven-jacoco-example 11 | 12 | 13 | UTF-8 14 | 1.8 15 | 5.0.0 16 | 1.0.0 17 | http://172.18.15.192:9000/sonar 18 | com.mysql.jdbc.Driver 19 | 0.7.9 20 | 21 | 22 | 23 | 24 | 25 | maven-compiler-plugin 26 | 3.1 27 | 28 | ${java.version} 29 | ${java.version} 30 | 31 | 32 | 33 | maven-surefire-plugin 34 | 2.19 35 | 36 | 37 | org.junit.platform 38 | junit-platform-surefire-provider 39 | ${junit.platform.version} 40 | 41 | 42 | 43 | 44 | 45 | 46 | org.jacoco 47 | jacoco-maven-plugin 48 | ${jacoco.version} 49 | 50 | 51 | prepare-agent 52 | 53 | prepare-agent 54 | 55 | 56 | 57 | report 58 | prepare-package 59 | 60 | report 61 | 62 | 63 | 64 | post-unit-test 65 | test 66 | 67 | report 68 | 69 | 70 | 71 | 72 | target/jacoco.exec 73 | 74 | target/jacoco-ut 75 | 76 | 77 | 78 | 79 | 80 | target/jacoco.exec 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | org.junit.jupiter 90 | junit-jupiter-engine 91 | ${junit.jupiter.version} 92 | test 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /junit5-maven-jacoco-example/src/main/java/com/howtoprogram/junit5/BasicSalaryCalculator.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.junit5; 2 | 3 | public class BasicSalaryCalculator { 4 | private double basicSalary; 5 | 6 | public double getBasicSalary() { 7 | return basicSalary; 8 | } 9 | 10 | public void setBasicSalary(double basicSalary) { 11 | if (basicSalary < 0) { 12 | throw new IllegalArgumentException("Negative salary is invalid."); 13 | } 14 | this.basicSalary = basicSalary; 15 | } 16 | 17 | public double getGrossSalary() { 18 | return this.basicSalary + getSocialInsurance() + getAdditionalBonus(); 19 | } 20 | 21 | public double getSocialInsurance() { 22 | return this.basicSalary * 25 / 100; 23 | } 24 | 25 | public double getAdditionalBonus() { 26 | return this.basicSalary / 10; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /junit5-maven-jacoco-example/src/test/java/com/howtoprogram/junit5/BasicSalaryCalculatorTest.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.junit5; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | import static org.junit.jupiter.api.Assertions.assertThrows; 5 | 6 | import org.junit.jupiter.api.AfterEach; 7 | import org.junit.jupiter.api.BeforeEach; 8 | import org.junit.jupiter.api.Test; 9 | 10 | public class BasicSalaryCalculatorTest { 11 | private BasicSalaryCalculator salaryCalculator; 12 | 13 | @BeforeEach 14 | void init() { 15 | salaryCalculator = new BasicSalaryCalculator(); 16 | } 17 | 18 | @Test 19 | void calculateWithValidSalaryTest() { 20 | double basicSalary = 4000; 21 | salaryCalculator.setBasicSalary(basicSalary); 22 | 23 | double expBasicSalary = basicSalary * 0.25; 24 | assertEquals(expBasicSalary, salaryCalculator.getSocialInsurance()); 25 | 26 | double expAddBonus = basicSalary * 0.1; 27 | assertEquals(expAddBonus, salaryCalculator.getAdditionalBonus()); 28 | 29 | double expGross = basicSalary + expBasicSalary + expAddBonus; 30 | assertEquals(expGross, salaryCalculator.getGrossSalary()); 31 | 32 | } 33 | 34 | @Test 35 | void calculateWithInValidSalaryTest() { 36 | 37 | double basicSalary = -100; 38 | assertThrows(IllegalArgumentException.class, () -> { 39 | salaryCalculator.setBasicSalary(basicSalary); 40 | }); 41 | 42 | } 43 | 44 | @AfterEach 45 | void tearDown() { 46 | salaryCalculator = null; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /junit5-mockito-example/README.md: -------------------------------------------------------------------------------- 1 | # JUnit 5 Mockito Example 2 | 3 | ## 1. Run the example 4 | 5 | Open terminal 6 | CD to the source code directory 7 | Type: mvn test 8 | 9 | All the source code are described in: [JUnit 5 Mockitor Example](https://howtoprogram.xyz/java-technologies/junit-5-tutorial/) 10 | 11 | ### 4. Related Posts 12 | #### [JUnit 5 vs JUnit 4](http://howtoprogram.xyz/2016/08/10/junit-5-vs-junit-4/) 13 | #### [JUnit 5 Basic Introduction](http://howtoprogram.xyz/2016/08/07/junit-5-basic-introduction/) 14 | #### [JUnit 5 Disable or Ignore A Test](http://howtoprogram.xyz/2016/08/14/junit-5-disable-ignore-tests/) 15 | #### [JUnit 5 Test Suite ](http://howtoprogram.xyz/2016/08/16/junit-5-test-suite/) 16 | #### [JUnit 5 Exception Testing](http://howtoprogram.xyz/2016/08/15/junit-5-exception-testing/) 17 | #### [JUnit 5 Assumptions With Assume](http://howtoprogram.xyz/2016/08/17/junit-5-assumptions-assume/) 18 | #### [JUnit 5 Nested Tests Examples](http://howtoprogram.xyz/2016/08/19/junit-5-nested-tests-examples/) 19 | #### [JUnit 5 Dynamic Tests - Generate Tests at Run-time](http://howtoprogram.xyz/2016/08/21/junit-5-dynamic-tests/) -------------------------------------------------------------------------------- /junit5-mockito-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.example 8 | junit5-mockito-example 9 | 1.0-SNAPSHOT 10 | 11 | 12 | UTF-8 13 | 1.8 14 | 1.8 15 | 5.3.0 16 | 17 | 18 | 19 | 20 | org.junit.jupiter 21 | junit-jupiter-api 22 | ${junit.jupiter.version} 23 | test 24 | 25 | 26 | org.junit.jupiter 27 | junit-jupiter-params 28 | ${junit.jupiter.version} 29 | test 30 | 31 | 32 | org.junit.jupiter 33 | junit-jupiter-engine 34 | ${junit.jupiter.version} 35 | test 36 | 37 | 38 | org.mockito 39 | mockito-junit-jupiter 40 | 2.22.0 41 | test 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | maven-surefire-plugin 50 | 2.22.0 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /junit5-mockito-example/src/main/java/com/howtoprogram/junit5/UserManager.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.junit5; 2 | 3 | public class UserManager { 4 | 5 | public int getUserCount() { 6 | // TODO Auto-generated method stub 7 | return 0; 8 | } 9 | 10 | public void save(String name) { 11 | // TODO Auto-generated method stub 12 | 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /junit5-mockito-example/src/main/java/com/howtoprogram/junit5/UserService.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.junit5; 2 | public class UserService { 3 | 4 | private final UserManager userManager; 5 | 6 | public UserService(){ 7 | this(new UserManager()); 8 | } 9 | 10 | public UserService(UserManager userManager){ 11 | this.userManager = userManager; 12 | } 13 | 14 | public void save(String name){ 15 | userManager.save(name); 16 | } 17 | 18 | public int getuserCount(){ 19 | try{ 20 | return userManager.getUserCount(); 21 | } 22 | catch (Exception e){ 23 | return -1; 24 | } 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /junit5-mockito-example/src/test/java/com/howtoprogram/junit5/UserServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.junit5; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.junit.jupiter.api.extension.ExtendWith; 5 | import org.mockito.Mock; 6 | import org.mockito.Mockito; 7 | import org.mockito.junit.jupiter.MockitoExtension; 8 | import org.mockito.junit.jupiter.MockitoSettings; 9 | import org.mockito.quality.Strictness; 10 | 11 | @ExtendWith(MockitoExtension.class) 12 | @MockitoSettings(strictness = Strictness.WARN) 13 | public class UserServiceTest { 14 | 15 | @Mock 16 | UserManager userManager; 17 | 18 | @Test 19 | public void testSaveArgMatch() throws Exception { 20 | final String name = "Carbery"; 21 | UserService userService = new UserService(userManager); 22 | userService.save(name); 23 | Mockito.verify(userManager, Mockito.times(1)).save(Mockito.anyString()); 24 | Mockito.verify(userManager, Mockito.times(1)).save(Mockito.isA(String.class)); 25 | Mockito.verify(userManager, Mockito.times(1)).save(Mockito.startsWith("Ca")); 26 | Mockito.verify(userManager, Mockito.times(1)).save(Mockito.endsWith("ry")); 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /junit5-nested-test-example/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | /target/ 3 | /.classpath/ 4 | /.project/ 5 | /.settings/ -------------------------------------------------------------------------------- /junit5-nested-test-example/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | junit5-timeout-test-example 4 | Project junit5-timeout-test-example created by Buildship. 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.buildship.core.gradleprojectbuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.m2e.core.maven2Builder 20 | 21 | 22 | 23 | 24 | 25 | org.eclipse.m2e.core.maven2Nature 26 | org.eclipse.buildship.core.gradleprojectnature 27 | org.eclipse.jdt.core.javanature 28 | 29 | 30 | -------------------------------------------------------------------------------- /junit5-nested-test-example/README.md: -------------------------------------------------------------------------------- 1 | #JUnit 5 Nested Tests Example 2 | 3 | 4 | ## 1. Import source code into Eclipse 5 | ### Maven 6 | 7 | Menu **File –> Import –> Maven –> Existing Maven Projects** 8 | 9 | Browse to your source code location 10 | 11 | Click **Finish** button to finish the importing 12 | 13 | ### Gradle 14 | Menu **File –> Import –> Gradle Project –> Select Project --> Next until finish** 15 | 16 | ## 3. Run the example 17 | 18 | 19 | Open the **TestUserService.java** 20 | 21 | **Right click -> Run As -> JUnit Test** or use the shortcut: **Alt+Shift+x, T** to start the main method 22 | 23 | 24 | All the source code are described in: [JUnit 5 Nested Tests Examples] (http://howtoprogram.xyz/2016/08/19/junit-5-nested-tests-examples/) 25 | 26 | ## 4. Related Posts 27 | ### [JUnit 5 Tutorial](http://howtoprogram.xyz/java-technologies/junit-5-tutorial/) 28 | ### [JUnit 5 vs JUnit 4](http://howtoprogram.xyz/2016/08/10/junit-5-vs-junit-4/) 29 | ### [JUnit 5 Basic Introduction](http://howtoprogram.xyz/2016/08/07/junit-5-basic-introduction/) 30 | ### [JUnit 5 Assertions Example](http://howtoprogram.xyz/2016/08/12/junit-5-assertions-example/) 31 | ### [JUnit 5 Exception Testing](http://howtoprogram.xyz/2016/08/15/junit-5-exception-testing/) 32 | ### [JUnit 5 Disable or Ignore A Test](http://howtoprogram.xyz/2016/08/14/junit-5-disable-ignore-tests/) 33 | ### [JUnit 5 Assumptions With Assume](http://howtoprogram.xyz/2016/08/17/junit-5-assumptions-assume/) 34 | ### [JUnit 5 Test Suite ](http://howtoprogram.xyz/2016/08/16/junit-5-test-suite/) 35 | ### [JUnit 5 Dynamic Tests - Generate Tests at Run-time](http://howtoprogram.xyz/2016/08/21/junit-5-dynamic-tests/) 36 | ### [JUnit 5 Maven Example](http://howtoprogram.xyz/2016/09/09/junit-5-maven-example/) 37 | -------------------------------------------------------------------------------- /junit5-nested-test-example/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | } 5 | dependencies { 6 | classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M2' 7 | } 8 | } 9 | 10 | repositories { 11 | mavenCentral() 12 | } 13 | 14 | ext.junitVintageVersion = '4.12.0-M2' 15 | ext.junitPlatformVersion = '1.0.0-M2' 16 | ext.junitJupiterVersion = '5.0.0-M2' 17 | 18 | apply plugin: 'java' 19 | apply plugin: 'eclipse' 20 | apply plugin: 'idea' 21 | apply plugin: 'org.junit.platform.gradle.plugin' 22 | 23 | jar { 24 | baseName = 'junit5-nested-test-example' 25 | version = '1.0.0-SNAPSHOT' 26 | } 27 | 28 | compileTestJava { 29 | sourceCompatibility = 1.8 30 | targetCompatibility = 1.8 31 | options.compilerArgs += '-parameters' 32 | } 33 | 34 | dependencies { 35 | // JUnit Jupiter API and TestEngine implementation 36 | testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}") 37 | testCompile("org.junit.platform:junit-platform-runner:${junitPlatformVersion}") 38 | testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}") 39 | 40 | } -------------------------------------------------------------------------------- /junit5-nested-test-example/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.howtoprogram 5 | junit5-nested-test-example 6 | 0.0.1-SNAPSHOT 7 | jar 8 | 9 | junit5-nested-test-example 10 | http://howtoprogram.xyz 11 | 12 | 13 | UTF-8 14 | 1.8 15 | 1.8 16 | 5.1.1 17 | 5.1.1 18 | 1.1.1 19 | 20 | 21 | 22 | 23 | 24 | maven-compiler-plugin 25 | 3.1 26 | 27 | 28 | maven-surefire-plugin 29 | 2.19 30 | 31 | 32 | org.junit.platform 33 | junit-platform-surefire-provider 34 | ${junit.platform.version} 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.junit.platform 44 | junit-platform-runner 45 | ${junit.platform.version} 46 | test 47 | 48 | 49 | org.junit.jupiter 50 | junit-jupiter-engine 51 | ${junit.jupiter.version} 52 | test 53 | 54 | 55 | org.junit.vintage 56 | junit-vintage-engine 57 | ${junit.vintage.version} 58 | test 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /junit5-nested-test-example/src/main/java/xyz/howtoprogram/junit5/nested/UserService.java: -------------------------------------------------------------------------------- 1 | package xyz.howtoprogram.junit5.nested; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | import org.junit.platform.commons.util.StringUtils; 7 | 8 | public class UserService { 9 | 10 | public boolean login(String username, String password) { 11 | if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) { 12 | throw new IllegalArgumentException("Username and password must not be null or empty"); 13 | } else if (username.equals("admin") && password.equals("password123")) { 14 | return true; 15 | } 16 | return false; 17 | } 18 | 19 | public boolean changePassword(long userId, String oldPassword, String newPassword) { 20 | if (userId == 1 && StringUtils.isNotBlank(newPassword) && StringUtils.isNotBlank(newPassword) 21 | && !newPassword.equals(oldPassword)) { 22 | return true; 23 | } 24 | return false; 25 | } 26 | 27 | public boolean resetPassword(long userId) { 28 | List existingUsers = new ArrayList<>(Arrays.asList(1L, 2L, 3L)); 29 | if (existingUsers.contains(userId)) { 30 | return true; 31 | } 32 | return false; 33 | } 34 | 35 | public boolean logout(long userId) { 36 | List existingUsers = new ArrayList<>(Arrays.asList(1L, 2L, 3L)); 37 | if (existingUsers.contains(userId)) { 38 | // do whatever 39 | } 40 | return true; 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /junit5-nested-test-example/src/test/java/xyz/howtoprogram/junit5/nested/TestUserService.java: -------------------------------------------------------------------------------- 1 | package xyz.howtoprogram.junit5.nested; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertTrue; 4 | import static org.junit.jupiter.api.Assertions.assertFalse; 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | import static org.junit.jupiter.api.Assertions.assertThrows; 7 | import org.junit.jupiter.api.BeforeEach; 8 | import org.junit.jupiter.api.DisplayName; 9 | import org.junit.jupiter.api.Nested; 10 | import org.junit.jupiter.api.Test; 11 | import org.junit.platform.runner.JUnitPlatform; 12 | import org.junit.runner.RunWith; 13 | 14 | @RunWith(JUnitPlatform.class) 15 | public class TestUserService { 16 | private UserService userService = null; 17 | 18 | @BeforeEach 19 | public void init() { 20 | userService = new UserService(); 21 | } 22 | 23 | @Test 24 | public void logoutSuccess() { 25 | long userId = 1L; 26 | assertTrue(userService.logout(userId)); 27 | 28 | } 29 | 30 | @Test 31 | public void resetPasswordExistingUser() { 32 | long userId = 1l; 33 | assertTrue(userService.resetPassword(userId)); 34 | 35 | } 36 | 37 | @Test 38 | public void resetPasswordUserNotExist() { 39 | long userId = 5l; 40 | assertFalse(userService.resetPassword(userId)); 41 | 42 | } 43 | 44 | @Nested 45 | @DisplayName("Test Login Feature") 46 | class LoginFeature { 47 | 48 | @Test 49 | void loginUsernamePasswordAreCorrect() { 50 | boolean actual = userService.login("admin", "password123"); 51 | assertTrue(actual); 52 | } 53 | 54 | @Test 55 | void loginUsernamePasswordAreInCorrect() { 56 | boolean actual = userService.login("admin", "password123456"); 57 | assertFalse(actual); 58 | } 59 | 60 | @Test 61 | void loginUsernamePasswordAreNulls() { 62 | IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { 63 | userService.login(null, null); 64 | }); 65 | assertEquals("Username and password must not be null or empty", exception.getMessage()); 66 | 67 | } 68 | 69 | @Test 70 | void loginUsernamePasswordAreEmpty() { 71 | 72 | assertThrows(IllegalArgumentException.class, () -> { 73 | userService.login("", ""); 74 | }); 75 | 76 | } 77 | } 78 | @Nested 79 | @DisplayName("Test ChangePassword Feature") 80 | class ChangePasswordFeature { 81 | @Test 82 | void changePasswordUserExistOldPasswordNewPasswordCorrect() { 83 | long userId = 1L; // existed user 84 | assertTrue(userService.changePassword(userId, "password123", "password123456")); 85 | } 86 | 87 | @Test 88 | void changePasswordUserNotExist() { 89 | long userId = 999L; // not existed user 90 | assertFalse(userService.changePassword(userId, "password123", "password123456")); 91 | } 92 | 93 | @Test 94 | void changePasswordUserExistOldPasswordAndNewPasswordEmpty() { 95 | long userId = 1L; // existed user 96 | assertFalse(userService.changePassword(userId, "", "")); 97 | } 98 | 99 | @Test 100 | void changePasswordUserExistOldPasswordEqualNewPassword() { 101 | long userId = 1L; // existed user 102 | assertFalse(userService.changePassword(userId, "password123", "password123")); 103 | } 104 | } 105 | 106 | } 107 | -------------------------------------------------------------------------------- /junit5-parameter-resolution-example/.gitignore: -------------------------------------------------------------------------------- 1 | # Eclipse 2 | .project 3 | .classpath 4 | .settings/ 5 | 6 | # Idea 7 | .idea/ 8 | *.iml 9 | *.iws 10 | *.ipr 11 | 12 | # OS 13 | Thumbs.db 14 | .DS_Store 15 | 16 | # Maven 17 | target/ 18 | 19 | # Build 20 | out/ 21 | build/ 22 | bin/ 23 | 24 | 25 | # Others 26 | *.log 27 | -------------------------------------------------------------------------------- /junit5-parameter-resolution-example/README.md: -------------------------------------------------------------------------------- 1 | #junit5 parameter resolution example 2 | 3 | 4 | ## 1. Import source code into Eclipse 5 | ### Maven 6 | 7 | Menu **File –> Import –> Maven –> Existing Maven Projects** 8 | 9 | Browse to your source code location 10 | 11 | Click **Finish** button to finish the importing 12 | 13 | ### Gradle 14 | Menu **File –> Import –> Gradle Project –> Select Project --> Next until finish** 15 | 16 | ## 3. Run the example 17 | 18 | 19 | Open the **TestOrderService.java** 20 | 21 | **Right click -> Run As -> JUnit Test** or use the shortcut: **Alt+Shift+x, T** to start the main method 22 | 23 | All the source code are described in: [JUnit 5 Parameter Resolution Example](http://howtoprogram.xyz/2016/10/28/junit-5-parameter-resolution-example/) 24 | 25 | ## 4. Related Posts 26 | [JUnit 5 Tutorial](http://howtoprogram.xyz/java-technologies/junit-5-tutorial/) 27 | 28 | [JUnit 5 vs JUnit 4](http://howtoprogram.xyz/2016/08/10/junit-5-vs-junit-4/) 29 | 30 | [JUnit 5 Basic Introduction](http://howtoprogram.xyz/2016/08/07/junit-5-basic-introduction/) 31 | 32 | [JUnit 5 Assertions Example](http://howtoprogram.xyz/2016/08/12/junit-5-assertions-example/) 33 | 34 | [JUnit 5 Exception Testing](http://howtoprogram.xyz/2016/08/15/junit-5-exception-testing/) 35 | 36 | [JUnit 5 Disable or Ignore A Test](http://howtoprogram.xyz/2016/08/14/junit-5-disable-ignore-tests/) 37 | 38 | [JUnit 5 Assumptions With Assume](http://howtoprogram.xyz/2016/08/17/junit-5-assumptions-assume/) 39 | 40 | [JUnit 5 Nested Tests Examples](http://howtoprogram.xyz/2016/08/19/junit-5-nested-tests-examples/) 41 | 42 | [JUnit 5 Dynamic Tests - Generate Tests at Run-time](http://howtoprogram.xyz/2016/08/21/junit-5-dynamic-tests/) 43 | 44 | [JUnit 5 Maven Example](http://howtoprogram.xyz/2016/09/09/junit-5-maven-example/) -------------------------------------------------------------------------------- /junit5-parameter-resolution-example/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | } 5 | dependencies { 6 | classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0' 7 | } 8 | } 9 | 10 | repositories { 11 | mavenCentral() 12 | } 13 | 14 | ext.junit4Version = '4.12' 15 | ext.junitVintageVersion = '4.12.0' 16 | ext.junitPlatformVersion = '1.0.0' 17 | ext.junitJupiterVersion = '5.0.0' 18 | ext.log4jVersion = '2.6.2' 19 | 20 | apply plugin: 'java' 21 | apply plugin: 'eclipse' 22 | apply plugin: 'idea' 23 | apply plugin: 'org.junit.platform.gradle.plugin' 24 | 25 | jar { 26 | baseName = 'junit5-parameter-resolution-example' 27 | version = '1.0.0-SNAPSHOT' 28 | } 29 | 30 | compileTestJava { 31 | sourceCompatibility = 1.8 32 | targetCompatibility = 1.8 33 | options.compilerArgs += '-parameters' 34 | } 35 | 36 | junitPlatform { 37 | // platformVersion '1.0.0' 38 | filters { 39 | engines { 40 | // include 'junit-jupiter', 'junit-vintage' 41 | // exclude 'custom-engine' 42 | } 43 | tags { 44 | // include 'fast' 45 | exclude 'slow' 46 | } 47 | // includeClassNamePattern '.*Test' 48 | } 49 | // configurationParameter 'junit.jupiter.conditions.deactivate', '*' 50 | // enableStandardTestTask true 51 | // reportsDir file('build/test-results/junit-platform') // this is the default 52 | logManager 'org.apache.logging.log4j.jul.LogManager' 53 | } 54 | 55 | dependencies { 56 | // JUnit Jupiter API and TestEngine implementation 57 | testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}") 58 | testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}") 59 | 60 | // If you also want to support JUnit 3 and JUnit 4 tests 61 | testCompile("junit:junit:${junit4Version}") 62 | testRuntime("org.junit.vintage:junit-vintage-engine:${junitVintageVersion}") 63 | 64 | testRuntime("org.apache.logging.log4j:log4j-core:${log4jVersion}") 65 | testRuntime("org.apache.logging.log4j:log4j-jul:${log4jVersion}") 66 | 67 | // Only needed to run tests in an (IntelliJ) IDE(A) that bundles an older version 68 | testRuntime("org.junit.platform:junit-platform-launcher:${junitPlatformVersion}") 69 | } 70 | 71 | task wrapper(type: Wrapper) { 72 | description = 'Generates gradlew[.bat] scripts' 73 | gradleVersion = '4.0.1' 74 | } -------------------------------------------------------------------------------- /junit5-parameter-resolution-example/gradle.properties: -------------------------------------------------------------------------------- 1 | systemProp.http.proxyHost=donkey.somehost.org 2 | systemProp.http.proxyPort=8080 3 | systemProp.http.proxyUser=userid 4 | systemProp.http.proxyPassword=password 5 | systemProp.http.nonProxyHosts=*.nonproxyrepos.com|localhost 6 | 7 | systemProp.https.proxyHost=donkey.somehost.org 8 | systemProp.https.proxyPort=8080 9 | systemProp.https.proxyUser=userid 10 | systemProp.https.proxyPassword=password 11 | systemProp.http.nonProxyHosts=*.nonproxyrepos.com|localhost -------------------------------------------------------------------------------- /junit5-parameter-resolution-example/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.howtoprogram 5 | junit5-parameter-resolution-example 6 | 0.0.1-SNAPSHOT 7 | jar 8 | 9 | junit5-parameter-resolution-example 10 | http://howtoprogram.xyz 11 | 12 | 13 | UTF-8 14 | 1.8 15 | 4.12 16 | 5.1.1 17 | ${junit.version}.0 18 | 5.1.1 19 | 1.1.1 20 | 21 | 22 | 23 | 24 | 25 | maven-compiler-plugin 26 | 3.1 27 | 28 | ${java.version} 29 | ${java.version} 30 | 31 | 32 | 33 | maven-surefire-plugin 34 | 2.19 35 | 36 | 37 | org.junit.platform 38 | junit-platform-surefire-provider 39 | ${junit.platform.version} 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.junit.jupiter 51 | junit-jupiter-engine 52 | ${junit.jupiter.version} 53 | test 54 | 55 | 56 | 57 | junit 58 | junit 59 | ${junit.version} 60 | test 61 | 62 | 63 | org.junit.platform 64 | junit-platform-runner 65 | ${junit.platform.version} 66 | test 67 | 68 | 69 | org.junit.vintage 70 | junit-vintage-engine 71 | ${junit.vintage.version} 72 | test 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /junit5-parameter-resolution-example/src/test/java/xyz/howtoprogram/junit5/order/AuditService.java: -------------------------------------------------------------------------------- 1 | package xyz.howtoprogram.junit5.order; 2 | 3 | public class AuditService { 4 | 5 | public void audit() { 6 | // Dummy methods 7 | System.out.println("---audited---"); 8 | 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /junit5-parameter-resolution-example/src/test/java/xyz/howtoprogram/junit5/order/AuditServiceParameterResolver.java: -------------------------------------------------------------------------------- 1 | 2 | package xyz.howtoprogram.junit5.order; 3 | 4 | import org.junit.jupiter.api.extension.ExtensionContext; 5 | import org.junit.jupiter.api.extension.ParameterContext; 6 | import org.junit.jupiter.api.extension.ParameterResolutionException; 7 | import org.junit.jupiter.api.extension.ParameterResolver; 8 | 9 | public class AuditServiceParameterResolver implements ParameterResolver { 10 | 11 | @Override 12 | public Object resolveParameter(ParameterContext parameterContext, 13 | ExtensionContext extensionContext) throws ParameterResolutionException { 14 | return new AuditService(); 15 | } 16 | 17 | @Override 18 | public boolean supportsParameter(ParameterContext parameterContext, 19 | ExtensionContext extensionContext) throws ParameterResolutionException { 20 | return (parameterContext.getParameter().getType() == AuditService.class); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /junit5-parameter-resolution-example/src/test/java/xyz/howtoprogram/junit5/order/EnvironmentInfo.java: -------------------------------------------------------------------------------- 1 | package xyz.howtoprogram.junit5.order; 2 | 3 | public class EnvironmentInfo { 4 | 5 | public String getOS() { 6 | return "Windows 10"; 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /junit5-parameter-resolution-example/src/test/java/xyz/howtoprogram/junit5/order/EnvironmentNameParameterResolver.java: -------------------------------------------------------------------------------- 1 | package xyz.howtoprogram.junit5.order; 2 | 3 | import org.junit.jupiter.api.extension.ExtensionContext; 4 | import org.junit.jupiter.api.extension.ParameterContext; 5 | import org.junit.jupiter.api.extension.ParameterResolutionException; 6 | import org.junit.jupiter.api.extension.ParameterResolver; 7 | 8 | public class EnvironmentNameParameterResolver implements ParameterResolver { 9 | 10 | @Override 11 | public Object resolveParameter(ParameterContext arg0, ExtensionContext arg1) throws ParameterResolutionException { 12 | return new EnvironmentInfo(); 13 | } 14 | 15 | @Override 16 | public boolean supportsParameter(ParameterContext arg0, ExtensionContext arg1) throws ParameterResolutionException { 17 | return true; 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /junit5-parameter-resolution-example/src/test/java/xyz/howtoprogram/junit5/order/Report.java: -------------------------------------------------------------------------------- 1 | package xyz.howtoprogram.junit5.order; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * @since 5.0 10 | */ 11 | @Target(ElementType.PARAMETER) 12 | @Retention(RetentionPolicy.RUNTIME) 13 | public @interface Report { 14 | } -------------------------------------------------------------------------------- /junit5-parameter-resolution-example/src/test/java/xyz/howtoprogram/junit5/order/ReportAnnotationParameterResolver.java: -------------------------------------------------------------------------------- 1 | package xyz.howtoprogram.junit5.order; 2 | 3 | import org.junit.jupiter.api.extension.ExtensionContext; 4 | import org.junit.jupiter.api.extension.ParameterContext; 5 | import org.junit.jupiter.api.extension.ParameterResolutionException; 6 | import org.junit.jupiter.api.extension.ParameterResolver; 7 | import org.junit.platform.commons.util.ReflectionUtils; 8 | 9 | public class ReportAnnotationParameterResolver implements ParameterResolver { 10 | 11 | @Override 12 | public Object resolveParameter(ParameterContext parameterContext, 13 | ExtensionContext extensionContext) throws ParameterResolutionException { 14 | return ReflectionUtils.newInstance(parameterContext.getParameter().getType()); 15 | } 16 | 17 | @Override 18 | public boolean supportsParameter(ParameterContext parameterContext, 19 | ExtensionContext extensionContext) throws ParameterResolutionException { 20 | return parameterContext.getParameter().isAnnotationPresent(Report.class); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /junit5-parameter-resolution-example/src/test/java/xyz/howtoprogram/junit5/order/ReportService.java: -------------------------------------------------------------------------------- 1 | package xyz.howtoprogram.junit5.order; 2 | 3 | public class ReportService { 4 | 5 | public void report() { 6 | // 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /junit5-parameter-resolution-example/src/test/java/xyz/howtoprogram/junit5/order/TestOrderService.java: -------------------------------------------------------------------------------- 1 | package xyz.howtoprogram.junit5.order; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | import org.junit.jupiter.api.DisplayName; 5 | import org.junit.jupiter.api.Test; 6 | import org.junit.jupiter.api.TestInfo; 7 | import org.junit.jupiter.api.TestReporter; 8 | import org.junit.jupiter.api.extension.ExtendWith; 9 | 10 | @ExtendWith(ReportAnnotationParameterResolver.class) 11 | @ExtendWith(AuditServiceParameterResolver.class) 12 | // @ExtendWith(EnvironmentNameParameterResolver.class) 13 | public class TestOrderService { 14 | 15 | @DisplayName("Test placeOrder method") 16 | @Test 17 | public void placeOrderOK(TestInfo testInfo, TestReporter testReporter) { 18 | 19 | assertEquals("Test placeOrder method", testInfo.getDisplayName()); 20 | testReporter.publishEntry("key", "value"); 21 | 22 | assertEquals(1, 1); 23 | } 24 | 25 | @Test 26 | public void placeOrderNoItem(AuditService auditService) { 27 | auditService.audit(); 28 | assertEquals(1, 1); 29 | } 30 | 31 | @Test 32 | public void placeOrderNG(@Report ReportService reportService) { 33 | reportService.report(); 34 | assertEquals(1, 1); 35 | } 36 | 37 | // @Test 38 | public void placeOrderNotLogin(EnvironmentInfo environmentInfo) { 39 | System.out.println(environmentInfo.getOS()); 40 | assertEquals("Windows 10", environmentInfo.getOS()); 41 | assertEquals(1, 1); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /junit5-spring-boot-example/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | /target/ 3 | /.classpath/ 4 | /.project/ 5 | /.settings/ -------------------------------------------------------------------------------- /junit5-spring-boot-example/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/howtoprogram/junit5-examples/2d8a9fb1ac0ea15d089b51415127b62715acdc34/junit5-spring-boot-example/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /junit5-spring-boot-example/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip 2 | -------------------------------------------------------------------------------- /junit5-spring-boot-example/README.md: -------------------------------------------------------------------------------- 1 | #JUnit 5 and Spring Boot Example 2 | 3 | 4 | ## 1. Import source code into Eclipse 5 | ### Maven 6 | 7 | Menu **File –> Import –> Maven –> Existing Maven Projects** 8 | 9 | Browse to your source code location 10 | 11 | Click **Finish** button to finish the importing 12 | 13 | 14 | ## 3. Run the example 15 | 16 | mvn test 17 | 18 | All the source code are described in: [JUnit 5 and Spring Boot Example](https://howtoprogram.xyz/2017/09/12/junit-5-spring-boot-example/) 19 | -------------------------------------------------------------------------------- /junit5-spring-boot-example/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | } 5 | dependencies { 6 | classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.6.RELEASE") 7 | } 8 | } 9 | 10 | apply plugin: 'java' 11 | apply plugin: 'eclipse' 12 | apply plugin: 'idea' 13 | apply plugin: 'org.springframework.boot' 14 | 15 | jar { 16 | baseName = 'junit5-spring-boot-example' 17 | version = '0.1.0' 18 | } 19 | 20 | repositories { 21 | mavenCentral() 22 | } 23 | 24 | sourceCompatibility = 1.8 25 | targetCompatibility = 1.8 26 | 27 | dependencies { 28 | compile("org.springframework.boot:spring-boot-starter-web") 29 | // tag::actuator[] 30 | compile("org.springframework.boot:spring-boot-starter-actuator") 31 | // end::actuator[] 32 | // tag::tests[] 33 | testCompile("org.springframework.boot:spring-boot-starter-test") 34 | // end::tests[] 35 | } 36 | 37 | -------------------------------------------------------------------------------- /junit5-spring-boot-example/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/howtoprogram/junit5-examples/2d8a9fb1ac0ea15d089b51415127b62715acdc34/junit5-spring-boot-example/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /junit5-spring-boot-example/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Aug 29 13:09:28 CDT 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip 7 | -------------------------------------------------------------------------------- /junit5-spring-boot-example/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /junit5-spring-boot-example/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /junit5-spring-boot-example/mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven2 Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # 58 | # Look for the Apple JDKs first to preserve the existing behaviour, and then look 59 | # for the new JDKs provided by Oracle. 60 | # 61 | if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK ] ; then 62 | # 63 | # Apple JDKs 64 | # 65 | export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home 66 | fi 67 | 68 | if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Java/JavaVirtualMachines/CurrentJDK ] ; then 69 | # 70 | # Apple JDKs 71 | # 72 | export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home 73 | fi 74 | 75 | if [ -z "$JAVA_HOME" ] && [ -L "/Library/Java/JavaVirtualMachines/CurrentJDK" ] ; then 76 | # 77 | # Oracle JDKs 78 | # 79 | export JAVA_HOME=/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home 80 | fi 81 | 82 | if [ -z "$JAVA_HOME" ] && [ -x "/usr/libexec/java_home" ]; then 83 | # 84 | # Apple JDKs 85 | # 86 | export JAVA_HOME=`/usr/libexec/java_home` 87 | fi 88 | ;; 89 | esac 90 | 91 | if [ -z "$JAVA_HOME" ] ; then 92 | if [ -r /etc/gentoo-release ] ; then 93 | JAVA_HOME=`java-config --jre-home` 94 | fi 95 | fi 96 | 97 | if [ -z "$M2_HOME" ] ; then 98 | ## resolve links - $0 may be a link to maven's home 99 | PRG="$0" 100 | 101 | # need this for relative symlinks 102 | while [ -h "$PRG" ] ; do 103 | ls=`ls -ld "$PRG"` 104 | link=`expr "$ls" : '.*-> \(.*\)$'` 105 | if expr "$link" : '/.*' > /dev/null; then 106 | PRG="$link" 107 | else 108 | PRG="`dirname "$PRG"`/$link" 109 | fi 110 | done 111 | 112 | saveddir=`pwd` 113 | 114 | M2_HOME=`dirname "$PRG"`/.. 115 | 116 | # make it fully qualified 117 | M2_HOME=`cd "$M2_HOME" && pwd` 118 | 119 | cd "$saveddir" 120 | # echo Using m2 at $M2_HOME 121 | fi 122 | 123 | # For Cygwin, ensure paths are in UNIX format before anything is touched 124 | if $cygwin ; then 125 | [ -n "$M2_HOME" ] && 126 | M2_HOME=`cygpath --unix "$M2_HOME"` 127 | [ -n "$JAVA_HOME" ] && 128 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 129 | [ -n "$CLASSPATH" ] && 130 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 131 | fi 132 | 133 | # For Migwn, ensure paths are in UNIX format before anything is touched 134 | if $mingw ; then 135 | [ -n "$M2_HOME" ] && 136 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 137 | [ -n "$JAVA_HOME" ] && 138 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 139 | # TODO classpath? 140 | fi 141 | 142 | if [ -z "$JAVA_HOME" ]; then 143 | javaExecutable="`which javac`" 144 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 145 | # readlink(1) is not available as standard on Solaris 10. 146 | readLink=`which readlink` 147 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 148 | if $darwin ; then 149 | javaHome="`dirname \"$javaExecutable\"`" 150 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 151 | else 152 | javaExecutable="`readlink -f \"$javaExecutable\"`" 153 | fi 154 | javaHome="`dirname \"$javaExecutable\"`" 155 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 156 | JAVA_HOME="$javaHome" 157 | export JAVA_HOME 158 | fi 159 | fi 160 | fi 161 | 162 | if [ -z "$JAVACMD" ] ; then 163 | if [ -n "$JAVA_HOME" ] ; then 164 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 165 | # IBM's JDK on AIX uses strange locations for the executables 166 | JAVACMD="$JAVA_HOME/jre/sh/java" 167 | else 168 | JAVACMD="$JAVA_HOME/bin/java" 169 | fi 170 | else 171 | JAVACMD="`which java`" 172 | fi 173 | fi 174 | 175 | if [ ! -x "$JAVACMD" ] ; then 176 | echo "Error: JAVA_HOME is not defined correctly." >&2 177 | echo " We cannot execute $JAVACMD" >&2 178 | exit 1 179 | fi 180 | 181 | if [ -z "$JAVA_HOME" ] ; then 182 | echo "Warning: JAVA_HOME environment variable is not set." 183 | fi 184 | 185 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 186 | 187 | # For Cygwin, switch paths to Windows format before running java 188 | if $cygwin; then 189 | [ -n "$M2_HOME" ] && 190 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 191 | [ -n "$JAVA_HOME" ] && 192 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 193 | [ -n "$CLASSPATH" ] && 194 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 195 | fi 196 | 197 | # traverses directory structure from process work directory to filesystem root 198 | # first directory with .mvn subdirectory is considered project base directory 199 | find_maven_basedir() { 200 | local basedir=$(pwd) 201 | local wdir=$(pwd) 202 | while [ "$wdir" != '/' ] ; do 203 | if [ -d "$wdir"/.mvn ] ; then 204 | basedir=$wdir 205 | break 206 | fi 207 | wdir=$(cd "$wdir/.."; pwd) 208 | done 209 | echo "${basedir}" 210 | } 211 | 212 | # concatenates all lines of a file 213 | concat_lines() { 214 | if [ -f "$1" ]; then 215 | echo "$(tr -s '\n' ' ' < "$1")" 216 | fi 217 | } 218 | 219 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-$(find_maven_basedir)} 220 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 221 | 222 | # Provide a "standardized" way to retrieve the CLI args that will 223 | # work with both Windows and non-Windows executions. 224 | MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" 225 | export MAVEN_CMD_LINE_ARGS 226 | 227 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 228 | 229 | exec "$JAVACMD" \ 230 | $MAVEN_OPTS \ 231 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 232 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 233 | ${WRAPPER_LAUNCHER} "$@" 234 | -------------------------------------------------------------------------------- /junit5-spring-boot-example/mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven2 Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' 39 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 40 | 41 | @REM set %HOME% to equivalent of $HOME 42 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 43 | 44 | @REM Execute a user defined script before this one 45 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 46 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 47 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 48 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 49 | :skipRcPre 50 | 51 | @setlocal 52 | 53 | set ERROR_CODE=0 54 | 55 | @REM To isolate internal variables from possible post scripts, we use another setlocal 56 | @setlocal 57 | 58 | @REM ==== START VALIDATION ==== 59 | if not "%JAVA_HOME%" == "" goto OkJHome 60 | 61 | echo. 62 | echo Error: JAVA_HOME not found in your environment. >&2 63 | echo Please set the JAVA_HOME variable in your environment to match the >&2 64 | echo location of your Java installation. >&2 65 | echo. 66 | goto error 67 | 68 | :OkJHome 69 | if exist "%JAVA_HOME%\bin\java.exe" goto init 70 | 71 | echo. 72 | echo Error: JAVA_HOME is set to an invalid directory. >&2 73 | echo JAVA_HOME = "%JAVA_HOME%" >&2 74 | echo Please set the JAVA_HOME variable in your environment to match the >&2 75 | echo location of your Java installation. >&2 76 | echo. 77 | goto error 78 | 79 | @REM ==== END VALIDATION ==== 80 | 81 | :init 82 | 83 | set MAVEN_CMD_LINE_ARGS=%* 84 | 85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 86 | @REM Fallback to current working directory if not found. 87 | 88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 90 | 91 | set EXEC_DIR=%CD% 92 | set WDIR=%EXEC_DIR% 93 | :findBaseDir 94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 95 | cd .. 96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 97 | set WDIR=%CD% 98 | goto findBaseDir 99 | 100 | :baseDirFound 101 | set MAVEN_PROJECTBASEDIR=%WDIR% 102 | cd "%EXEC_DIR%" 103 | goto endDetectBaseDir 104 | 105 | :baseDirNotFound 106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 107 | cd "%EXEC_DIR%" 108 | 109 | :endDetectBaseDir 110 | 111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 112 | 113 | @setlocal EnableExtensions EnableDelayedExpansion 114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 116 | 117 | :endReadAdditionalConfig 118 | 119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 120 | 121 | set WRAPPER_JAR="".\.mvn\wrapper\maven-wrapper.jar"" 122 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 123 | 124 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CMD_LINE_ARGS% 125 | if ERRORLEVEL 1 goto error 126 | goto end 127 | 128 | :error 129 | set ERROR_CODE=1 130 | 131 | :end 132 | @endlocal & set ERROR_CODE=%ERROR_CODE% 133 | 134 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 135 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 136 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 137 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 138 | :skipRcPost 139 | 140 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 141 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 142 | 143 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 144 | 145 | exit /B %ERROR_CODE% -------------------------------------------------------------------------------- /junit5-spring-boot-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.springframework 8 | junit5-spring-boot-example 9 | 0.1.0 10 | 11 | 12 | org.springframework.boot 13 | spring-boot-starter-parent 14 | 2.0.4.RELEASE 15 | 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-web 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-actuator 26 | 27 | 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-test 33 | test 34 | 35 | 36 | org.junit.jupiter 37 | junit-jupiter-api 38 | 39 | 40 | org.junit.jupiter 41 | junit-jupiter-engine 42 | 43 | 44 | 45 | 46 | 47 | 48 | 1.8 49 | 5.3.0 50 | 1.3.0 51 | 52 | 53 | 54 | 55 | 56 | org.springframework.boot 57 | spring-boot-maven-plugin 58 | 59 | 60 | 61 | 62 | 63 | 64 | spring-snapshots 65 | http://repo.spring.io/snapshot 66 | 67 | true 68 | 69 | 70 | 71 | spring-milestones 72 | http://repo.spring.io/milestone 73 | 74 | 75 | 76 | 77 | spring-snapshots 78 | http://repo.spring.io/snapshot 79 | 80 | 81 | spring-milestones 82 | http://repo.spring.io/milestone 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /junit5-spring-boot-example/src/main/java/com/howtoprogram/unit5/Application.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.unit5; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Application.class, args); 11 | } 12 | 13 | } -------------------------------------------------------------------------------- /junit5-spring-boot-example/src/main/java/com/howtoprogram/unit5/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.unit5; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | @RestController 7 | public class HelloController { 8 | 9 | @GetMapping("/about") 10 | public String aboutMe() { 11 | return "JUnit 5 and Spring Boot Example."; 12 | } 13 | 14 | } -------------------------------------------------------------------------------- /junit5-spring-boot-example/src/test/java/com/howtoprogram/unit5/ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.unit5; 2 | 3 | 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.test.context.SpringBootTest; 7 | import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; 8 | import org.springframework.boot.test.web.client.TestRestTemplate; 9 | 10 | import static org.junit.jupiter.api.Assertions.assertEquals; 11 | import org.junit.jupiter.api.Test; 12 | import org.junit.jupiter.api.extension.ExtendWith; 13 | import org.springframework.test.context.junit.jupiter.SpringExtension; 14 | 15 | @ExtendWith(SpringExtension.class) 16 | @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) 17 | public class ApplicationTests { 18 | 19 | @Autowired 20 | private TestRestTemplate restTemplate; 21 | 22 | @Test 23 | public void testAbout() { 24 | String message = this.restTemplate.getForObject("/about", String.class); 25 | assertEquals("JUnit 5 and Spring Boot Example.", message); 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /junit5-tag-filter-example/.gradle/3.0/taskArtifacts/cache.properties: -------------------------------------------------------------------------------- 1 | #Thu Dec 08 22:16:10 ICT 2016 2 | -------------------------------------------------------------------------------- /junit5-tag-filter-example/.gradle/3.0/taskArtifacts/cache.properties.lock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/howtoprogram/junit5-examples/2d8a9fb1ac0ea15d089b51415127b62715acdc34/junit5-tag-filter-example/.gradle/3.0/taskArtifacts/cache.properties.lock -------------------------------------------------------------------------------- /junit5-tag-filter-example/.gradle/3.0/taskArtifacts/fileHashes.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/howtoprogram/junit5-examples/2d8a9fb1ac0ea15d089b51415127b62715acdc34/junit5-tag-filter-example/.gradle/3.0/taskArtifacts/fileHashes.bin -------------------------------------------------------------------------------- /junit5-tag-filter-example/.gradle/3.0/taskArtifacts/fileSnapshots.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/howtoprogram/junit5-examples/2d8a9fb1ac0ea15d089b51415127b62715acdc34/junit5-tag-filter-example/.gradle/3.0/taskArtifacts/fileSnapshots.bin -------------------------------------------------------------------------------- /junit5-tag-filter-example/.gradle/3.0/taskArtifacts/fileSnapshotsToTreeSnapshotsIndex.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/howtoprogram/junit5-examples/2d8a9fb1ac0ea15d089b51415127b62715acdc34/junit5-tag-filter-example/.gradle/3.0/taskArtifacts/fileSnapshotsToTreeSnapshotsIndex.bin -------------------------------------------------------------------------------- /junit5-tag-filter-example/.gradle/3.0/taskArtifacts/taskArtifacts.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/howtoprogram/junit5-examples/2d8a9fb1ac0ea15d089b51415127b62715acdc34/junit5-tag-filter-example/.gradle/3.0/taskArtifacts/taskArtifacts.bin -------------------------------------------------------------------------------- /junit5-tag-filter-example/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | } 5 | dependencies { 6 | classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0' 7 | } 8 | } 9 | 10 | repositories { 11 | mavenCentral() 12 | } 13 | 14 | ext.junit4Version = '4.12' 15 | ext.junitVintageVersion = '4.12.0' 16 | ext.junitPlatformVersion = '1.0.0' 17 | ext.junitJupiterVersion = '5.0.0' 18 | ext.log4jVersion = '2.6.2' 19 | 20 | apply plugin: 'java' 21 | apply plugin: 'eclipse' 22 | apply plugin: 'idea' 23 | apply plugin: 'org.junit.platform.gradle.plugin' 24 | 25 | jar { 26 | baseName = 'junit5-tag-filter-example' 27 | version = '1.0.0-SNAPSHOT' 28 | } 29 | 30 | compileTestJava { 31 | sourceCompatibility = 1.8 32 | targetCompatibility = 1.8 33 | options.compilerArgs += '-parameters' 34 | } 35 | 36 | junitPlatform { 37 | // platformVersion '1.0.0' 38 | filters { 39 | engines { 40 | // include 'junit-jupiter', 'junit-vintage' 41 | // exclude 'custom-engine' 42 | } 43 | tags { 44 | include 'service', 'fast' 45 | exclude 'slow' 46 | } 47 | // includeClassNamePattern '.*Test' 48 | } 49 | // enableStandardTestTask true 50 | // reportsDir file('build/test-results/junit-platform') // this is the default 51 | logManager 'org.apache.logging.log4j.jul.LogManager' 52 | } 53 | 54 | dependencies { 55 | // JUnit Jupiter API and TestEngine implementation 56 | testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}") 57 | testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}") 58 | 59 | // If you also want to support JUnit 3 and JUnit 4 tests 60 | testCompile("junit:junit:${junit4Version}") 61 | testRuntime("org.junit.vintage:junit-vintage-engine:${junitVintageVersion}") 62 | 63 | testRuntime("org.apache.logging.log4j:log4j-core:${log4jVersion}") 64 | testRuntime("org.apache.logging.log4j:log4j-jul:${log4jVersion}") 65 | } 66 | 67 | task wrapper(type: Wrapper) { 68 | description = 'Generates gradlew[.bat] scripts' 69 | gradleVersion = '3.0' 70 | } 71 | -------------------------------------------------------------------------------- /junit5-tag-filter-example/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/howtoprogram/junit5-examples/2d8a9fb1ac0ea15d089b51415127b62715acdc34/junit5-tag-filter-example/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /junit5-tag-filter-example/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Aug 28 19:04:45 CEST 2016 2 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.0-bin.zip 3 | distributionBase=GRADLE_USER_HOME 4 | distributionPath=wrapper/dists 5 | zipStorePath=wrapper/dists 6 | zipStoreBase=GRADLE_USER_HOME 7 | -------------------------------------------------------------------------------- /junit5-tag-filter-example/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn ( ) { 37 | echo "$*" 38 | } 39 | 40 | die ( ) { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 165 | if [[ "$(uname)" == "Darwin" ]] && [[ "$HOME" == "$PWD" ]]; then 166 | cd "$(dirname "$0")" 167 | fi 168 | 169 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 170 | -------------------------------------------------------------------------------- /junit5-tag-filter-example/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /junit5-tag-filter-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.howtoprogram 7 | junit5-tag-filter-example 8 | 1.0-SNAPSHOT 9 | jar 10 | 11 | 12 | UTF-8 13 | 1.8 14 | 5.1.1 15 | 1.1.1 16 | 17 | 18 | 19 | 20 | 21 | maven-compiler-plugin 22 | 3.1 23 | 24 | ${java.version} 25 | ${java.version} 26 | 27 | 28 | 29 | maven-surefire-plugin 30 | 2.19 31 | 32 | 33 | service,slow 34 | fast 35 | 36 | 37 | 38 | 39 | org.junit.platform 40 | junit-platform-surefire-provider 41 | ${junit.platform.version} 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.junit.jupiter 51 | junit-jupiter-engine 52 | ${junit.jupiter.version} 53 | test 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /junit5-tag-filter-example/src/test/java/com/howtoprogram/junit5/OrderServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.howtoprogram.junit5; 2 | 3 | import org.junit.jupiter.api.Tag; 4 | import org.junit.jupiter.api.Test; 5 | 6 | @Tag("service") 7 | public class OrderServiceTest { 8 | 9 | @Test 10 | @Tag("slow") 11 | public void placeOrderTest() { 12 | } 13 | 14 | @Test 15 | @Tag("fast") 16 | public void checkout() { 17 | } 18 | 19 | @Test 20 | @Tag("slow") 21 | public void doPayment() { 22 | } 23 | 24 | @Test 25 | @Tag("fast") 26 | public void validateOrder() { 27 | } 28 | } -------------------------------------------------------------------------------- /junit5-test-suite-example/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | /target/ 3 | /.classpath/ 4 | /.project/ 5 | /.settings/ -------------------------------------------------------------------------------- /junit5-test-suite-example/README.md: -------------------------------------------------------------------------------- 1 | #JUnit 5 Test Suite Example 2 | 3 | 4 | ## 1. Import source code into Eclipse 5 | ### Maven 6 | 7 | Menu **File –> Import –> Maven –> Existing Maven Projects** 8 | 9 | Browse to your source code location 10 | 11 | Click **Finish** button to finish the importing 12 | 13 | ### Gradle 14 | Menu **File –> Import –> Gradle Project –> Select Project --> Next until finish** 15 | 16 | ## 3. Run the example 17 | 18 | 19 | Open the **PlayOrderFeatureSuite.java, UserFeatureSuite.java** 20 | 21 | **Right click -> Run As -> JUnit Test** or use the shortcut: **Alt+Shift+x, T** to start the main method 22 | 23 | All the source code are described in: [JUnit 5 Test Suite ](http://howtoprogram.xyz/2016/08/16/junit-5-test-suite/) 24 | 25 | ## 4. Related Posts 26 | ### [JUnit 5 Tutorial](http://howtoprogram.xyz/java-technologies/junit-5-tutorial/) 27 | ### [JUnit 5 vs JUnit 4](http://howtoprogram.xyz/2016/08/10/junit-5-vs-junit-4/) 28 | ### [JUnit 5 Basic Introduction](http://howtoprogram.xyz/2016/08/07/junit-5-basic-introduction/) 29 | ### [JUnit 5 Assertions Example](http://howtoprogram.xyz/2016/08/12/junit-5-assertions-example/) 30 | ### [JUnit 5 Exception Testing](http://howtoprogram.xyz/2016/08/15/junit-5-exception-testing/) 31 | ### [JUnit 5 Disable or Ignore A Test](http://howtoprogram.xyz/2016/08/14/junit-5-disable-ignore-tests/) 32 | ### [JUnit 5 Assumptions With Assume](http://howtoprogram.xyz/2016/08/17/junit-5-assumptions-assume/) 33 | ### [JUnit 5 Nested Tests Examples] (http://howtoprogram.xyz/2016/08/19/junit-5-nested-tests-examples/) 34 | ### [JUnit 5 Dynamic Tests - Generate Tests at Run-time](http://howtoprogram.xyz/2016/08/21/junit-5-dynamic-tests/) 35 | ### [JUnit 5 Maven Example](http://howtoprogram.xyz/2016/09/09/junit-5-maven-example/) -------------------------------------------------------------------------------- /junit5-test-suite-example/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | } 5 | dependencies { 6 | classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M2' 7 | } 8 | } 9 | 10 | repositories { 11 | mavenCentral() 12 | } 13 | 14 | ext.junitVintageVersion = '4.12.0-M2' 15 | ext.junitPlatformVersion = '1.0.0-M2' 16 | ext.junitJupiterVersion = '5.0.0-M2' 17 | 18 | apply plugin: 'java' 19 | apply plugin: 'eclipse' 20 | apply plugin: 'idea' 21 | apply plugin: 'org.junit.platform.gradle.plugin' 22 | 23 | jar { 24 | baseName = 'junit5-test-suite-example' 25 | version = '1.0.0-SNAPSHOT' 26 | } 27 | 28 | compileTestJava { 29 | sourceCompatibility = 1.8 30 | targetCompatibility = 1.8 31 | options.compilerArgs += '-parameters' 32 | } 33 | 34 | dependencies { 35 | // JUnit Jupiter API and TestEngine implementation 36 | testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}") 37 | testCompile("org.junit.platform:junit-platform-runner:${junitPlatformVersion}") 38 | testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}") 39 | 40 | } -------------------------------------------------------------------------------- /junit5-test-suite-example/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.howtoprogram 5 | junit5-test-suite-example 6 | 0.0.1-SNAPSHOT 7 | jar 8 | 9 | junit5-test-suite-example 10 | http://howtoprogram.xyz 11 | 12 | 13 | UTF-8 14 | 1.8 15 | 1.8 16 | 5.1.1 17 | 5.1.1 18 | 1.1.1 19 | 20 | 21 | 22 | 23 | 24 | maven-compiler-plugin 25 | 3.1 26 | 27 | 28 | maven-surefire-plugin 29 | 2.19 30 | 31 | 32 | org.junit.platform 33 | junit-platform-surefire-provider 34 | ${junit.platform.version} 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.junit.platform 44 | junit-platform-runner 45 | ${junit.platform.version} 46 | test 47 | 48 | 49 | org.junit.jupiter 50 | junit-jupiter-engine 51 | ${junit.jupiter.version} 52 | test 53 | 54 | 55 | org.junit.vintage 56 | junit-vintage-engine 57 | ${junit.vintage.version} 58 | test 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /junit5-test-suite-example/src/test/java/xyz/howtoprogram/junit5/order/TestOrderService.java: -------------------------------------------------------------------------------- 1 | package xyz.howtoprogram.junit5.order; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import org.junit.jupiter.api.Test; 6 | import org.junit.platform.runner.JUnitPlatform; 7 | import org.junit.runner.RunWith; 8 | 9 | @RunWith(JUnitPlatform.class) 10 | public class TestOrderService { 11 | 12 | @Test 13 | public void placeOrderNoItem() { 14 | assertEquals(1, 1); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /junit5-test-suite-example/src/test/java/xyz/howtoprogram/junit5/payment/TestPaymentService.java: -------------------------------------------------------------------------------- 1 | package xyz.howtoprogram.junit5.payment; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import org.junit.jupiter.api.Test; 6 | import org.junit.platform.runner.JUnitPlatform; 7 | import org.junit.runner.RunWith; 8 | 9 | @RunWith(JUnitPlatform.class) 10 | public class TestPaymentService { 11 | @Test 12 | public void doPaymentZeroAmount() { 13 | assertEquals(1, 1); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /junit5-test-suite-example/src/test/java/xyz/howtoprogram/junit5/suite/PlayOrderFeatureSuite.java: -------------------------------------------------------------------------------- 1 | package xyz.howtoprogram.junit5.suite; 2 | 3 | import org.junit.platform.runner.JUnitPlatform; 4 | import org.junit.platform.suite.api.SelectClasses; 5 | import org.junit.runner.RunWith; 6 | 7 | import xyz.howtoprogram.junit5.order.TestOrderService; 8 | import xyz.howtoprogram.junit5.payment.TestPaymentService; 9 | import xyz.howtoprogram.junit5.user.TestUserService; 10 | 11 | @RunWith(JUnitPlatform.class) 12 | @SelectClasses({TestUserService.class, TestOrderService.class, TestPaymentService.class}) 13 | public class PlayOrderFeatureSuite { 14 | } 15 | -------------------------------------------------------------------------------- /junit5-test-suite-example/src/test/java/xyz/howtoprogram/junit5/suite/UserFeatureSuite.java: -------------------------------------------------------------------------------- 1 | package xyz.howtoprogram.junit5.suite; 2 | 3 | import org.junit.platform.runner.JUnitPlatform; 4 | import org.junit.platform.suite.api.SelectPackages; 5 | import org.junit.runner.RunWith; 6 | 7 | @RunWith(JUnitPlatform.class) 8 | @SelectPackages("xyz.howtoprogram.junit5.user") 9 | public class UserFeatureSuite { 10 | } 11 | -------------------------------------------------------------------------------- /junit5-test-suite-example/src/test/java/xyz/howtoprogram/junit5/user/TestPasswordService.java: -------------------------------------------------------------------------------- 1 | package xyz.howtoprogram.junit5.user; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import org.junit.jupiter.api.Test; 6 | import org.junit.platform.runner.JUnitPlatform; 7 | import org.junit.runner.RunWith; 8 | 9 | @RunWith(JUnitPlatform.class) 10 | public class TestPasswordService { 11 | @Test 12 | public void changePasswordShortPassword() { 13 | assertEquals(1, 1); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /junit5-test-suite-example/src/test/java/xyz/howtoprogram/junit5/user/TestUserService.java: -------------------------------------------------------------------------------- 1 | package xyz.howtoprogram.junit5.user; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | import org.junit.jupiter.api.Test; 5 | import org.junit.platform.runner.JUnitPlatform; 6 | import org.junit.runner.RunWith; 7 | 8 | @RunWith(JUnitPlatform.class) 9 | public class TestUserService { 10 | 11 | @Test 12 | public void loginCorrectUsernameAndPassword() { 13 | assertEquals(1, 1); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /junit5-timeout-example/README.md: -------------------------------------------------------------------------------- 1 | #JUnit 5 Timeout Test Example 2 | 3 | 4 | ## 1. Import source code into Eclipse 5 | ### Maven 6 | 7 | Menu **File –> Import –> Maven –> Existing Maven Projects** 8 | 9 | Browse to your source code location 10 | 11 | Click **Finish** button to finish the importing 12 | 13 | ### Gradle 14 | Menu **File –> Import –> Gradle Project –> Select Project --> Next until finish** 15 | 16 | ## 3. Run the example 17 | 18 | 19 | Open the **TestOrderService.java 20 | 21 | **Right click -> Run As -> JUnit Test** or use the shortcut: **Alt+Shift+x, T** to start the main method 22 | 23 | All the source code are described in: [Timeout Test in JUnit 5](http://howtoprogram.xyz/2016/12/01/junit-5-timeout-test/) 24 | 25 | ## 4. Related Posts 26 | ### [JUnit 5 Tutorial](http://howtoprogram.xyz/java-technologies/junit-5-tutorial/) 27 | ### [JUnit 5 vs JUnit 4](http://howtoprogram.xyz/2016/08/10/junit-5-vs-junit-4/) 28 | ### [JUnit 5 Basic Introduction](http://howtoprogram.xyz/2016/08/07/junit-5-basic-introduction/) 29 | ### [JUnit 5 Assertions Example](http://howtoprogram.xyz/2016/08/12/junit-5-assertions-example/) 30 | ### [JUnit 5 Exception Testing](http://howtoprogram.xyz/2016/08/15/junit-5-exception-testing/) 31 | ### [JUnit 5 Disable or Ignore A Test](http://howtoprogram.xyz/2016/08/14/junit-5-disable-ignore-tests/) 32 | ### [JUnit 5 Assumptions With Assume](http://howtoprogram.xyz/2016/08/17/junit-5-assumptions-assume/) 33 | ### [JUnit 5 Nested Tests Examples] (http://howtoprogram.xyz/2016/08/19/junit-5-nested-tests-examples/) 34 | ### [JUnit 5 Dynamic Tests - Generate Tests at Run-time](http://howtoprogram.xyz/2016/08/21/junit-5-dynamic-tests/) 35 | ### [JUnit 5 Maven Example](http://howtoprogram.xyz/2016/09/09/junit-5-maven-example/) -------------------------------------------------------------------------------- /junit5-timeout-example/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | } 5 | dependencies { 6 | classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M3' 7 | } 8 | } 9 | 10 | repositories { 11 | mavenCentral() 12 | } 13 | 14 | ext.junitVintageVersion = '4.12.0-M3' 15 | ext.junitPlatformVersion = '1.0.0-M3' 16 | ext.junitJupiterVersion = '5.0.0-M3' 17 | 18 | apply plugin: 'java' 19 | apply plugin: 'eclipse' 20 | apply plugin: 'idea' 21 | apply plugin: 'org.junit.platform.gradle.plugin' 22 | 23 | jar { 24 | baseName = 'junit5-timeout-example' 25 | version = '1.0.0-SNAPSHOT' 26 | } 27 | 28 | compileTestJava { 29 | sourceCompatibility = 1.8 30 | targetCompatibility = 1.8 31 | options.compilerArgs += '-parameters' 32 | } 33 | 34 | dependencies { 35 | // JUnit Jupiter API and TestEngine implementation 36 | testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}") 37 | testCompile("org.junit.platform:junit-platform-runner:${junitPlatformVersion}") 38 | testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}") 39 | 40 | } -------------------------------------------------------------------------------- /junit5-timeout-example/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.howtoprogram 5 | junit5-timeout-example 6 | 0.0.1-SNAPSHOT 7 | jar 8 | 9 | JUnit 5 Timeout Tests 10 | http://howtoprogram.xyz 11 | 12 | 13 | UTF-8 14 | 1.8 15 | 1.8 16 | 5.1.1 17 | 5.1.1 18 | 1.1.1 19 | 20 | 21 | 22 | 23 | 24 | maven-compiler-plugin 25 | 3.1 26 | 27 | 28 | maven-surefire-plugin 29 | 2.19 30 | 31 | 32 | org.junit.platform 33 | junit-platform-surefire-provider 34 | ${junit.platform.version} 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.junit.platform 44 | junit-platform-runner 45 | ${junit.platform.version} 46 | test 47 | 48 | 49 | org.junit.jupiter 50 | junit-jupiter-engine 51 | ${junit.jupiter.version} 52 | test 53 | 54 | 55 | org.junit.vintage 56 | junit-vintage-engine 57 | ${junit.vintage.version} 58 | test 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /junit5-timeout-example/src/main/java/xyz/howtoprogram/junit5/timeout/OrderService.java: -------------------------------------------------------------------------------- 1 | package xyz.howtoprogram.junit5.timeout; 2 | 3 | public class OrderService { 4 | 5 | public void doPayment() { 6 | try { 7 | Thread.sleep(10000);// 10 seconds 8 | } catch (InterruptedException e) { 9 | e.printStackTrace(); 10 | } 11 | } 12 | 13 | public void printShippingLabel() { 14 | try { 15 | Thread.sleep(20000);// 20 seconds 16 | } catch (InterruptedException e) { 17 | e.printStackTrace(); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /junit5-timeout-example/src/test/java/xyz/howtoprogram/junit5/timeout/TestOrderService.java: -------------------------------------------------------------------------------- 1 | package xyz.howtoprogram.junit5.timeout; 2 | 3 | import static java.time.Duration.ofSeconds; 4 | import static org.junit.jupiter.api.Assertions.assertTimeout; 5 | import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; 6 | import org.junit.jupiter.api.Test; 7 | import org.junit.platform.runner.JUnitPlatform; 8 | import org.junit.runner.RunWith; 9 | 10 | @RunWith(JUnitPlatform.class) 11 | public class TestOrderService { 12 | 13 | @Test 14 | public void doPaymentNotExceed15Seconds() { 15 | OrderService orderService = new OrderService(); 16 | assertTimeout(ofSeconds(15), () -> { 17 | // This method runs in 10 seconds 18 | orderService.doPayment(); 19 | }); 20 | } 21 | 22 | @Test 23 | public void doPaymentExceed5Seconds() { 24 | OrderService orderService = new OrderService(); 25 | assertTimeout(ofSeconds(5), () -> { 26 | // This method runs in 10 seconds 27 | orderService.doPayment(); 28 | } , "The doPayment method take more than 5 seconds"); 29 | } 30 | 31 | @Test 32 | public void printShippingLabelExceeded15SecondsWithPreemptiveTermination() { 33 | OrderService orderService = new OrderService(); 34 | assertTimeoutPreemptively(ofSeconds(15), () -> { 35 | // This method takes 20 seconds to run 36 | orderService.printShippingLabel(); 37 | } , () -> "The printShippingLabel method took more than 15 seconds and was aborted."); 38 | } 39 | } 40 | --------------------------------------------------------------------------------