├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── gradle.properties ├── junit4 └── src │ ├── integTest │ ├── java │ │ └── com │ │ │ └── tngtech │ │ │ └── test │ │ │ └── java │ │ │ └── junit │ │ │ └── dataprovider │ │ │ ├── category │ │ │ ├── CategoryOne.java │ │ │ ├── CategoryTwo.java │ │ │ ├── CategoryOneTestSuite.java │ │ │ ├── CategoryTwoTestSuite.java │ │ │ └── CategoryTest.java │ │ │ ├── override │ │ │ ├── DataProviderAcceptanceTestInterface.java │ │ │ ├── DataProviderOverridingAcceptanceTest.java │ │ │ ├── DataProviderAbstractAcceptanceBaseTest.java │ │ │ └── DataProviderImplementingAcceptanceTest.java │ │ │ ├── StringDataProvider.java │ │ │ ├── custom │ │ │ ├── CustomDataProviderRunner.java │ │ │ ├── DateTimeAwareStringConverter.java │ │ │ └── DataProviderCustomStringConverterAcceptanceTest.java │ │ │ ├── RuleAcceptanceTest.java │ │ │ ├── DataProviderNewlineAcceptanceTest.java │ │ │ ├── DataProviderBeforeAcceptanceTest.java │ │ │ ├── CacheDataProviderResultsGloballyAcceptanceTest.java │ │ │ └── DataProviderListArgAcceptanceTest.java │ └── groovy │ │ └── com │ │ └── tngtech │ │ └── test │ │ └── java │ │ └── junit │ │ └── dataprovider │ │ └── DataProviderGroovyAcceptanceTest.groovy │ ├── test │ └── java │ │ └── com │ │ └── tngtech │ │ └── java │ │ └── junit │ │ └── dataprovider │ │ └── internal │ │ ├── placeholder │ │ ├── IndexPlaceholderTest.java │ │ ├── SimpleMethodNamePlaceholderTest.java │ │ ├── CanonicalMethodNamePlaceholderTest.java │ │ ├── SimpleClassNamePlaceholderTest.java │ │ └── CanonicalClassNamePlaceholderTest.java │ │ └── convert │ │ └── SingleArgConverterTest.java │ └── main │ └── java │ └── com │ └── tngtech │ └── java │ └── junit │ └── dataprovider │ ├── common │ └── Preconditions.java │ ├── DataProviderMethodResolver.java │ ├── internal │ └── convert │ │ ├── AbstractObjectConverter.java │ │ ├── ObjectArrayConverter.java │ │ └── SingleArgConverter.java │ └── format │ └── DataProviderPlaceholderFormatter.java ├── junit-jupiter └── src │ ├── integTest │ ├── java │ │ └── com │ │ │ └── tngtech │ │ │ └── test │ │ │ └── junit │ │ │ └── dataprovider │ │ │ ├── override │ │ │ ├── AcceptanceTestInterface.java │ │ │ ├── AbstractAcceptanceBaseTest.java │ │ │ ├── OverridingAcceptanceTest.java │ │ │ └── ImplementingAcceptanceTest.java │ │ │ ├── custom │ │ │ ├── meta │ │ │ │ ├── FixedLocationDataProviderTest.java │ │ │ │ ├── ExtendWithDataProvider.java │ │ │ │ ├── DataProviderLocation.java │ │ │ │ ├── StringDataProviderExtension.java │ │ │ │ ├── DataProviderTest.java │ │ │ │ ├── StringDataProvider.java │ │ │ │ ├── DataProviderTestExtension.java │ │ │ │ └── MetaAnnotationAcceptanceTest.java │ │ │ ├── resolver │ │ │ │ ├── DataProviderStartWithTestMethodNameResolver.java │ │ │ │ ├── CustomResolverDataProviderTest.java │ │ │ │ └── CustomResolverDataProviderTestExtension.java │ │ │ ├── placeholder │ │ │ │ ├── StripArgumentLengthPlaceholder.java │ │ │ │ ├── CustomPlaceholderAcceptanceTest.java │ │ │ │ └── CustomPlaceholderDataProviderExtension.java │ │ │ └── converter │ │ │ │ ├── DateTimeAwareStringConverter.java │ │ │ │ ├── CustomConverterDataProviderExtension.java │ │ │ │ └── CustomConverterDataProvider.java │ │ │ ├── external │ │ │ ├── ExternalDataProvider.java │ │ │ └── ExternalDataProviderAcceptanceTest.java │ │ │ ├── TagAcceptanceTest.java │ │ │ ├── ListAsArgumentAcceptanceTest.java │ │ │ ├── BeforeAcceptanceTest.java │ │ │ ├── NameConventionAcceptanceTest.java │ │ │ ├── DataproviderMethodParameterAcceptanceTest.java │ │ │ ├── NewlineAcceptanceTest.java │ │ │ ├── CacheDataProviderResultsGloballyAcceptanceTest.java │ │ │ ├── AdditionalTestMethodParameterAcceptanceTest.java │ │ │ ├── NestedAcceptanceTest.java │ │ │ ├── NonStaticDataProviderAcceptanceTest.java │ │ │ ├── ListDataProviderAcceptanceTest.java │ │ │ └── SingleArgumentDataProviderAcceptanceTest.java │ └── groovy │ │ └── com │ │ └── tngtech │ │ └── test │ │ └── java │ │ └── junit │ │ └── dataprovider │ │ └── GroovyAcceptanceTest.groovy │ ├── main │ └── java │ │ └── com │ │ └── tngtech │ │ └── junit │ │ └── dataprovider │ │ ├── format │ │ └── DataProviderPlaceholderFormatter.java │ │ ├── DataProviderExtension.java │ │ ├── DataProviderParameterResolver.java │ │ └── DisplayNameContext.java │ └── test │ └── java │ └── com │ └── tngtech │ └── junit │ └── dataprovider │ └── AbstractDataProviderInvocationContextProviderTest.java ├── junit-jupiter-params └── src │ ├── integTest │ ├── java │ │ └── com │ │ │ └── tngtech │ │ │ └── test │ │ │ └── junit │ │ │ └── dataprovider │ │ │ ├── override │ │ │ ├── AcceptanceTestInterface.java │ │ │ ├── OverridingAcceptanceTest.java │ │ │ ├── AbstractAcceptanceBaseTest.java │ │ │ └── ImplementingAcceptanceTest.java │ │ │ ├── custom │ │ │ ├── meta │ │ │ │ ├── FixedLocationDataProviderTest.java │ │ │ │ ├── StringDataProviderProvider.java │ │ │ │ ├── DataProviderLocation.java │ │ │ │ ├── StringDataProvider.java │ │ │ │ ├── DataProviderTest.java │ │ │ │ ├── DataProviderTestProvider.java │ │ │ │ └── MetaAnnotationAcceptanceTest.java │ │ │ ├── resolver │ │ │ │ ├── DataProviderStartWithTestMethodNameResolver.java │ │ │ │ ├── CustomResolverUseDataProvider.java │ │ │ │ ├── CustomResolverUseDataProviderArgumentProvider.java │ │ │ │ └── CustomResolverAcceptanceTest.java │ │ │ ├── converter │ │ │ │ ├── CustomConverterDataProviderArgumentProvider.java │ │ │ │ ├── DateTimeAwareStringConverter.java │ │ │ │ └── CustomConverterDataProvider.java │ │ │ └── placeholder │ │ │ │ └── CustomPlaceholderAcceptanceTest.java │ │ │ ├── external │ │ │ ├── ExternalDataProvider.java │ │ │ └── ExternalDataProviderAcceptanceTest.java │ │ │ ├── TagAcceptanceTest.java │ │ │ ├── FormatAcceptanceTest.java │ │ │ ├── ListAsArgumentAcceptanceTest.java │ │ │ ├── BeforeAcceptanceTest.java │ │ │ ├── NewlineAcceptanceTest.java │ │ │ ├── NameConventionAcceptanceTest.java │ │ │ ├── DataProviderMethodParameterAcceptanceTest.java │ │ │ ├── AdditionalTestMethodParameterAcceptanceTest.java │ │ │ ├── CachedDataProviderResultsGloballyAcceptanceTest.java │ │ │ ├── NestedAcceptanceTest.java │ │ │ ├── NonStaticDataProviderAcceptanceTest.java │ │ │ ├── ListDataProviderAcceptanceTest.java │ │ │ ├── SingleArgumentDataProviderAcceptanceTest.java │ │ │ ├── VarargsAcceptanceTest.java │ │ │ └── CacheDataProviderResultsAcceptanceTest.java │ └── groovy │ │ └── com │ │ └── tngtech │ │ └── test │ │ └── java │ │ └── junit │ │ └── dataprovider │ │ └── GroovyAcceptanceTest.groovy │ └── main │ └── java │ └── com │ └── tngtech │ └── junit │ └── dataprovider │ └── StringDataProviderArgumentProvider.java ├── .github ├── workflows │ ├── gradle-wrapper-validation.yml │ └── gradle.yml ├── ISSUE_TEMPLATE.md └── PULL_REQUEST_TEMPLATE.md ├── settings.gradle.kts ├── NOTICE ├── SECURITY.md ├── core └── src │ ├── test │ └── java │ │ └── com │ │ └── tngtech │ │ └── junit │ │ └── dataprovider │ │ ├── placeholder │ │ ├── IndexPlaceholderTest.java │ │ ├── SimpleMethodNamePlaceholderTest.java │ │ ├── CanonicalMethodNamePlaceholderTest.java │ │ ├── SimpleClassNamePlaceholderTest.java │ │ ├── CanonicalClassNamePlaceholderTest.java │ │ └── CompleteMethodSignaturePlaceholderTest.java │ │ ├── testutils │ │ └── Methods.java │ │ └── convert │ │ └── ConverterContextTest.java │ └── main │ └── java │ └── com │ └── tngtech │ └── junit │ └── dataprovider │ ├── placeholder │ ├── IndexPlaceholder.java │ ├── SimpleMethodNamePlaceholder.java │ ├── CompleteMethodSignaturePlaceholder.java │ ├── SimpleClassNamePlaceholder.java │ ├── CanonicalClassNamePlaceholder.java │ └── ArgumentPlaceholder.java │ ├── resolver │ ├── ResolveStrategy.java │ └── DataProviderMethodResolver.java │ ├── format │ └── DataProviderTestNameFormatter.java │ ├── convert │ └── SingleArgConverter.java │ └── Preconditions.java ├── .travis.yml └── CONTRIBUTING.md /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TNG/junit-dataprovider/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | bin/ 2 | build/ 3 | out/ 4 | .classpath 5 | .gradle 6 | .idea 7 | .settings 8 | .project 9 | 10 | *.iml 11 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # disable TLS v1.3 protocol because of coveralls upload, see https://github.com/kt3k/coveralls-gradle-plugin/issues/85 2 | systemProp.jdk.tls.client.protocols="TLSv1,TLSv1.1,TLSv1.2" 3 | -------------------------------------------------------------------------------- /junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/category/CategoryOne.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.java.junit.dataprovider.category; 2 | 3 | public interface CategoryOne { 4 | // marks test with a @Category 5 | } 6 | -------------------------------------------------------------------------------- /junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/category/CategoryTwo.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.java.junit.dataprovider.category; 2 | 3 | public interface CategoryTwo { 4 | // marks test with a @Category 5 | } 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.6-all.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/override/AcceptanceTestInterface.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.override; 2 | 3 | public interface AcceptanceTestInterface { 4 | 5 | void testToBeImplemented(); 6 | 7 | void testToBeImplemented(T value); 8 | } 9 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/override/AcceptanceTestInterface.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.override; 2 | 3 | public interface AcceptanceTestInterface { 4 | 5 | void testToBeImplemented(); 6 | 7 | void testToBeImplemented(T value); 8 | } 9 | -------------------------------------------------------------------------------- /.github/workflows/gradle-wrapper-validation.yml: -------------------------------------------------------------------------------- 1 | name: "Validate Gradle Wrapper" 2 | on: [push, pull_request] 3 | 4 | jobs: 5 | validation: 6 | name: "Gradle wrapper validation" 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v2 10 | - uses: gradle/wrapper-validation-action@v1 11 | -------------------------------------------------------------------------------- /junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderAcceptanceTestInterface.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.java.junit.dataprovider.override; 2 | 3 | public interface DataProviderAcceptanceTestInterface { 4 | 5 | void testToBeImplemented(); 6 | 7 | void testToBeImplemented(T value); 8 | } 9 | -------------------------------------------------------------------------------- /settings.gradle.kts: -------------------------------------------------------------------------------- 1 | pluginManagement { 2 | repositories { 3 | mavenCentral() // required as first repo for Bnd Gradle Plugins, see also https://github.com/bndtools/bnd/issues/3174 4 | gradlePluginPortal() 5 | } 6 | } 7 | plugins { 8 | id("com.gradle.enterprise").version("3.1.1") 9 | } 10 | include(":core", ":junit4", ":junit-jupiter", ":junit-jupiter-params") 11 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ## Overview 2 | 3 | TODO: Replace the following bullet points with your issue description. 4 | 5 | - ( ) **Bug report.** Please provide the version you are using and a 6 | reproducer, if possible. 7 | 8 | - ( ) **Feature request.** Tell what your use case is and what you have 9 | already tried. 10 | 11 | - ( ) **Question.** Ask your question and state your use case. 12 | 13 | ## Deliverables 14 | 15 | - [ ] ... 16 | -------------------------------------------------------------------------------- /.github/workflows/gradle.yml: -------------------------------------------------------------------------------- 1 | name: Java CI 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ubuntu-latest 9 | 10 | strategy: 11 | matrix: 12 | java-version: [ 8, 11 ] 13 | 14 | steps: 15 | - uses: actions/checkout@v6 16 | - name: Setup JDK 1.${{ matrix.java-version }} 17 | uses: actions/setup-java@v1 18 | with: 19 | java-version: ${{ matrix.java-version }} 20 | - name: Build with Gradle 21 | run: ./gradlew build 22 | -------------------------------------------------------------------------------- /junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/category/CategoryOneTestSuite.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.java.junit.dataprovider.category; 2 | 3 | import org.junit.experimental.categories.Categories; 4 | import org.junit.experimental.categories.Categories.IncludeCategory; 5 | import org.junit.runner.RunWith; 6 | import org.junit.runners.Suite.SuiteClasses; 7 | 8 | @RunWith(Categories.class) 9 | @SuiteClasses(CategoryTest.class) 10 | @IncludeCategory(CategoryOne.class) 11 | public class CategoryOneTestSuite { 12 | // suite to run tests with 13 | } 14 | -------------------------------------------------------------------------------- /junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/category/CategoryTwoTestSuite.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.java.junit.dataprovider.category; 2 | 3 | import org.junit.experimental.categories.Categories; 4 | import org.junit.experimental.categories.Categories.IncludeCategory; 5 | import org.junit.runner.RunWith; 6 | import org.junit.runners.Suite.SuiteClasses; 7 | 8 | @RunWith(Categories.class) 9 | @SuiteClasses(CategoryTest.class) 10 | @IncludeCategory(CategoryTwo.class) 11 | public class CategoryTwoTestSuite { 12 | // suite to run tests with 13 | } 14 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/meta/FixedLocationDataProviderTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.meta; 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 | @Retention(RetentionPolicy.RUNTIME) 9 | @Target({ ElementType.METHOD }) 10 | @DataProviderTest(location = DataProviderLocation.class) 11 | @interface FixedLocationDataProviderTest { 12 | // annotation needs no properties 13 | } -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/meta/FixedLocationDataProviderTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.meta; 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 | @Retention(RetentionPolicy.RUNTIME) 9 | @Target({ ElementType.METHOD }) 10 | @DataProviderTest(location = DataProviderLocation.class) 11 | @interface FixedLocationDataProviderTest { 12 | // annotation needs no properties 13 | } -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | JUnit dataprovider 2 | ------------------ 3 | 4 | Copyright 2016-2017 Pascal Schumacher 5 | Copyright 2016 Moritz Prinz 6 | Copyright 2014 Jan Schäfer 7 | Copyright 2014 Alexei Barantsev 8 | Copyright 2014 Michael Adler 9 | Copyright 2013 Stefan Wolf 10 | Copyright 2013-2018 Andreas Schmid and 11 | Copyright 2013 Leo von Klenze 12 | 13 | This product includes software developed at TNG Technology Consulting GmbH (https://www.tngtech.com/). 14 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/external/ExternalDataProvider.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.external; 2 | 3 | import com.tngtech.junit.dataprovider.DataProvider; 4 | 5 | class ExternalDataProvider { 6 | 7 | @DataProvider 8 | static Object[][] dataProviderIsStringLengthGreaterTwo() { 9 | // @formatter:off 10 | return new Object[][] { 11 | { "", false }, 12 | { "1", false }, 13 | { "12", false }, 14 | { "123", true }, 15 | { "Test", true }, 16 | }; 17 | // @formatter:on 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/StringDataProvider.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.java.junit.dataprovider; 2 | 3 | import com.tngtech.java.junit.dataprovider.DataProvider; 4 | 5 | public class StringDataProvider { 6 | 7 | @DataProvider 8 | public static Object[][] dataProviderIsStringLengthGreaterTwo() { 9 | // @formatter:off 10 | return new Object[][] { 11 | { "", false }, 12 | { "1", false }, 13 | { "12", false }, 14 | { "123", true }, 15 | { "Test", true }, 16 | }; 17 | // @formatter:on 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/external/ExternalDataProvider.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.external; 2 | 3 | import com.tngtech.junit.dataprovider.DataProvider; 4 | 5 | class ExternalDataProvider { 6 | 7 | @DataProvider 8 | static Object[][] dataProviderIsStringLengthGreaterTwo() { 9 | // @formatter:off 10 | return new Object[][] { 11 | { "", false }, 12 | { "1", false }, 13 | { "12", false }, 14 | { "123", true }, 15 | { "Test", true }, 16 | }; 17 | // @formatter:on 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/custom/CustomDataProviderRunner.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.java.junit.dataprovider.custom; 2 | 3 | import org.junit.runners.model.InitializationError; 4 | 5 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 6 | 7 | public class CustomDataProviderRunner extends DataProviderRunner { 8 | 9 | public CustomDataProviderRunner(Class clazz) throws InitializationError { 10 | super(clazz); 11 | } 12 | 13 | @Override 14 | protected void initializeHelpers() { 15 | super.initializeHelpers(); 16 | dataConverter.setStringConverter(new DateTimeAwareStringConverter()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/resolver/DataProviderStartWithTestMethodNameResolver.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.resolver; 2 | 3 | import com.tngtech.junit.dataprovider.resolver.DataProviderMethodResolver; 4 | import com.tngtech.junit.dataprovider.resolver.DefaultDataProviderMethodResolver; 5 | 6 | /** 7 | * {@link DataProviderMethodResolver} which uses all dataproviders prefixed with the name of the {@code testMethod}. 8 | */ 9 | class DataProviderStartWithTestMethodNameResolver extends DefaultDataProviderMethodResolver { 10 | @Override 11 | protected boolean isMatchingNameConvention(String testMethodName, String dataProviderMethodName) { 12 | return dataProviderMethodName.startsWith(testMethodName); 13 | } 14 | } -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/resolver/DataProviderStartWithTestMethodNameResolver.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.resolver; 2 | 3 | import com.tngtech.junit.dataprovider.resolver.DataProviderMethodResolver; 4 | import com.tngtech.junit.dataprovider.resolver.DefaultDataProviderMethodResolver; 5 | 6 | /** 7 | * {@link DataProviderMethodResolver} which uses all dataproviders prefixed with the name of the {@code testMethod}. 8 | */ 9 | class DataProviderStartWithTestMethodNameResolver extends DefaultDataProviderMethodResolver { 10 | @Override 11 | protected boolean isMatchingNameConvention(String testMethodName, String dataProviderMethodName) { 12 | return dataProviderMethodName.startsWith(testMethodName); 13 | } 14 | } -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ## Overview 2 | 3 | TODO: Please describe your changes here and list any open questions you might have. 4 | 5 | --- 6 | 7 | I hereby agree to the terms of the [JUnit dataprovider Contributor License Agreement](https://github.com/tng/junit-dataprovider/blob/master/CONTRIBUTING.md#junit-dataprovider-contributor-license-agreement). 8 | 9 | --- 10 | 11 | ### Definition of Done 12 | 13 | - [ ] There are no TODOs left in the code 14 | - [ ] Method [preconditions](https://github.com/TNG/junit-dataprovider/blob/master/core/src/main/java/com/tngtech/junit/dataprovider/Preconditions.java) are checked and documented in the method's Javadoc 15 | - [ ] Change is covered by automated tests (unit and/or integration tests) 16 | - [ ] [Build](https://travis-ci.org/TNG/junit-dataprovider) has passed 17 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/external/ExternalDataProviderAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.external; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import org.junit.jupiter.params.ParameterizedTest; 6 | 7 | import com.tngtech.junit.dataprovider.UseDataProvider; 8 | 9 | class ExternalDataProviderAcceptanceTest { 10 | 11 | @ParameterizedTest 12 | @UseDataProvider(value = "dataProviderIsStringLengthGreaterTwo", location = ExternalDataProvider.class) 13 | void testIsStringLengthGreaterThanTwo(String str, boolean expected) { 14 | // Given: 15 | 16 | // When: 17 | boolean isGreaterThanTwo = (str != null) && str.length() > 2; 18 | 19 | // Then: 20 | assertThat(isGreaterThanTwo).isEqualTo(expected); 21 | } 22 | } -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/meta/ExtendWithDataProvider.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.meta; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | import org.junit.jupiter.api.extension.ExtendWith; 10 | 11 | import com.tngtech.junit.dataprovider.DataProviderExtension; 12 | import com.tngtech.junit.dataprovider.UseDataProviderExtension; 13 | 14 | @Documented 15 | @Retention(RetentionPolicy.RUNTIME) 16 | @Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE }) 17 | @ExtendWith(DataProviderExtension.class) 18 | @ExtendWith(UseDataProviderExtension.class) 19 | @interface ExtendWithDataProvider { 20 | // meta annotation for all possibilites of using a dataprovider 21 | } 22 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Supported Versions 4 | 5 | 6 | Use this section to tell people about which versions of your project are 7 | currently being supported with security updates. 8 | 9 | | Version | Supported | 10 | | --------- | ------------------ | 11 | | <= 1.12.x | :x: | 12 | | 1.13.x | :heavy_check_mark: | 13 | | <= 2.3 | :x: | 14 | | 2.4 | : | 15 | | 2.5 | :heavy_check_mark: | 16 | | 2.6 | :heavy_check_mark: | 17 | 18 | ## Reporting a Vulnerability 19 | 20 | If you have any security issues / vulnerabilities, please don't hesitate to file an issue. We will consider all issues within 21 | the next few working days and will address it with a fix if applicable. 22 | 23 | ## Third party libraries 24 | 25 | JUnit dataprovider is shipped without a single dependency such that there are no issues in transitive dependencies 26 | and it is up to you to choose a proper JUnit version. 27 | -------------------------------------------------------------------------------- /core/src/test/java/com/tngtech/junit/dataprovider/placeholder/IndexPlaceholderTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.junit.dataprovider.placeholder; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.util.Arrays; 6 | import java.util.Collections; 7 | 8 | import org.junit.Test; 9 | 10 | import com.tngtech.junit.dataprovider.testutils.Methods; 11 | 12 | public class IndexPlaceholderTest { 13 | 14 | private final IndexPlaceholder underTest = new IndexPlaceholder(); 15 | 16 | @Test 17 | public void testProcessShouldReplacePlaceholder() { 18 | // Given: 19 | final int index = 42; 20 | 21 | ReplacementData data = ReplacementData.of(Methods.anyMethod(), index, Collections.singletonList(0)); 22 | 23 | // When: 24 | String result = underTest.process(data, "%i"); 25 | 26 | // Then: 27 | assertThat(result).isEqualTo(String.valueOf(index)); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/override/AbstractAcceptanceBaseTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.override; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | import static org.assertj.core.api.Assertions.fail; 5 | 6 | import org.junit.jupiter.api.TestTemplate; 7 | 8 | import com.tngtech.junit.dataprovider.DataProvider; 9 | import com.tngtech.junit.dataprovider.UseDataProvider; 10 | 11 | abstract class AbstractAcceptanceBaseTest { 12 | 13 | @DataProvider 14 | static Object[][] dataProviderBase() { 15 | return new Object[][] { { "1" } }; 16 | } 17 | 18 | @TestTemplate 19 | @UseDataProvider 20 | void testBase(@SuppressWarnings("unused") String one) { 21 | fail("should be overridden and therefore not fail"); 22 | } 23 | 24 | @TestTemplate 25 | @UseDataProvider 26 | public void testChild(String one) { 27 | assertThat(one).isEqualTo("1"); 28 | } 29 | } -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/override/OverridingAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.override; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import org.junit.jupiter.params.ParameterizedTest; 6 | 7 | import com.tngtech.junit.dataprovider.DataProvider; 8 | import com.tngtech.junit.dataprovider.UseDataProvider; 9 | 10 | class OverridingAcceptanceTest extends AbstractAcceptanceBaseTest { 11 | 12 | @Override 13 | @ParameterizedTest 14 | @UseDataProvider 15 | void testBase(String one) { 16 | assertThat(one).isEqualTo("1"); 17 | } 18 | 19 | @ParameterizedTest 20 | @UseDataProvider("dataProviderBase") 21 | public void testBaseNotOverridden(String one) { 22 | assertThat(one).isEqualTo("1"); 23 | } 24 | 25 | @DataProvider 26 | public static Object[][] dataProviderChild() { 27 | return new Object[][] { { "1" } }; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/override/AbstractAcceptanceBaseTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.override; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | import static org.assertj.core.api.Assertions.fail; 5 | 6 | import org.junit.jupiter.params.ParameterizedTest; 7 | 8 | import com.tngtech.junit.dataprovider.DataProvider; 9 | import com.tngtech.junit.dataprovider.UseDataProvider; 10 | 11 | abstract class AbstractAcceptanceBaseTest { 12 | 13 | @DataProvider 14 | static Object[][] dataProviderBase() { 15 | return new Object[][] { { "1" } }; 16 | } 17 | 18 | @ParameterizedTest 19 | @UseDataProvider 20 | void testBase(@SuppressWarnings("unused") String one) { 21 | fail("should be overridden and therefore not fail"); 22 | } 23 | 24 | @ParameterizedTest 25 | @UseDataProvider 26 | public void testChild(String one) { 27 | assertThat(one).isEqualTo("1"); 28 | } 29 | } -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/meta/StringDataProviderProvider.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.meta; 2 | 3 | import com.tngtech.junit.dataprovider.AbstractStringDataProviderArgumentProvider; 4 | import com.tngtech.junit.dataprovider.convert.ConverterContext; 5 | 6 | class StringDataProviderProvider extends AbstractStringDataProviderArgumentProvider { 7 | 8 | @Override 9 | public void accept(StringDataProvider sourceAnnotation) { 10 | this.sourceAnnotation = sourceAnnotation; 11 | } 12 | 13 | @Override 14 | protected Object getData(StringDataProvider annotation) { 15 | return annotation.value(); 16 | } 17 | 18 | @Override 19 | protected ConverterContext getConverterContext(StringDataProvider annotation) { 20 | return new ConverterContext(annotation.splitBy(), annotation.convertNulls(), annotation.trimValues(), 21 | annotation.ignoreEnumCase()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /junit4/src/test/java/com/tngtech/java/junit/dataprovider/internal/placeholder/IndexPlaceholderTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.java.junit.dataprovider.internal.placeholder; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.mockito.InjectMocks; 8 | import org.mockito.junit.MockitoJUnitRunner; 9 | 10 | import com.tngtech.java.junit.dataprovider.BaseTest; 11 | 12 | @RunWith(MockitoJUnitRunner.class) 13 | public class IndexPlaceholderTest extends BaseTest { 14 | 15 | @InjectMocks 16 | private IndexPlaceholder underTest; 17 | 18 | @Test 19 | public void testProcessShouldReplacePlaceholder() { 20 | // Given: 21 | final int index = 42; 22 | 23 | underTest.setContext(anyMethod(), index, new Object[] { 0 }); 24 | 25 | // When: 26 | String result = underTest.process("%i"); 27 | 28 | // Then: 29 | assertThat(result).isEqualTo(String.valueOf(index)); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /core/src/test/java/com/tngtech/junit/dataprovider/placeholder/SimpleMethodNamePlaceholderTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.junit.dataprovider.placeholder; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.lang.reflect.Method; 6 | import java.util.Arrays; 7 | import java.util.Collections; 8 | 9 | import org.junit.Test; 10 | 11 | import com.tngtech.junit.dataprovider.testutils.Methods; 12 | 13 | public class SimpleMethodNamePlaceholderTest { 14 | 15 | private final SimpleMethodNamePlaceholder underTest = new SimpleMethodNamePlaceholder(); 16 | 17 | @Test 18 | public void testProcessShouldReplacePlaceholder() { 19 | // Given: 20 | final Method method = Methods.anyMethod(); 21 | 22 | ReplacementData data = ReplacementData.of(method, 0, Collections.singletonList(0)); 23 | 24 | // When: 25 | String result = underTest.process(data, "%m"); 26 | 27 | // Then: 28 | assertThat(result).isEqualTo(method.getName()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /core/src/test/java/com/tngtech/junit/dataprovider/placeholder/CanonicalMethodNamePlaceholderTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.junit.dataprovider.placeholder; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.lang.reflect.Method; 6 | import java.util.Arrays; 7 | import java.util.Collections; 8 | 9 | import org.junit.Test; 10 | 11 | import com.tngtech.junit.dataprovider.testutils.Methods; 12 | 13 | public class CanonicalMethodNamePlaceholderTest { 14 | 15 | private final CompleteMethodSignaturePlaceholder underTest = new CompleteMethodSignaturePlaceholder(); 16 | 17 | @Test 18 | public void testProcessShouldReplacePlaceholder() { 19 | // Given: 20 | final Method method = Methods.anyMethod(); 21 | 22 | ReplacementData data = ReplacementData.of(method, 0, Collections.singletonList(0)); 23 | 24 | // When: 25 | String result = underTest.process(data, "%cm"); 26 | 27 | // Then: 28 | assertThat(result).isEqualTo(method.toString()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /core/src/test/java/com/tngtech/junit/dataprovider/placeholder/SimpleClassNamePlaceholderTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.junit.dataprovider.placeholder; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.lang.reflect.Method; 6 | import java.util.Arrays; 7 | import java.util.Collections; 8 | 9 | import org.junit.Test; 10 | 11 | import com.tngtech.junit.dataprovider.testutils.Methods; 12 | 13 | public class SimpleClassNamePlaceholderTest { 14 | 15 | private final SimpleClassNamePlaceholder underTest = new SimpleClassNamePlaceholder(); 16 | 17 | @Test 18 | public void testProcessShouldReplacePlaceholder() { 19 | // Given: 20 | final Method method = Methods.anyMethod(); 21 | 22 | ReplacementData data = ReplacementData.of(method, 0, Collections.singletonList(0)); 23 | 24 | // When: 25 | String result = underTest.process(data, "%c"); 26 | 27 | // Then: 28 | assertThat(result).isEqualTo(method.getDeclaringClass().getSimpleName()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /core/src/test/java/com/tngtech/junit/dataprovider/placeholder/CanonicalClassNamePlaceholderTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.junit.dataprovider.placeholder; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.lang.reflect.Method; 6 | import java.util.Arrays; 7 | import java.util.Collections; 8 | 9 | import org.junit.Test; 10 | 11 | import com.tngtech.junit.dataprovider.testutils.Methods; 12 | 13 | public class CanonicalClassNamePlaceholderTest { 14 | 15 | private final CanonicalClassNamePlaceholder underTest = new CanonicalClassNamePlaceholder(); 16 | 17 | @Test 18 | public void testProcessShouldReplacePlaceholder() { 19 | // Given: 20 | final Method method = Methods.anyMethod(); 21 | ReplacementData data = ReplacementData.of(method, 0, Collections.singletonList(0)); 22 | 23 | // When: 24 | String result = underTest.process(data, "%cc"); 25 | 26 | // Then: 27 | assertThat(result).isEqualTo(method.getDeclaringClass().getCanonicalName()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /core/src/test/java/com/tngtech/junit/dataprovider/placeholder/CompleteMethodSignaturePlaceholderTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.junit.dataprovider.placeholder; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.lang.reflect.Method; 6 | import java.util.Arrays; 7 | import java.util.Collections; 8 | 9 | import org.junit.Test; 10 | 11 | import com.tngtech.junit.dataprovider.testutils.Methods; 12 | 13 | public class CompleteMethodSignaturePlaceholderTest { 14 | 15 | private final CompleteMethodSignaturePlaceholder underTest = new CompleteMethodSignaturePlaceholder(); 16 | 17 | @Test 18 | public void testProcessShouldReplacePlaceholder() { 19 | // Given: 20 | final Method method = Methods.anyMethod(); 21 | 22 | ReplacementData data = ReplacementData.of(method, 0, Collections.singletonList(0)); 23 | 24 | // When: 25 | String result = underTest.process(data, "%cm"); 26 | 27 | // Then: 28 | assertThat(result).isEqualTo(method.toString()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | 3 | os: linux 4 | dist: trusty 5 | sudo: false 6 | 7 | jdk: 8 | - openjdk8 9 | - oraclejdk8 10 | - oraclejdk9 11 | - oraclejdk11 12 | 13 | script: 14 | - ./gradlew --scan build -Pjunit4Version=4.10 -PjunitJupiterVersion=5.9.0 -PskipSpotBugs 15 | - ./gradlew --scan build -Pjunit4Version=4.11 -PjunitJupiterVersion=5.9.0 -PskipSpotBugs 16 | - ./gradlew --scan build -Pjunit4Version=4.12 -PjunitJupiterVersion=5.9.0 -PskipSpotBugs 17 | - ./gradlew --scan build 18 | 19 | - cd ${TRAVIS_BUILD_DIR}/junit4/ 20 | - mvn test 21 | # - ant test 22 | 23 | - cd ${TRAVIS_BUILD_DIR}/junit-jupiter/ 24 | - mvn test 25 | 26 | - cd ${TRAVIS_BUILD_DIR}/junit-jupiter-params/ 27 | - mvn test 28 | 29 | before_cache: 30 | - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock 31 | - rm -fr $HOME/.gradle/caches/*/plugin-resolution/ 32 | 33 | cache: 34 | directories: 35 | - $HOME/.gradle/caches/ 36 | - $HOME/.gradle/wrapper/ 37 | 38 | after_success: 39 | - cd ${TRAVIS_BUILD_DIR} 40 | - ./gradlew --scan jacocoRootReport coveralls 41 | -------------------------------------------------------------------------------- /junit4/src/test/java/com/tngtech/java/junit/dataprovider/internal/placeholder/SimpleMethodNamePlaceholderTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.java.junit.dataprovider.internal.placeholder; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.lang.reflect.Method; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.mockito.InjectMocks; 10 | import org.mockito.junit.MockitoJUnitRunner; 11 | 12 | import com.tngtech.java.junit.dataprovider.BaseTest; 13 | 14 | @RunWith(MockitoJUnitRunner.class) 15 | public class SimpleMethodNamePlaceholderTest extends BaseTest { 16 | 17 | @InjectMocks 18 | private SimpleMethodNamePlaceholder underTest; 19 | 20 | @Test 21 | public void testProcessShouldReplacePlaceholder() { 22 | // Given: 23 | final Method method = anyMethod(); 24 | 25 | underTest.setContext(method, 0, new Object[] { 0 }); 26 | 27 | // When: 28 | String result = underTest.process("%m"); 29 | 30 | // Then: 31 | assertThat(result).isEqualTo(method.getName()); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /junit4/src/test/java/com/tngtech/java/junit/dataprovider/internal/placeholder/CanonicalMethodNamePlaceholderTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.java.junit.dataprovider.internal.placeholder; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.lang.reflect.Method; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.mockito.InjectMocks; 10 | import org.mockito.junit.MockitoJUnitRunner; 11 | 12 | import com.tngtech.java.junit.dataprovider.BaseTest; 13 | 14 | @RunWith(MockitoJUnitRunner.class) 15 | public class CanonicalMethodNamePlaceholderTest extends BaseTest { 16 | 17 | @InjectMocks 18 | private CompleteMethodSignaturePlaceholder underTest; 19 | 20 | @Test 21 | public void testProcessShouldReplacePlaceholder() { 22 | // Given: 23 | final Method method = anyMethod(); 24 | 25 | underTest.setContext(method, 0, new Object[] { 0 }); 26 | 27 | // When: 28 | String result = underTest.process("%cm"); 29 | 30 | // Then: 31 | assertThat(result).isEqualTo(method.toString()); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/external/ExternalDataProviderAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.external; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import org.junit.jupiter.api.TestTemplate; 6 | import org.junit.jupiter.api.extension.ExtendWith; 7 | 8 | import com.tngtech.junit.dataprovider.DataProviderExtension; 9 | import com.tngtech.junit.dataprovider.UseDataProvider; 10 | import com.tngtech.junit.dataprovider.UseDataProviderExtension; 11 | 12 | @ExtendWith(DataProviderExtension.class) 13 | @ExtendWith(UseDataProviderExtension.class) 14 | class ExternalDataProviderAcceptanceTest { 15 | 16 | @TestTemplate 17 | @UseDataProvider(value = "dataProviderIsStringLengthGreaterTwo", location = ExternalDataProvider.class) 18 | void testIsStringLengthGreaterThanTwo(String str, boolean expected) { 19 | // Given: 20 | 21 | // When: 22 | boolean isGreaterThanTwo = (str != null) && str.length() > 2; 23 | 24 | // Then: 25 | assertThat(isGreaterThanTwo).isEqualTo(expected); 26 | } 27 | } -------------------------------------------------------------------------------- /junit4/src/test/java/com/tngtech/java/junit/dataprovider/internal/placeholder/SimpleClassNamePlaceholderTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.java.junit.dataprovider.internal.placeholder; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.lang.reflect.Method; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.mockito.InjectMocks; 10 | import org.mockito.junit.MockitoJUnitRunner; 11 | 12 | import com.tngtech.java.junit.dataprovider.BaseTest; 13 | 14 | @RunWith(MockitoJUnitRunner.class) 15 | public class SimpleClassNamePlaceholderTest extends BaseTest { 16 | 17 | @InjectMocks 18 | private SimpleClassNamePlaceholder underTest; 19 | 20 | @Test 21 | public void testProcessShouldReplacePlaceholder() { 22 | // Given: 23 | final Method method = anyMethod(); 24 | 25 | underTest.setContext(method, 0, new Object[] { 0 }); 26 | 27 | // When: 28 | String result = underTest.process("%c"); 29 | 30 | // Then: 31 | assertThat(result).isEqualTo(method.getDeclaringClass().getSimpleName()); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /core/src/main/java/com/tngtech/junit/dataprovider/placeholder/IndexPlaceholder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 TNG Technology Consulting GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.tngtech.junit.dataprovider.placeholder; 17 | 18 | public class IndexPlaceholder extends BasePlaceholder { 19 | public IndexPlaceholder() { 20 | super("%i"); 21 | } 22 | 23 | @Override 24 | protected String getReplacementFor(String placeholder, ReplacementData data) { 25 | return String.valueOf(data.getTestIndex()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderOverridingAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.java.junit.dataprovider.override; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | 8 | import com.tngtech.java.junit.dataprovider.DataProvider; 9 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 10 | import com.tngtech.java.junit.dataprovider.UseDataProvider; 11 | 12 | @RunWith(DataProviderRunner.class) 13 | public class DataProviderOverridingAcceptanceTest extends DataProviderAbstractAcceptanceBaseTest { 14 | 15 | @Override 16 | @Test 17 | @UseDataProvider 18 | public void testBase(String one) { 19 | assertThat(one).isEqualTo("1"); 20 | } 21 | 22 | @Test 23 | @UseDataProvider("dataProviderBase") 24 | public void testBaseNotOverridden(String one) { 25 | assertThat(one).isEqualTo("1"); 26 | } 27 | 28 | @DataProvider 29 | public static Object[][] dataProviderChild() { 30 | return new Object[][] { { "1" } }; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /junit4/src/test/java/com/tngtech/java/junit/dataprovider/internal/placeholder/CanonicalClassNamePlaceholderTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.java.junit.dataprovider.internal.placeholder; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.lang.reflect.Method; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.mockito.InjectMocks; 10 | import org.mockito.junit.MockitoJUnitRunner; 11 | 12 | import com.tngtech.java.junit.dataprovider.BaseTest; 13 | 14 | @RunWith(MockitoJUnitRunner.class) 15 | public class CanonicalClassNamePlaceholderTest extends BaseTest { 16 | 17 | @InjectMocks 18 | private CanonicalClassNamePlaceholder underTest; 19 | 20 | @Test 21 | public void testProcessShouldReplacePlaceholder() { 22 | // Given: 23 | final Method method = anyMethod(); 24 | 25 | underTest.setContext(method, 0, new Object[] { 0 }); 26 | 27 | // When: 28 | String result = underTest.process("%cc"); 29 | 30 | // Then: 31 | assertThat(result).isEqualTo(method.getDeclaringClass().getCanonicalName()); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/override/OverridingAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.override; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import org.junit.jupiter.api.TestTemplate; 6 | import org.junit.jupiter.api.extension.ExtendWith; 7 | 8 | import com.tngtech.junit.dataprovider.DataProvider; 9 | import com.tngtech.junit.dataprovider.UseDataProvider; 10 | import com.tngtech.junit.dataprovider.UseDataProviderExtension; 11 | 12 | @ExtendWith(UseDataProviderExtension.class) 13 | class OverridingAcceptanceTest extends AbstractAcceptanceBaseTest { 14 | 15 | @Override 16 | @TestTemplate 17 | @UseDataProvider 18 | void testBase(String one) { 19 | assertThat(one).isEqualTo("1"); 20 | } 21 | 22 | @TestTemplate 23 | @UseDataProvider("dataProviderBase") 24 | public void testBaseNotOverridden(String one) { 25 | assertThat(one).isEqualTo("1"); 26 | } 27 | 28 | @DataProvider 29 | public static Object[][] dataProviderChild() { 30 | return new Object[][] { { "1" } }; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /core/src/main/java/com/tngtech/junit/dataprovider/placeholder/SimpleMethodNamePlaceholder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 TNG Technology Consulting GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.tngtech.junit.dataprovider.placeholder; 17 | 18 | public class SimpleMethodNamePlaceholder extends BasePlaceholder { 19 | public SimpleMethodNamePlaceholder() { 20 | super("%m"); 21 | } 22 | 23 | @Override 24 | protected String getReplacementFor(String placeholder, ReplacementData data) { 25 | return data.getTestMethod().getName(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /core/src/main/java/com/tngtech/junit/dataprovider/placeholder/CompleteMethodSignaturePlaceholder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 TNG Technology Consulting GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.tngtech.junit.dataprovider.placeholder; 17 | 18 | public class CompleteMethodSignaturePlaceholder extends BasePlaceholder { 19 | public CompleteMethodSignaturePlaceholder() { 20 | super("%cm"); 21 | } 22 | 23 | @Override 24 | protected String getReplacementFor(String placeholder, ReplacementData data) { 25 | return data.getTestMethod().toString(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /core/src/main/java/com/tngtech/junit/dataprovider/placeholder/SimpleClassNamePlaceholder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 TNG Technology Consulting GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.tngtech.junit.dataprovider.placeholder; 17 | 18 | public class SimpleClassNamePlaceholder extends BasePlaceholder { 19 | public SimpleClassNamePlaceholder() { 20 | super("%c"); 21 | } 22 | 23 | @Override 24 | protected String getReplacementFor(String placeholder, ReplacementData data) { 25 | return data.getTestMethod().getDeclaringClass().getSimpleName(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /core/src/main/java/com/tngtech/junit/dataprovider/placeholder/CanonicalClassNamePlaceholder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 TNG Technology Consulting GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.tngtech.junit.dataprovider.placeholder; 17 | 18 | public class CanonicalClassNamePlaceholder extends BasePlaceholder { 19 | public CanonicalClassNamePlaceholder() { 20 | super("%cc"); 21 | } 22 | 23 | @Override 24 | protected String getReplacementFor(String placeholder, ReplacementData data) { 25 | return data.getTestMethod().getDeclaringClass().getCanonicalName(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /core/src/main/java/com/tngtech/junit/dataprovider/resolver/ResolveStrategy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 TNG Technology Consulting GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.tngtech.junit.dataprovider.resolver; 17 | 18 | public enum ResolveStrategy { 19 | /** 20 | * Strategy which stops after the first resolver returns valid dataproviders (= non-empty list) or no further 21 | * resolvers are available. 22 | */ 23 | UNTIL_FIRST_MATCH, 24 | 25 | /** 26 | * Loops over all resolvers and aggregates all resulting dataproviders. 27 | */ 28 | AGGREGATE_ALL_MATCHES, 29 | } -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/meta/DataProviderLocation.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.meta; 2 | 3 | import static com.tngtech.junit.dataprovider.DataProviders.$; 4 | import static com.tngtech.junit.dataprovider.DataProviders.$$; 5 | 6 | import com.tngtech.junit.dataprovider.DataProvider; 7 | 8 | class DataProviderLocation { 9 | 10 | @DataProvider 11 | static Object[][] dataProviderAdd() { 12 | //@formatter:off 13 | return new Object[][] { 14 | { 0, 0, 0 }, 15 | { 0, 1, 1 }, 16 | { 1, 0, 1 }, 17 | { 1, 1, 2 }, 18 | 19 | { 0, -1, -1 }, 20 | { -1, -1, -2 }, 21 | }; 22 | //@formatter:on 23 | } 24 | 25 | @DataProvider 26 | static Object[][] dataProviderMinus() { 27 | // @formatter:off 28 | return $$( 29 | $( 0, 0, 0 ), 30 | $( 0, 1, -1 ), 31 | $( 0, -1, 1 ), 32 | $( 1, 0, 1 ), 33 | $( 1, 1, 0 ), 34 | $( -1, 0, -1 ), 35 | $( -1, -1, 0 ) 36 | ); 37 | // @formatter:on 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/meta/DataProviderLocation.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.meta; 2 | 3 | import static com.tngtech.junit.dataprovider.DataProviders.$; 4 | import static com.tngtech.junit.dataprovider.DataProviders.$$; 5 | 6 | import com.tngtech.junit.dataprovider.DataProvider; 7 | 8 | class DataProviderLocation { 9 | 10 | @DataProvider 11 | static Object[][] dataProviderAdd() { 12 | //@formatter:off 13 | return new Object[][] { 14 | { 0, 0, 0 }, 15 | { 0, 1, 1 }, 16 | { 1, 0, 1 }, 17 | { 1, 1, 2 }, 18 | 19 | { 0, -1, -1 }, 20 | { -1, -1, -2 }, 21 | }; 22 | //@formatter:on 23 | } 24 | 25 | @DataProvider 26 | static Object[][] dataProviderMinus() { 27 | // @formatter:off 28 | return $$( 29 | $( 0, 0, 0 ), 30 | $( 0, 1, -1 ), 31 | $( 0, -1, 1 ), 32 | $( 1, 0, 1 ), 33 | $( 1, 1, 0 ), 34 | $( -1, 0, -1 ), 35 | $( -1, -1, 0 ) 36 | ); 37 | // @formatter:on 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/meta/StringDataProviderExtension.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.meta; 2 | 3 | import com.tngtech.junit.dataprovider.DataProviderInvocationContextProvider; 4 | import com.tngtech.junit.dataprovider.DisplayNameContext; 5 | import com.tngtech.junit.dataprovider.convert.ConverterContext; 6 | 7 | class StringDataProviderExtension extends DataProviderInvocationContextProvider { 8 | 9 | StringDataProviderExtension() { 10 | super(StringDataProvider.class); 11 | } 12 | 13 | @Override 14 | protected Object getData(StringDataProvider annotation) { 15 | return annotation.value(); 16 | } 17 | 18 | @Override 19 | protected ConverterContext getConverterContext(StringDataProvider annotation) { 20 | return new ConverterContext(annotation.splitBy(), annotation.convertNulls(), annotation.trimValues(), 21 | annotation.ignoreEnumCase()); 22 | } 23 | 24 | @Override 25 | protected DisplayNameContext getDisplayNameContext(StringDataProvider annotation) { 26 | return new DisplayNameContext(annotation.formatter(), annotation.format(), getDefaultPlaceholders()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderAbstractAcceptanceBaseTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.java.junit.dataprovider.override; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | import static org.assertj.core.api.Assertions.fail; 5 | 6 | import org.junit.Test; 7 | 8 | import com.tngtech.java.junit.dataprovider.DataProvider; 9 | import com.tngtech.java.junit.dataprovider.UseDataProvider; 10 | 11 | public abstract class DataProviderAbstractAcceptanceBaseTest { 12 | @DataProvider 13 | public static Object[][] dataProviderBase() { 14 | return new Object[][] { { "1" } }; 15 | } 16 | 17 | @Test 18 | @UseDataProvider 19 | public void testBase(@SuppressWarnings("unused") String one) { 20 | fail("should be overridden and therefore not fail"); 21 | } 22 | 23 | // Does not work since v1.12.0 any more as new dataprovider resolver mechanism uses "Method#getDeclaringClass()" 24 | // instead of "testClass" (= JUnit4 Frameworks "TestClass"); Workaround by specifying "location" explicitly ... 25 | @Test 26 | @UseDataProvider(location = DataProviderOverridingAcceptanceTest.class) 27 | public void testChild(String one) { 28 | assertThat(one).isEqualTo("1"); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/TagAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import org.junit.jupiter.api.Tag; 6 | import org.junit.jupiter.api.Test; 7 | import org.junit.jupiter.params.ParameterizedTest; 8 | 9 | import com.tngtech.junit.dataprovider.DataProvider; 10 | import com.tngtech.junit.dataprovider.UseDataProvider; 11 | 12 | @Tag("two") 13 | class TagAcceptanceTest { 14 | 15 | @Test 16 | void testNone() { 17 | // Expect: 18 | assertThat("none").hasSize(4); 19 | } 20 | 21 | @Tag("one") 22 | @Test 23 | void testOne() { 24 | // Expect: 25 | assertThat("one").hasSize(3); 26 | } 27 | 28 | @DataProvider 29 | static Object[][] dataProvider() { 30 | // @formatter:off 31 | return new Object[][] { 32 | { "", 0 }, 33 | { "1", 1 }, 34 | { "12", 2 }, 35 | }; 36 | // @formatter:on 37 | } 38 | 39 | @Tag("one") 40 | @ParameterizedTest 41 | @UseDataProvider("dataProvider") 42 | void test(String string, int expectedLength) { 43 | // Expect: 44 | assertThat(string).hasSize(expectedLength); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/FormatAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import org.junit.jupiter.params.ParameterizedTest; 6 | 7 | import com.tngtech.junit.dataprovider.DataProvider; 8 | import com.tngtech.junit.dataprovider.UseDataProvider; 9 | 10 | class FormatAcceptanceTest { 11 | 12 | @DataProvider // TODO original: (format = "%a[0] * %a[1] == %a[2]") 13 | static Object[][] dataProviderMultiply() { 14 | // @formatter:off 15 | return new Object[][] { 16 | { 0, 0, 0 }, 17 | { -1, 0, 0 }, 18 | { 0, 1, 0 }, 19 | { 1, 1, 1 }, 20 | { 1, -1, -1 }, 21 | { -1, -1, 1 }, 22 | { 1, 2, 2 }, 23 | { -1, 2, -2 }, 24 | { -1, -2, 2 }, 25 | { -1, -2, 2 }, 26 | { 6, 7, 42 }, 27 | }; 28 | // @formatter:on 29 | } 30 | 31 | @ParameterizedTest(name = "{0} * {1} == {2}") 32 | @UseDataProvider("dataProviderMultiply") 33 | void testMultiply(int a, int b, int expected) { 34 | // Expect: 35 | assertThat(a * b).isEqualTo(expected); 36 | } 37 | } -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/placeholder/StripArgumentLengthPlaceholder.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.placeholder; 2 | 3 | import java.util.List; 4 | 5 | import com.tngtech.junit.dataprovider.placeholder.ArgumentPlaceholder; 6 | 7 | class StripArgumentLengthPlaceholder extends ArgumentPlaceholder { 8 | private final int maxLength; 9 | 10 | StripArgumentLengthPlaceholder(int maxLength) { 11 | this.maxLength = maxLength; 12 | } 13 | 14 | @Override 15 | protected String formatAll(List arguments) { 16 | StringBuilder stringBuilder = new StringBuilder(); 17 | for (int i = 0; i < arguments.size(); i++) { 18 | String formattedParameter = format(arguments.get(i)); 19 | if (formattedParameter.length() > maxLength) { 20 | stringBuilder.append(formattedParameter, 0, maxLength - 5); 21 | stringBuilder.append("..."); 22 | stringBuilder.append(formattedParameter.substring(formattedParameter.length() - 2)); 23 | } else { 24 | stringBuilder.append(formattedParameter); 25 | } 26 | if (i < arguments.size() - 1) { 27 | stringBuilder.append(", "); 28 | } 29 | } 30 | return stringBuilder.toString(); 31 | } 32 | } -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/placeholder/CustomPlaceholderAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.placeholder; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import org.junit.jupiter.api.TestTemplate; 6 | import org.junit.jupiter.api.extension.ExtendWith; 7 | 8 | import com.tngtech.junit.dataprovider.DataProvider; 9 | 10 | @ExtendWith(CustomPlaceholderDataProviderExtension.class) 11 | class CustomPlaceholderAcceptanceTest { 12 | 13 | // @formatter:off 14 | @TestTemplate 15 | @DataProvider({ 16 | "veryVeryLongMethodNameWhichMustBeStripped, null, false", 17 | "veryVeryLongMethodNameWhichMustBeStripped, , false", 18 | "veryVeryLongMethodNameWhichMustBeStripped, veryVeryLongMethodNameWhichMustBeStripped, true", 19 | "veryverylongmethodnamewhichmustbestripped, veryVeryLongMethodNameWhichMustBeStripped, true", 20 | "veryVeryLongMethodNameWhichMustBeStripped, veryverylongmethodnamewhichmustbestripped, true" 21 | }) 22 | // @formatter:on 23 | void testEqualsIgnoreCase(String methodName1, String methodName2, boolean expected) { 24 | // Expected: 25 | assertThat(methodName1.equalsIgnoreCase(methodName2)).isEqualTo(expected); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/ListAsArgumentAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | import org.junit.jupiter.params.ParameterizedTest; 9 | 10 | import com.tngtech.junit.dataprovider.DataProvider; 11 | import com.tngtech.junit.dataprovider.UseDataProvider; 12 | 13 | class ListAsArgumentAcceptanceTest { 14 | 15 | @DataProvider 16 | static Object[][] dataProviderListArg() { 17 | // @formatter:off 18 | return new Object[][] { 19 | { Arrays.asList("a", "b"), "c" }, 20 | }; 21 | // @formatter:on 22 | } 23 | 24 | @ParameterizedTest 25 | @UseDataProvider 26 | void testListArg(List list, String string) { 27 | // Expected: 28 | assertThat(list).doesNotContain(string); 29 | } 30 | 31 | @DataProvider 32 | static Object[][] stringsData() { 33 | return new Object[][] { 34 | { Arrays.asList("string1", "stringValue"), "stringValue" } 35 | }; 36 | } 37 | 38 | @ParameterizedTest 39 | @UseDataProvider("stringsData") 40 | void test(List strings, String expectedValue) { 41 | // Expected: 42 | assertThat(strings).contains(expectedValue); 43 | } 44 | } -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/meta/StringDataProvider.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.meta; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | import org.junit.jupiter.params.ParameterizedTest; 10 | import org.junit.jupiter.params.provider.ArgumentsSource; 11 | 12 | import com.tngtech.junit.dataprovider.DataProvider; 13 | 14 | /** 15 | * @see DataProvider 16 | */ 17 | @Documented 18 | @Retention(RetentionPolicy.RUNTIME) 19 | @Target(ElementType.METHOD) 20 | @ParameterizedTest 21 | @ArgumentsSource(StringDataProviderProvider.class) 22 | @interface StringDataProvider { 23 | /** 24 | * @see DataProvider#value() 25 | */ 26 | String[] value() default {}; 27 | 28 | /** 29 | * @see DataProvider#splitBy() 30 | */ 31 | String splitBy() default "\\|"; 32 | 33 | /** 34 | * @see DataProvider#convertNulls() 35 | */ 36 | boolean convertNulls() default true; 37 | 38 | /** 39 | * @see DataProvider#trimValues() 40 | */ 41 | boolean trimValues() default true; 42 | 43 | /** 44 | * @see DataProvider#ignoreEnumCase() 45 | */ 46 | boolean ignoreEnumCase() default false; 47 | } 48 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/converter/CustomConverterDataProviderArgumentProvider.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.converter; 2 | 3 | import org.junit.platform.commons.support.ReflectionSupport; 4 | 5 | import com.tngtech.junit.dataprovider.AbstractStringDataProviderArgumentProvider; 6 | import com.tngtech.junit.dataprovider.convert.ConverterContext; 7 | 8 | class CustomConverterDataProviderArgumentProvider 9 | extends AbstractStringDataProviderArgumentProvider { 10 | 11 | @Override 12 | public void accept(CustomConverterDataProvider sourceAnnotation) { 13 | this.sourceAnnotation = sourceAnnotation; 14 | } 15 | 16 | @Override 17 | protected Object getData(CustomConverterDataProvider annotation) { 18 | return annotation.value(); 19 | } 20 | 21 | @Override 22 | protected ConverterContext getConverterContext(CustomConverterDataProvider annotation) { 23 | return new ConverterContext(ReflectionSupport.newInstance(annotation.objectArrayConverter()), 24 | ReflectionSupport.newInstance(annotation.singleArgConverter()), 25 | ReflectionSupport.newInstance(annotation.stringConverter()), annotation.splitBy(), 26 | annotation.convertNulls(), annotation.trimValues(), annotation.ignoreEnumCase()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/placeholder/CustomPlaceholderAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.placeholder; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import org.junit.jupiter.params.ParameterizedTest; 6 | 7 | import com.tngtech.junit.dataprovider.DataProvider; 8 | 9 | // Note: no pendent in junit-jupiter-params to use custom placeholder / formatter such that arguments are normally displayed 10 | class CustomPlaceholderAcceptanceTest { 11 | 12 | // @formatter:off 13 | @ParameterizedTest 14 | @DataProvider({ 15 | "veryVeryLongMethodNameWhichMustBeStripped, null, false", 16 | "veryVeryLongMethodNameWhichMustBeStripped, , false", 17 | "veryVeryLongMethodNameWhichMustBeStripped, veryVeryLongMethodNameWhichMustBeStripped, true", 18 | "veryverylongmethodnamewhichmustbestripped, veryVeryLongMethodNameWhichMustBeStripped, true", 19 | "veryVeryLongMethodNameWhichMustBeStripped, veryverylongmethodnamewhichmustbestripped, true" 20 | }) 21 | // @formatter:on 22 | void testEqualsIgnoreCase(String methodName1, String methodName2, boolean expected) { 23 | // Expected: 24 | assertThat(methodName1.equalsIgnoreCase(methodName2)).isEqualTo(expected); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/custom/DateTimeAwareStringConverter.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.java.junit.dataprovider.custom; 2 | 3 | import java.text.DateFormat; 4 | import java.text.ParseException; 5 | import java.text.SimpleDateFormat; 6 | import java.util.Date; 7 | 8 | import com.tngtech.java.junit.dataprovider.DataProvider; 9 | import com.tngtech.java.junit.dataprovider.internal.convert.StringConverter; 10 | 11 | public class DateTimeAwareStringConverter extends StringConverter { 12 | 13 | private final DateFormat dateTimeIso; 14 | private final DateFormat dateIso; 15 | 16 | public DateTimeAwareStringConverter() { 17 | dateTimeIso = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS z"); 18 | dateIso = new SimpleDateFormat("yyyy-MM-dd"); 19 | } 20 | 21 | @Override 22 | protected Object customConvertValue(String str, Class targetType, DataProvider dataProvider) { 23 | if (Date.class.equals(targetType)) { 24 | try { 25 | return dateTimeIso.parse(str); 26 | } catch (ParseException e) { 27 | // ignore 28 | } 29 | try { 30 | return dateIso.parse(str); 31 | } catch (ParseException e) { 32 | // ignore 33 | } 34 | } 35 | return super.customConvertValue(str, targetType, dataProvider); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/override/ImplementingAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.override; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.util.concurrent.atomic.AtomicInteger; 6 | 7 | import org.junit.jupiter.api.Test; 8 | import org.junit.jupiter.params.ParameterizedTest; 9 | 10 | import com.tngtech.junit.dataprovider.DataProvider; 11 | import com.tngtech.junit.dataprovider.UseDataProvider; 12 | 13 | class ImplementingAcceptanceTest implements AcceptanceTestInterface { 14 | 15 | private static final AtomicInteger noOfNormalTestsCalls = new AtomicInteger(0); 16 | private static final AtomicInteger noOfDataProviderTestsCalls = new AtomicInteger(0); 17 | 18 | @DataProvider 19 | public static Object[][] dataProviderToBeImplemented() { 20 | // @formatter:off 21 | return new Object[][] { 22 | { 1 }, 23 | { 2 }, 24 | }; 25 | // @formatter:on 26 | } 27 | 28 | @Test 29 | @Override 30 | public void testToBeImplemented() { 31 | assertThat(1).isEqualTo(noOfNormalTestsCalls.incrementAndGet()); 32 | } 33 | 34 | @ParameterizedTest 35 | @UseDataProvider 36 | @Override 37 | public void testToBeImplemented(Integer value) { 38 | assertThat(value).isEqualTo(noOfDataProviderTestsCalls.incrementAndGet()); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/TagAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import org.junit.jupiter.api.Tag; 6 | import org.junit.jupiter.api.Test; 7 | import org.junit.jupiter.api.TestTemplate; 8 | import org.junit.jupiter.api.extension.ExtendWith; 9 | 10 | import com.tngtech.junit.dataprovider.DataProvider; 11 | import com.tngtech.junit.dataprovider.UseDataProvider; 12 | import com.tngtech.junit.dataprovider.UseDataProviderExtension; 13 | 14 | @Tag("two") 15 | @ExtendWith(UseDataProviderExtension.class) 16 | class TagAcceptanceTest { 17 | 18 | @Test 19 | void testNone() { 20 | // Expect: 21 | assertThat("none").hasSize(4); 22 | } 23 | 24 | @Tag("one") 25 | @Test 26 | void testOne() { 27 | // Expect: 28 | assertThat("one").hasSize(3); 29 | } 30 | 31 | @DataProvider 32 | static Object[][] dataProvider() { 33 | // @formatter:off 34 | return new Object[][] { 35 | { "", 0 }, 36 | { "1", 1 }, 37 | { "12", 2 }, 38 | }; 39 | // @formatter:on 40 | } 41 | 42 | @Tag("one") 43 | @TestTemplate 44 | @UseDataProvider("dataProvider") 45 | void test(String string, int expectedLength) { 46 | // Expect: 47 | assertThat(string).hasSize(expectedLength); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/converter/DateTimeAwareStringConverter.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.converter; 2 | 3 | import java.text.DateFormat; 4 | import java.text.ParseException; 5 | import java.text.SimpleDateFormat; 6 | import java.util.Date; 7 | 8 | import com.tngtech.junit.dataprovider.convert.ConverterContext; 9 | import com.tngtech.junit.dataprovider.convert.StringConverter; 10 | 11 | class DateTimeAwareStringConverter extends StringConverter { 12 | 13 | private final DateFormat dateTimeIso; 14 | private final DateFormat dateIso; 15 | 16 | DateTimeAwareStringConverter() { 17 | dateTimeIso = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS z"); 18 | dateIso = new SimpleDateFormat("yyyy-MM-dd"); 19 | } 20 | 21 | @Override 22 | protected Object customConvertValue(String str, Class targetType, ConverterContext context) { 23 | if (Date.class.equals(targetType)) { 24 | try { 25 | return dateTimeIso.parse(str); 26 | } catch (@SuppressWarnings("unused") ParseException e) { 27 | // ignore 28 | } 29 | try { 30 | return dateIso.parse(str); 31 | } catch (@SuppressWarnings("unused") ParseException e) { 32 | // ignore 33 | } 34 | } 35 | return super.customConvertValue(str, targetType, context); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /core/src/test/java/com/tngtech/junit/dataprovider/testutils/Methods.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.junit.dataprovider.testutils; 2 | 3 | import static org.assertj.core.api.Assertions.fail; 4 | 5 | import java.lang.reflect.Method; 6 | 7 | public class Methods { 8 | 9 | /** 10 | * @return a {@link Method} (never {@code null}) 11 | * @throws AssertionError if no {@link Method} could be found 12 | */ 13 | public static Method anyMethod() { 14 | return getMethod(Methods.class, "anyMethod"); 15 | } 16 | 17 | /** 18 | * @param clazz to be search for the given method name 19 | * @param methodName name of the {@link Method} to be searched 20 | * @return the found {@link Method} (never {@code null}) 21 | * @throws AssertionError if {@link Method} could not be found 22 | */ 23 | public static Method getMethod(Class clazz, String methodName) { 24 | return getMethodInt(clazz, methodName); 25 | } 26 | 27 | private static Method getMethodInt(Class clazz, String methodName) { 28 | if (clazz == null) { 29 | fail(String.format("No method with name '%s' found.", methodName)); 30 | return null; 31 | } 32 | 33 | for (Method method : clazz.getDeclaredMethods()) { 34 | if (method.getName().equals(methodName)) { 35 | return method; 36 | } 37 | } 38 | return getMethodInt(clazz.getSuperclass(), methodName); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/converter/DateTimeAwareStringConverter.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.converter; 2 | 3 | import java.text.DateFormat; 4 | import java.text.ParseException; 5 | import java.text.SimpleDateFormat; 6 | import java.util.Date; 7 | 8 | import com.tngtech.junit.dataprovider.convert.ConverterContext; 9 | import com.tngtech.junit.dataprovider.convert.StringConverter; 10 | 11 | class DateTimeAwareStringConverter extends StringConverter { 12 | 13 | private final DateFormat dateTimeIso; 14 | private final DateFormat dateIso; 15 | 16 | DateTimeAwareStringConverter() { 17 | dateTimeIso = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS z"); 18 | dateIso = new SimpleDateFormat("yyyy-MM-dd"); 19 | } 20 | 21 | @Override 22 | protected Object customConvertValue(String str, Class targetType, ConverterContext context) { 23 | if (Date.class.equals(targetType)) { 24 | try { 25 | return dateTimeIso.parse(str); 26 | } catch (@SuppressWarnings("unused") ParseException e) { 27 | // ignore 28 | } 29 | try { 30 | return dateIso.parse(str); 31 | } catch (@SuppressWarnings("unused") ParseException e) { 32 | // ignore 33 | } 34 | } 35 | return super.customConvertValue(str, targetType, context); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/category/CategoryTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.java.junit.dataprovider.category; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import org.junit.Test; 6 | import org.junit.experimental.categories.Category; 7 | import org.junit.runner.RunWith; 8 | 9 | import com.tngtech.java.junit.dataprovider.DataProvider; 10 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 11 | import com.tngtech.java.junit.dataprovider.UseDataProvider; 12 | 13 | @Category(CategoryTwo.class) 14 | @RunWith(DataProviderRunner.class) 15 | public class CategoryTest { 16 | 17 | @Test 18 | public void testNone() { 19 | // Expect: 20 | assertThat("none").hasSize(4); 21 | } 22 | 23 | @Category(CategoryOne.class) 24 | @Test 25 | public void testOne() { 26 | // Expect: 27 | assertThat("one").hasSize(3); 28 | } 29 | 30 | @DataProvider 31 | public static Object[][] dataProvider() { 32 | // @formatter:off 33 | return new Object[][] { 34 | { "", 0 }, 35 | { "1", 1 }, 36 | { "12", 2 }, 37 | }; 38 | // @formatter:on 39 | } 40 | 41 | @Category(CategoryOne.class) 42 | @Test 43 | @UseDataProvider("dataProvider") 44 | public void test(String string, int expectedLength) { 45 | // Expect: 46 | assertThat(string).hasSize(expectedLength); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /core/src/main/java/com/tngtech/junit/dataprovider/format/DataProviderTestNameFormatter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 TNG Technology Consulting GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.tngtech.junit.dataprovider.format; 17 | 18 | import java.lang.reflect.Method; 19 | import java.util.List; 20 | 21 | /** Provides the format for a dataprovider test using test method, invocation index and arguments list. */ 22 | public interface DataProviderTestNameFormatter { 23 | 24 | /** 25 | * Method formatting the given arguments to provide a test method name. 26 | * 27 | * @param testMethod the test method to be executed 28 | * @param invocationIndex the index within one dataprovider 29 | * @param arguments the arguments used to executed the test method 30 | * @return a formatted test method name 31 | */ 32 | String format(Method testMethod, int invocationIndex, List arguments); 33 | } 34 | -------------------------------------------------------------------------------- /junit4/src/main/java/com/tngtech/java/junit/dataprovider/common/Preconditions.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 TNG Technology Consulting GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.tngtech.java.junit.dataprovider.common; 17 | 18 | public class Preconditions { 19 | 20 | public static T checkNotNull(T object, String errorMessage) { 21 | return com.tngtech.junit.dataprovider.Preconditions.checkNotNull(object, errorMessage); 22 | } 23 | 24 | public static void checkArgument(boolean expression, String errorMessage) { 25 | com.tngtech.junit.dataprovider.Preconditions.checkArgument(expression, errorMessage); 26 | } 27 | 28 | public static void checkArgument(boolean expression, String errorMessageFormat, Object... errorMessageArgs) { 29 | com.tngtech.junit.dataprovider.Preconditions.checkArgument(expression, errorMessageFormat, 30 | errorMessageArgs); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/ListAsArgumentAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | import org.junit.jupiter.api.TestTemplate; 9 | import org.junit.jupiter.api.extension.ExtendWith; 10 | 11 | import com.tngtech.junit.dataprovider.DataProvider; 12 | import com.tngtech.junit.dataprovider.UseDataProvider; 13 | import com.tngtech.junit.dataprovider.UseDataProviderExtension; 14 | 15 | @ExtendWith(UseDataProviderExtension.class) 16 | class ListAsArgumentAcceptanceTest { 17 | 18 | @DataProvider 19 | static Object[][] dataProviderListArg() { 20 | // @formatter:off 21 | return new Object[][] { 22 | { Arrays.asList("a", "b"), "c" }, 23 | }; 24 | // @formatter:on 25 | } 26 | 27 | @TestTemplate 28 | @UseDataProvider 29 | void testListArg(List list, String string) { 30 | // Expected: 31 | assertThat(list).doesNotContain(string); 32 | } 33 | 34 | @DataProvider 35 | static Object[][] stringsData() { 36 | return new Object[][] { 37 | { Arrays.asList("string1", "stringValue"), "stringValue" } 38 | }; 39 | } 40 | 41 | @TestTemplate 42 | @UseDataProvider("stringsData") 43 | void test(List strings, String expectedValue) { 44 | // Expected: 45 | assertThat(strings).contains(expectedValue); 46 | } 47 | } -------------------------------------------------------------------------------- /junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderImplementingAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.java.junit.dataprovider.override; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.util.concurrent.atomic.AtomicInteger; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import com.tngtech.java.junit.dataprovider.DataProvider; 11 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 12 | import com.tngtech.java.junit.dataprovider.UseDataProvider; 13 | 14 | @RunWith(DataProviderRunner.class) 15 | public class DataProviderImplementingAcceptanceTest implements DataProviderAcceptanceTestInterface { 16 | 17 | private static final AtomicInteger noOfNormalTestsCalls = new AtomicInteger(0); 18 | private static final AtomicInteger noOfDataProviderTestsCalls = new AtomicInteger(0); 19 | 20 | @DataProvider 21 | public static Object[][] dataProviderToBeImplemented() { 22 | // @formatter:off 23 | return new Object[][] { 24 | { 1 }, 25 | { 2 }, 26 | }; 27 | // @formatter:on 28 | } 29 | 30 | @Test 31 | @Override 32 | public void testToBeImplemented() { 33 | assertThat(1).isEqualTo(noOfNormalTestsCalls.incrementAndGet()); 34 | } 35 | 36 | @Test 37 | @UseDataProvider 38 | @Override 39 | public void testToBeImplemented(Integer value) { 40 | assertThat(value).isEqualTo(noOfDataProviderTestsCalls.incrementAndGet()); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/RuleAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.java.junit.dataprovider; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import org.junit.ClassRule; 6 | import org.junit.Rule; 7 | import org.junit.Test; 8 | import org.junit.rules.TestWatcher; 9 | import org.junit.runner.Description; 10 | import org.junit.runner.RunWith; 11 | 12 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 13 | 14 | @RunWith(DataProviderRunner.class) 15 | public class RuleAcceptanceTest { 16 | 17 | public static class SomeTestRule extends TestWatcher { 18 | boolean started; 19 | 20 | @Override 21 | protected void starting(Description description) { 22 | started = true; 23 | } 24 | 25 | @Override 26 | protected void finished(Description description) { 27 | assertThat(started).as("Rule was not started, but 'finished' called").isTrue(); 28 | } 29 | } 30 | 31 | @ClassRule 32 | public static final SomeTestRule classRule = new SomeTestRule(); 33 | 34 | @Rule 35 | public SomeTestRule rule = new SomeTestRule(); 36 | 37 | @Test 38 | public void testClassRuleShouldBeStartedBeforeTest() { 39 | // Expected: 40 | assertThat(classRule.started).as("'@ClassRule' was not started").isTrue(); 41 | } 42 | 43 | @Test 44 | public void testRuleShouldBeStartedBeforeTest() { 45 | // Expected: 46 | assertThat(rule.started).as("'@Rule' was not started").isTrue(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/override/ImplementingAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.override; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.util.concurrent.atomic.AtomicInteger; 6 | 7 | import org.junit.jupiter.api.Test; 8 | import org.junit.jupiter.api.TestTemplate; 9 | import org.junit.jupiter.api.extension.ExtendWith; 10 | 11 | import com.tngtech.junit.dataprovider.DataProvider; 12 | import com.tngtech.junit.dataprovider.UseDataProvider; 13 | import com.tngtech.junit.dataprovider.UseDataProviderExtension; 14 | 15 | @ExtendWith(UseDataProviderExtension.class) 16 | class ImplementingAcceptanceTest implements AcceptanceTestInterface { 17 | 18 | private static final AtomicInteger noOfNormalTestsCalls = new AtomicInteger(0); 19 | private static final AtomicInteger noOfDataProviderTestsCalls = new AtomicInteger(0); 20 | 21 | @DataProvider 22 | public static Object[][] dataProviderToBeImplemented() { 23 | // @formatter:off 24 | return new Object[][] { 25 | { 1 }, 26 | { 2 }, 27 | }; 28 | // @formatter:on 29 | } 30 | 31 | @Test 32 | @Override 33 | public void testToBeImplemented() { 34 | assertThat(1).isEqualTo(noOfNormalTestsCalls.incrementAndGet()); 35 | } 36 | 37 | @TestTemplate 38 | @UseDataProvider 39 | @Override 40 | public void testToBeImplemented(Integer value) { 41 | assertThat(value).isEqualTo(noOfDataProviderTestsCalls.incrementAndGet()); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/converter/CustomConverterDataProviderExtension.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.converter; 2 | 3 | import org.junit.platform.commons.support.ReflectionSupport; 4 | 5 | import com.tngtech.junit.dataprovider.DataProviderInvocationContextProvider; 6 | import com.tngtech.junit.dataprovider.DisplayNameContext; 7 | import com.tngtech.junit.dataprovider.convert.ConverterContext; 8 | 9 | class CustomConverterDataProviderExtension extends DataProviderInvocationContextProvider { 10 | 11 | CustomConverterDataProviderExtension() { 12 | super(CustomConverterDataProvider.class); 13 | } 14 | 15 | @Override 16 | protected Object getData(CustomConverterDataProvider annotation) { 17 | return annotation.value(); 18 | } 19 | 20 | @Override 21 | protected ConverterContext getConverterContext(CustomConverterDataProvider annotation) { 22 | return new ConverterContext(ReflectionSupport.newInstance(annotation.objectArrayConverter()), 23 | ReflectionSupport.newInstance(annotation.singleArgConverter()), 24 | ReflectionSupport.newInstance(annotation.stringConverter()), annotation.splitBy(), 25 | annotation.convertNulls(), annotation.trimValues(), annotation.ignoreEnumCase()); 26 | } 27 | 28 | @Override 29 | protected DisplayNameContext getDisplayNameContext(CustomConverterDataProvider annotation) { 30 | return new DisplayNameContext(annotation.formatter(), annotation.format(), getDefaultPlaceholders()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /core/src/main/java/com/tngtech/junit/dataprovider/resolver/DataProviderMethodResolver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 TNG Technology Consulting GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.tngtech.junit.dataprovider.resolver; 17 | 18 | import java.lang.reflect.Method; 19 | import java.util.List; 20 | 21 | /** 22 | * Interface to be used to implement a dataprovider method resolver based on the given {@link DataProviderResolverContext}. 23 | */ 24 | public interface DataProviderMethodResolver { 25 | 26 | /** 27 | * Returns the dataprovider methods that belongs to the given test method using the given resolver context or an 28 | * empty {@link List} if no dataprovider method could be found. 29 | * 30 | * @param context for resolving of dataprovider methods 31 | * @return the resolved dataprovider methods or an empty {@link List} if no dataprovider methods could be found 32 | * @throws NullPointerException if given {@code context} is {@code null} 33 | */ 34 | List resolve(DataProviderResolverContext context); 35 | } 36 | -------------------------------------------------------------------------------- /junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/custom/DataProviderCustomStringConverterAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.java.junit.dataprovider.custom; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.util.Calendar; 6 | import java.util.Date; 7 | import java.util.GregorianCalendar; 8 | import java.util.TimeZone; 9 | 10 | import org.junit.Test; 11 | import org.junit.runner.RunWith; 12 | 13 | import com.tngtech.java.junit.dataprovider.DataProvider; 14 | 15 | @RunWith(CustomDataProviderRunner.class) 16 | public class DataProviderCustomStringConverterAcceptanceTest { 17 | 18 | // @formatter:off 19 | @Test 20 | @DataProvider(value = { 21 | "2016-02-19 | 2016 | 02 | 19 | 00 | 00 | 00 | 000 | UTC", 22 | "2016-02-19T20:15:22.629 GMT | 2016 | 02 | 19 | 20 | 15 | 22 | 629 | UTC", 23 | }, splitBy = "\\|") 24 | // @formatter:off 25 | public void testDateTime(Date date, int year, int month, int dayOfMonth, int hourOfDay, int minute, int second, int millis, String timeZone) { 26 | // Expect: 27 | assertThat(date).isEqualTo(date(year, month, dayOfMonth, hourOfDay, minute, second, millis, timeZone)); 28 | } 29 | 30 | private Date date(int year, int month, int dayOfMonth, int hourOfDay, int minute, int second, int millis, String timeZone) { 31 | GregorianCalendar calendar = new GregorianCalendar(year, month - 1, dayOfMonth, hourOfDay, minute, second); 32 | calendar.set(Calendar.MILLISECOND, millis); 33 | TimeZone.setDefault(TimeZone.getTimeZone(timeZone)); 34 | return calendar.getTime(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/DataProviderNewlineAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.java.junit.dataprovider; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | 6 | import com.tngtech.java.junit.dataprovider.DataProvider; 7 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 8 | import com.tngtech.java.junit.dataprovider.UseDataProvider; 9 | 10 | @RunWith(DataProviderRunner.class) 11 | public class DataProviderNewlineAcceptanceTest { 12 | 13 | private static class NewlinesInToString { 14 | private final int lines; 15 | 16 | public NewlinesInToString(int lines) { 17 | this.lines = lines; 18 | } 19 | 20 | @Override 21 | public String toString() { 22 | StringBuilder b = new StringBuilder(); 23 | for (int i = 0; i < lines; i++) { 24 | b.append("Line ").append(i).append("\n"); 25 | } 26 | return b.toString(); 27 | } 28 | 29 | } 30 | 31 | @DataProvider 32 | public static Object[][] dataProviderNewlinesWithinParameters() { 33 | // @formatter:off 34 | return new Object[][] { 35 | { new NewlinesInToString(0) }, 36 | { new NewlinesInToString(1) }, 37 | { new NewlinesInToString(2) }, 38 | { new NewlinesInToString(3) }, 39 | }; 40 | // @formatter:on 41 | } 42 | 43 | @Test 44 | @UseDataProvider("dataProviderNewlinesWithinParameters") 45 | public void testNewlinesWithinParameters(@SuppressWarnings("unused") NewlinesInToString object) { 46 | // Check output within IDE 47 | } 48 | } -------------------------------------------------------------------------------- /junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/DataProviderBeforeAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.java.junit.dataprovider; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | 9 | import com.tngtech.java.junit.dataprovider.DataProvider; 10 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 11 | import com.tngtech.java.junit.dataprovider.UseDataProvider; 12 | 13 | @RunWith(DataProviderRunner.class) 14 | public class DataProviderBeforeAcceptanceTest { 15 | 16 | private Object field; 17 | 18 | @Before 19 | public void setup() { 20 | field = Integer.valueOf(1); 21 | } 22 | 23 | @DataProvider 24 | public static Object[][] dataProviderFieldIsEqualTo() { 25 | // @formatter:off 26 | return new Object[][] { 27 | { 1 }, 28 | { Integer.valueOf(1) }, 29 | }; 30 | // @formatter:on 31 | } 32 | 33 | @Test 34 | @UseDataProvider 35 | public void testFieldIsEqualTo(Object obj) { 36 | // Expect: 37 | assertThat(field).isEqualTo(obj); 38 | } 39 | 40 | @DataProvider 41 | public static Object[][] dataProviderFieldIsNotEqualTo() { 42 | // @formatter:off 43 | return new Object[][] { 44 | { null }, 45 | { "" }, 46 | { Integer.valueOf(2) }, 47 | }; 48 | // @formatter:on 49 | } 50 | 51 | @Test 52 | @UseDataProvider 53 | public void testFieldIsNotEqualTo(Object obj) { 54 | // Expect: 55 | assertThat(field).isNotEqualTo(obj); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/BeforeAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import org.junit.jupiter.api.BeforeAll; 6 | import org.junit.jupiter.api.BeforeEach; 7 | import org.junit.jupiter.params.ParameterizedTest; 8 | 9 | import com.tngtech.junit.dataprovider.DataProvider; 10 | import com.tngtech.junit.dataprovider.UseDataProvider; 11 | 12 | class BeforeAcceptanceTest { 13 | 14 | private static Object staticField; 15 | private Object field; 16 | 17 | @BeforeAll 18 | static void setupClass() { 19 | staticField = Integer.valueOf(1); 20 | } 21 | 22 | @BeforeEach 23 | void setup() { 24 | field = Integer.valueOf(1); 25 | } 26 | 27 | @DataProvider 28 | static Object[][] dataProviderFieldIsEqualTo() { 29 | // @formatter:off 30 | return new Object[][] { 31 | { 1 }, 32 | { staticField }, 33 | }; 34 | // @formatter:on 35 | } 36 | 37 | @ParameterizedTest 38 | @UseDataProvider 39 | void testFieldIsEqualTo(Object obj) { 40 | // Expect: 41 | assertThat(field).isEqualTo(obj); 42 | } 43 | 44 | @DataProvider 45 | static Object[][] dataProviderFieldIsNotEqualTo() { 46 | // @formatter:off 47 | return new Object[][] { 48 | { null }, 49 | { "" }, 50 | { Integer.valueOf(2) }, 51 | }; 52 | // @formatter:on 53 | } 54 | 55 | @ParameterizedTest 56 | @UseDataProvider 57 | void testFieldIsNotEqualTo(Object obj) { 58 | // Expect: 59 | assertThat(field).isNotEqualTo(obj); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/resolver/CustomResolverDataProviderTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.resolver; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | import org.junit.jupiter.api.TestTemplate; 10 | import org.junit.jupiter.api.extension.ExtendWith; 11 | 12 | import com.tngtech.junit.dataprovider.UseDataProvider; 13 | import com.tngtech.junit.dataprovider.resolver.DataProviderMethodResolver; 14 | import com.tngtech.junit.dataprovider.resolver.DataProviderResolverContext; 15 | import com.tngtech.junit.dataprovider.resolver.ResolveStrategy; 16 | 17 | /** 18 | * Annotate a test method for using it with a dataprovider. 19 | */ 20 | @Documented 21 | @Retention(RetentionPolicy.RUNTIME) 22 | @Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD }) 23 | @TestTemplate 24 | @ExtendWith(CustomResolverDataProviderTestExtension.class) 25 | @interface CustomResolverDataProviderTest { 26 | 27 | /** 28 | * @see UseDataProvider#value() 29 | */ 30 | String value() default DataProviderResolverContext.METHOD_NAME_TO_USE_CONVENTION; 31 | 32 | /** 33 | * @see UseDataProvider#location() 34 | */ 35 | Class[] location() default {}; 36 | 37 | /** 38 | * @see UseDataProvider#resolver() 39 | */ 40 | Class[] resolver() default { 41 | DataProviderStartWithTestMethodNameResolver.class }; 42 | 43 | /** 44 | * @see UseDataProvider#resolveStrategy() 45 | */ 46 | ResolveStrategy resolveStrategy() default ResolveStrategy.UNTIL_FIRST_MATCH; 47 | } 48 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/meta/DataProviderTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.meta; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | import org.junit.jupiter.api.TestTemplate; 10 | import org.junit.jupiter.api.extension.ExtendWith; 11 | 12 | import com.tngtech.junit.dataprovider.UseDataProvider; 13 | import com.tngtech.junit.dataprovider.resolver.DataProviderMethodResolver; 14 | import com.tngtech.junit.dataprovider.resolver.DataProviderResolverContext; 15 | import com.tngtech.junit.dataprovider.resolver.DefaultDataProviderMethodResolver; 16 | import com.tngtech.junit.dataprovider.resolver.ResolveStrategy; 17 | 18 | /** 19 | * Annotate a test method for using it with a dataprovider. 20 | */ 21 | @Documented 22 | @Retention(RetentionPolicy.RUNTIME) 23 | @Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD }) 24 | @TestTemplate 25 | @ExtendWith(DataProviderTestExtension.class) 26 | @interface DataProviderTest { 27 | 28 | /** 29 | * @see UseDataProvider#value() 30 | */ 31 | String value() default DataProviderResolverContext.METHOD_NAME_TO_USE_CONVENTION; 32 | 33 | /** 34 | * @see UseDataProvider#location() 35 | */ 36 | Class[] location() default {}; 37 | 38 | /** 39 | * @see UseDataProvider#resolver() 40 | */ 41 | Class[] resolver() default { DefaultDataProviderMethodResolver.class }; 42 | 43 | /** 44 | * @see UseDataProvider#resolveStrategy() 45 | */ 46 | ResolveStrategy resolveStrategy() default ResolveStrategy.UNTIL_FIRST_MATCH; 47 | } 48 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/resolver/CustomResolverUseDataProvider.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.resolver; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | import org.junit.jupiter.params.provider.ArgumentsSource; 10 | 11 | import com.tngtech.junit.dataprovider.resolver.DataProviderMethodResolver; 12 | import com.tngtech.junit.dataprovider.resolver.DataProviderResolverContext; 13 | import com.tngtech.junit.dataprovider.resolver.ResolveStrategy; 14 | 15 | /** 16 | * Annotate a test method for using it with a dataprovider. 17 | */ 18 | @Documented 19 | @Retention(RetentionPolicy.RUNTIME) 20 | @Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD }) 21 | @ArgumentsSource(CustomResolverUseDataProviderArgumentProvider.class) 22 | @interface CustomResolverUseDataProvider { 23 | 24 | /** 25 | * @see com.tngtech.junit.dataprovider.UseDataProvider#value() 26 | */ 27 | String value() default DataProviderResolverContext.METHOD_NAME_TO_USE_CONVENTION; 28 | 29 | /** 30 | * @see com.tngtech.junit.dataprovider.UseDataProvider#location() 31 | */ 32 | Class[] location() default {}; 33 | 34 | /** 35 | * @see com.tngtech.junit.dataprovider.UseDataProvider#resolver() 36 | */ 37 | Class[] resolver() default { 38 | DataProviderStartWithTestMethodNameResolver.class }; 39 | 40 | /** 41 | * @see com.tngtech.junit.dataprovider.UseDataProvider#resolveStrategy() 42 | */ 43 | ResolveStrategy resolveStrategy() default ResolveStrategy.UNTIL_FIRST_MATCH; 44 | } 45 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/NewlineAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import org.junit.jupiter.params.ParameterizedTest; 4 | 5 | import com.tngtech.junit.dataprovider.DataProvider; 6 | import com.tngtech.junit.dataprovider.UseDataProvider; 7 | 8 | class NewlineAcceptanceTest { 9 | 10 | private static class NewlinesInToString { 11 | private final int lines; 12 | 13 | NewlinesInToString(int lines) { 14 | this.lines = lines; 15 | } 16 | 17 | @Override 18 | public String toString() { 19 | StringBuilder b = new StringBuilder(); 20 | for (int i = 0; i < lines; i++) { 21 | b.append("Line ").append(i).append("\n"); 22 | } 23 | return b.toString(); 24 | } 25 | 26 | } 27 | 28 | @DataProvider 29 | static Object[][] dataProviderNewlinesWithinParameters() { 30 | // @formatter:off 31 | return new Object[][] { 32 | { new NewlinesInToString(0) }, 33 | { new NewlinesInToString(1) }, 34 | { new NewlinesInToString(2) }, 35 | { new NewlinesInToString(3) }, 36 | }; 37 | // @formatter:on 38 | } 39 | 40 | @ParameterizedTest 41 | @UseDataProvider("dataProviderNewlinesWithinParameters") 42 | void testNewlinesWithinParameters(@SuppressWarnings("unused") NewlinesInToString object) { 43 | // Check output within IDE 44 | } 45 | 46 | @ParameterizedTest 47 | @DataProvider({ "Do it.\nOr let it." }) 48 | void testWithStringContainingTabsNewlineAndCarriageReturn(@SuppressWarnings("unused") String string) { 49 | // nothing to do => Just look at the test output in Eclispe's JUnit view if it is displayed correctly 50 | } 51 | } -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/meta/StringDataProvider.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.meta; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | import org.junit.jupiter.api.TestTemplate; 10 | import org.junit.jupiter.api.extension.ExtendWith; 11 | 12 | import com.tngtech.junit.dataprovider.DataProvider; 13 | import com.tngtech.junit.dataprovider.format.DataProviderPlaceholderFormatter; 14 | import com.tngtech.junit.dataprovider.format.DataProviderTestNameFormatter; 15 | 16 | /** 17 | * @see DataProvider 18 | */ 19 | @Documented 20 | @Retention(RetentionPolicy.RUNTIME) 21 | @Target(ElementType.METHOD) 22 | @TestTemplate 23 | @ExtendWith(StringDataProviderExtension.class) 24 | @interface StringDataProvider { 25 | /** 26 | * @see DataProvider#value() 27 | */ 28 | String[] value() default {}; 29 | 30 | /** 31 | * @see DataProvider#splitBy() 32 | */ 33 | String splitBy() default "\\|"; 34 | 35 | /** 36 | * @see DataProvider#convertNulls() 37 | */ 38 | boolean convertNulls() default true; 39 | 40 | /** 41 | * @see DataProvider#trimValues() 42 | */ 43 | boolean trimValues() default true; 44 | 45 | /** 46 | * @see DataProvider#ignoreEnumCase() 47 | */ 48 | boolean ignoreEnumCase() default false; 49 | 50 | /** 51 | * @see DataProvider#format() 52 | */ 53 | String format() default DataProvider.DEFAULT_FORMAT; 54 | 55 | /** 56 | * @see DataProvider#formatter() 57 | */ 58 | Class formatter() default DataProviderPlaceholderFormatter.class; 59 | } 60 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/NameConventionAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static com.tngtech.junit.dataprovider.DataProviders.$; 4 | import static com.tngtech.junit.dataprovider.DataProviders.$$; 5 | import static org.assertj.core.api.Assertions.assertThat; 6 | 7 | import org.junit.jupiter.params.ParameterizedTest; 8 | 9 | import com.tngtech.junit.dataprovider.DataProvider; 10 | import com.tngtech.junit.dataprovider.UseDataProvider; 11 | 12 | class NameConventionAcceptanceTest { 13 | 14 | @DataProvider 15 | static Object[][] testIsEmptyString() { 16 | // @formatter:off 17 | return new Object[][] { 18 | { null }, 19 | { "" }, 20 | }; 21 | // @formatter:on 22 | } 23 | 24 | @ParameterizedTest 25 | @UseDataProvider 26 | void testIsEmptyString(String str) { 27 | // Given: 28 | 29 | // When: 30 | boolean isEmpty = (str == null) || str.isEmpty(); 31 | 32 | // Then: 33 | assertThat(isEmpty).isTrue(); 34 | } 35 | 36 | @DataProvider 37 | static Object[][] dataProviderAdd() { 38 | // @formatter:off 39 | return $$( 40 | $( -1, -1, -2 ), 41 | $( -1, 0, -1 ), 42 | $( 0, -1, -1 ), 43 | $( 0, 0, 0 ), 44 | $( 0, 1, 1 ), 45 | $( 1, 0, 1 ), 46 | $( 1, 1, 2 ) 47 | ); 48 | // @formatter:on 49 | } 50 | 51 | @ParameterizedTest 52 | @UseDataProvider 53 | void testAdd(int a, int b, int expected) { 54 | // Given: 55 | 56 | // When: 57 | int result = a + b; 58 | 59 | // Then: 60 | assertThat(result).isEqualTo(expected); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /junit-jupiter/src/main/java/com/tngtech/junit/dataprovider/format/DataProviderPlaceholderFormatter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 TNG Technology Consulting GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.tngtech.junit.dataprovider.format; 17 | 18 | import java.lang.reflect.Method; 19 | import java.util.List; 20 | 21 | import com.tngtech.junit.dataprovider.placeholder.BasePlaceholder; 22 | import com.tngtech.junit.dataprovider.placeholder.ReplacementData; 23 | 24 | public class DataProviderPlaceholderFormatter implements DataProviderTestNameFormatter { 25 | 26 | private final String format; 27 | private final List placeholders; 28 | 29 | public DataProviderPlaceholderFormatter(String format, List placeholders) { 30 | this.format = format; 31 | this.placeholders = placeholders; 32 | } 33 | 34 | @Override 35 | public String format(Method testMethod, int invocationIndex, List arguments) { 36 | ReplacementData data = ReplacementData.of(testMethod, invocationIndex, arguments); 37 | 38 | String result = format; 39 | for (BasePlaceholder placeHolder : placeholders) { 40 | result = placeHolder.process(data, result); 41 | } 42 | return result; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/meta/DataProviderTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.meta; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | import org.junit.jupiter.params.ParameterizedTest; 10 | import org.junit.jupiter.params.provider.ArgumentsSource; 11 | 12 | import com.tngtech.junit.dataprovider.resolver.DataProviderMethodResolver; 13 | import com.tngtech.junit.dataprovider.resolver.DataProviderResolverContext; 14 | import com.tngtech.junit.dataprovider.resolver.DefaultDataProviderMethodResolver; 15 | import com.tngtech.junit.dataprovider.resolver.ResolveStrategy; 16 | 17 | /** 18 | * Annotate a test method for using it with a dataprovider. 19 | */ 20 | @Documented 21 | @Retention(RetentionPolicy.RUNTIME) 22 | @Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD }) 23 | @ParameterizedTest 24 | @ArgumentsSource(DataProviderTestProvider.class) 25 | @interface DataProviderTest { 26 | 27 | /** 28 | * @see com.tngtech.junit.dataprovider.UseDataProvider#value() 29 | */ 30 | String value() default DataProviderResolverContext.METHOD_NAME_TO_USE_CONVENTION; 31 | 32 | /** 33 | * @see com.tngtech.junit.dataprovider.UseDataProvider#location() 34 | */ 35 | Class[] location() default {}; 36 | 37 | /** 38 | * @see com.tngtech.junit.dataprovider.UseDataProvider#resolver() 39 | */ 40 | Class[] resolver() default { DefaultDataProviderMethodResolver.class }; 41 | 42 | /** 43 | * @see com.tngtech.junit.dataprovider.UseDataProvider#resolveStrategy() 44 | */ 45 | ResolveStrategy resolveStrategy() default ResolveStrategy.UNTIL_FIRST_MATCH; 46 | } 47 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/BeforeAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import org.junit.jupiter.api.BeforeAll; 6 | import org.junit.jupiter.api.BeforeEach; 7 | import org.junit.jupiter.api.TestTemplate; 8 | import org.junit.jupiter.api.extension.ExtendWith; 9 | 10 | import com.tngtech.junit.dataprovider.DataProvider; 11 | import com.tngtech.junit.dataprovider.UseDataProvider; 12 | import com.tngtech.junit.dataprovider.UseDataProviderExtension; 13 | 14 | @ExtendWith(UseDataProviderExtension.class) 15 | class BeforeAcceptanceTest { 16 | 17 | private static Object staticField; 18 | private Object field; 19 | 20 | @BeforeAll 21 | static void setupClass() { 22 | staticField = Integer.valueOf(1); 23 | } 24 | 25 | @BeforeEach 26 | void setup() { 27 | field = Integer.valueOf(1); 28 | } 29 | 30 | @DataProvider 31 | static Object[][] dataProviderFieldIsEqualTo() { 32 | // @formatter:off 33 | return new Object[][] { 34 | { 1 }, 35 | { staticField }, 36 | }; 37 | // @formatter:on 38 | } 39 | 40 | @TestTemplate 41 | @UseDataProvider 42 | void testFieldIsEqualTo(Object obj) { 43 | // Expect: 44 | assertThat(field).isEqualTo(obj); 45 | } 46 | 47 | @DataProvider 48 | static Object[][] dataProviderFieldIsNotEqualTo() { 49 | // @formatter:off 50 | return new Object[][] { 51 | { null }, 52 | { "" }, 53 | { Integer.valueOf(2) }, 54 | }; 55 | // @formatter:on 56 | } 57 | 58 | @TestTemplate 59 | @UseDataProvider 60 | void testFieldIsNotEqualTo(Object obj) { 61 | // Expect: 62 | assertThat(field).isNotEqualTo(obj); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/DataProviderMethodParameterAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | import static org.assertj.core.util.Preconditions.checkNotNull; 5 | 6 | import java.lang.annotation.ElementType; 7 | import java.lang.annotation.Retention; 8 | import java.lang.annotation.RetentionPolicy; 9 | import java.lang.annotation.Target; 10 | 11 | import org.junit.jupiter.api.TestInfo; 12 | import org.junit.jupiter.api.TestReporter; 13 | import org.junit.jupiter.params.ParameterizedTest; 14 | 15 | import com.tngtech.junit.dataprovider.DataProvider; 16 | import com.tngtech.junit.dataprovider.UseDataProvider; 17 | 18 | class DataProviderMethodParameterAcceptanceTest { 19 | 20 | @Retention(RetentionPolicy.RUNTIME) 21 | @Target(ElementType.METHOD) 22 | @interface ExternalFile { 23 | enum Format { 24 | CSV, 25 | XML, 26 | XLS; 27 | } 28 | 29 | Format format(); 30 | 31 | String value(); 32 | } 33 | 34 | @DataProvider 35 | static Object[][] loadFromExternalFile(TestInfo testInfo, TestReporter testReporter) { 36 | checkNotNull(testInfo, "'testInfo' is not set"); 37 | checkNotNull(testReporter, "'testReporter' is not set"); 38 | 39 | String testDataFile = testInfo.getTestMethod().get().getAnnotation(ExternalFile.class).value(); 40 | // Load the data from the external file here ... 41 | return new Object[][] { { testDataFile } }; 42 | } 43 | 44 | @ParameterizedTest 45 | @UseDataProvider("loadFromExternalFile") 46 | @ExternalFile(format = ExternalFile.Format.CSV, value = "testdata.csv") 47 | void testThatUsesUniversalDataProvider(String testData) { 48 | // Expect: 49 | assertThat(testData).isEqualTo("testdata.csv"); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/AdditionalTestMethodParameterAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import org.junit.jupiter.api.TestInfo; 6 | import org.junit.jupiter.params.ParameterizedTest; 7 | 8 | import com.tngtech.junit.dataprovider.DataProvider; 9 | import com.tngtech.junit.dataprovider.UseDataProvider; 10 | 11 | class AdditionalTestMethodParameterAcceptanceTest { 12 | 13 | @DataProvider 14 | static Object[][] dataProviderObjectArrayArray() { 15 | // @formatter:off 16 | return new Object[][] { 17 | { 0, 1, "0, 1" }, 18 | { 'a', 'b', "a, b" }, 19 | }; 20 | // @formatter:on 21 | } 22 | 23 | @ParameterizedTest 24 | @UseDataProvider 25 | void testObjectArrayArray(Object a, Object b, String expected, TestInfo testInfo) { 26 | // Expect: 27 | assertThat(testInfo.getDisplayName()).contains(expected); 28 | } 29 | 30 | @DataProvider 31 | static Object[] dataProviderObjectArray() { 32 | // @formatter:off 33 | return new Object[] { 34 | 'a', 35 | "a", 36 | }; 37 | // @formatter:on 38 | } 39 | 40 | @ParameterizedTest 41 | @UseDataProvider 42 | void testObjectArray(Object a, TestInfo testInfo) { 43 | // Expect: 44 | assertThat(testInfo.getDisplayName()).contains("a"); 45 | } 46 | 47 | @ParameterizedTest 48 | // @formatter:off 49 | @DataProvider(value = { 50 | "2 | 3 | 2, 3", 51 | "c | d | c, d", 52 | }, splitBy = "\\|") 53 | // @formatter:on 54 | void testThree(String a, String b, String expected, TestInfo testInfo) { 55 | // Expect: 56 | assertThat(testInfo.getDisplayName()).contains(expected); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /junit4/src/main/java/com/tngtech/java/junit/dataprovider/DataProviderMethodResolver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 TNG Technology Consulting GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.tngtech.java.junit.dataprovider; 17 | 18 | import java.util.List; 19 | 20 | import org.junit.runners.model.FrameworkMethod; 21 | 22 | /** 23 | * Interface to be used to implement a dataprovider method resolver based on the test method. The resolver can be specified for test case 24 | * using {@link UseDataProvider#resolver()}. The provided resolvers are executed according to the provide strategy in 25 | * {@link UseDataProvider#resolveStrategy()}. 26 | * 27 | */ 28 | public interface DataProviderMethodResolver { 29 | 30 | /** 31 | * Returns the dataprovider methods that belongs to the given test method or an empty {@link List} if no such dataprovider method could 32 | * be found. 33 | * 34 | * @param testMethod test method that uses a dataprovider 35 | * @param useDataProvider {@link UseDataProvider} annoation on the given test method 36 | * @return the resolved dataprovider methods or an empty {@link List} if dataprovider could not be found (never {@code null}) 37 | * @throws IllegalArgumentException if given {@code testMethod} is {@code null} 38 | */ 39 | List resolve(FrameworkMethod testMethod, UseDataProvider useDataProvider); 40 | } 41 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/placeholder/CustomPlaceholderDataProviderExtension.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.placeholder; 2 | 3 | import java.util.List; 4 | 5 | import org.junit.platform.commons.support.ReflectionSupport; 6 | 7 | import com.tngtech.junit.dataprovider.DataProvider; 8 | import com.tngtech.junit.dataprovider.DataProviderInvocationContextProvider; 9 | import com.tngtech.junit.dataprovider.DisplayNameContext; 10 | import com.tngtech.junit.dataprovider.convert.ConverterContext; 11 | import com.tngtech.junit.dataprovider.placeholder.BasePlaceholder; 12 | 13 | class CustomPlaceholderDataProviderExtension extends DataProviderInvocationContextProvider { 14 | 15 | CustomPlaceholderDataProviderExtension() { 16 | super(DataProvider.class); 17 | } 18 | 19 | @Override 20 | protected Object getData(DataProvider dataProvider) { 21 | return dataProvider.value(); 22 | } 23 | 24 | @Override 25 | protected ConverterContext getConverterContext(DataProvider dataProvider) { 26 | return new ConverterContext(ReflectionSupport.newInstance(dataProvider.objectArrayConverter()), 27 | ReflectionSupport.newInstance(dataProvider.singleArgConverter()), 28 | ReflectionSupport.newInstance(dataProvider.stringConverter()), dataProvider.splitBy(), 29 | dataProvider.convertNulls(), dataProvider.trimValues(), dataProvider.ignoreEnumCase()); 30 | } 31 | 32 | @Override 33 | protected DisplayNameContext getDisplayNameContext(DataProvider dataProvider) { 34 | @SuppressWarnings("unchecked") 35 | List defaultPlaceholders = (List) getDefaultPlaceholders(); 36 | defaultPlaceholders.add(0, new StripArgumentLengthPlaceholder(10)); 37 | return new DisplayNameContext(dataProvider.formatter(), dataProvider.format(), defaultPlaceholders); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /core/src/main/java/com/tngtech/junit/dataprovider/convert/SingleArgConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 TNG Technology Consulting GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.tngtech.junit.dataprovider.convert; 17 | 18 | import static com.tngtech.junit.dataprovider.Preconditions.checkArgument; 19 | 20 | public class SingleArgConverter extends AbstractObjectConverter { 21 | 22 | /** 23 | * {@inheritDoc} 24 | * 25 | * @param data argument for test method 26 | * @param isVarargs determines whether test method has a varargs parameter 27 | * @param parameterTypes target types of parameters 28 | * @return {@code Object[]} which is converted and checked 29 | * @throws IllegalArgumentException if and only if the parameter size does not fit, test method has varargs, or 30 | * there is a type mismatch 31 | */ 32 | @Override 33 | public Object[] convert(Object data, boolean isVarargs, Class[] parameterTypes) { 34 | checkArgument(parameterTypes.length >= 1, 35 | "Object[] dataprovider must at least have a single argument for the dataprovider but found no parameters"); 36 | checkArgument(!isVarargs, "Object[] dataprovider does not support varargs"); 37 | 38 | Object[] result = new Object[] { data }; 39 | checkIfArgumentsMatchParameterTypes(result, parameterTypes); 40 | return result; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/main/java/com/tngtech/junit/dataprovider/StringDataProviderArgumentProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 TNG Technology Consulting GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.tngtech.junit.dataprovider; 17 | 18 | import org.junit.platform.commons.support.ReflectionSupport; 19 | 20 | import com.tngtech.junit.dataprovider.convert.ConverterContext; 21 | 22 | /** 23 | * Default implementation for using a dataprovider that provides the data directly within itself. 24 | */ 25 | class StringDataProviderArgumentProvider extends AbstractStringDataProviderArgumentProvider { 26 | 27 | @Override 28 | public void accept(DataProvider sourceAnnotation) { 29 | this.sourceAnnotation = sourceAnnotation; 30 | } 31 | 32 | @Override 33 | protected Object getData(DataProvider dataProvider) { 34 | return dataProvider.value(); 35 | } 36 | 37 | @Override 38 | protected ConverterContext getConverterContext(DataProvider dataProvider) { 39 | return new ConverterContext(ReflectionSupport.newInstance(dataProvider.objectArrayConverter()), 40 | ReflectionSupport.newInstance(dataProvider.singleArgConverter()), 41 | ReflectionSupport.newInstance(dataProvider.stringConverter()), dataProvider.splitBy(), 42 | dataProvider.convertNulls(), dataProvider.trimValues(), dataProvider.ignoreEnumCase()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/NameConventionAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static com.tngtech.junit.dataprovider.DataProviders.$; 4 | import static com.tngtech.junit.dataprovider.DataProviders.$$; 5 | import static org.assertj.core.api.Assertions.assertThat; 6 | 7 | import org.junit.jupiter.api.TestTemplate; 8 | import org.junit.jupiter.api.extension.ExtendWith; 9 | 10 | import com.tngtech.junit.dataprovider.DataProvider; 11 | import com.tngtech.junit.dataprovider.UseDataProvider; 12 | import com.tngtech.junit.dataprovider.UseDataProviderExtension; 13 | 14 | @ExtendWith(UseDataProviderExtension.class) 15 | class NameConventionAcceptanceTest { 16 | 17 | @DataProvider 18 | static Object[][] testIsEmptyString() { 19 | // @formatter:off 20 | return new Object[][] { 21 | { null }, 22 | { "" }, 23 | }; 24 | // @formatter:on 25 | } 26 | 27 | @TestTemplate 28 | @UseDataProvider 29 | void testIsEmptyString(String str) { 30 | // Given: 31 | 32 | // When: 33 | boolean isEmpty = (str == null) || str.isEmpty(); 34 | 35 | // Then: 36 | assertThat(isEmpty).isTrue(); 37 | } 38 | 39 | @DataProvider 40 | static Object[][] dataProviderAdd() { 41 | // @formatter:off 42 | return $$( 43 | $( -1, -1, -2 ), 44 | $( -1, 0, -1 ), 45 | $( 0, -1, -1 ), 46 | $( 0, 0, 0 ), 47 | $( 0, 1, 1 ), 48 | $( 1, 0, 1 ), 49 | $( 1, 1, 2 ) 50 | ); 51 | // @formatter:on 52 | } 53 | 54 | @TestTemplate 55 | @UseDataProvider 56 | void testAdd(int a, int b, int expected) { 57 | // Given: 58 | 59 | // When: 60 | int result = a + b; 61 | 62 | // Then: 63 | assertThat(result).isEqualTo(expected); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/CachedDataProviderResultsGloballyAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import org.junit.jupiter.params.ParameterizedTest; 6 | 7 | import com.tngtech.junit.dataprovider.UseDataProvider; 8 | 9 | class CachedDataProviderResultsGloballyAcceptanceTest { 10 | 11 | @ParameterizedTest 12 | @UseDataProvider(value = "dataProviderCachedDataProviderResults", location = CacheDataProviderResultsAcceptanceTest.class) 13 | void testCachedDataProviderResultsOne(int noOfDataProvderCalls) { 14 | // Expected: 15 | assertThat(noOfDataProvderCalls).isEqualTo(1); 16 | } 17 | 18 | @ParameterizedTest 19 | @UseDataProvider(value = "dataProviderCachedDataProviderResults", location = CacheDataProviderResultsAcceptanceTest.class) 20 | void testCachedDataProviderResultsTwo(int noOfDataProvderCalls) { 21 | // Expected: 22 | assertThat(noOfDataProvderCalls).isEqualTo(1); 23 | } 24 | 25 | @ParameterizedTest 26 | @UseDataProvider(value = "dataProviderDoNotCacheDataProviderResults", location = CacheDataProviderResultsAcceptanceTest.class) 27 | public void testDoNotCacheDataProviderResultsOne(int noOfDataProvderCalls) { 28 | // Expected: 29 | assertThat(noOfDataProvderCalls).isEqualTo( 30 | CacheDataProviderResultsAcceptanceTest.noOfTestsCallsUsingNotCachedDataProvider.incrementAndGet()); 31 | } 32 | 33 | @ParameterizedTest 34 | @UseDataProvider(value = "dataProviderDoNotCacheDataProviderResults", location = CacheDataProviderResultsAcceptanceTest.class) 35 | public void testDoNotCacheCachedDataProviderResultsTwo(int noOfDataProvderCalls) { 36 | // Expected: 37 | assertThat(noOfDataProvderCalls).isEqualTo( 38 | CacheDataProviderResultsAcceptanceTest.noOfTestsCallsUsingNotCachedDataProvider.incrementAndGet()); 39 | } 40 | } -------------------------------------------------------------------------------- /core/src/main/java/com/tngtech/junit/dataprovider/Preconditions.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 TNG Technology Consulting GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.tngtech.junit.dataprovider; 17 | 18 | public class Preconditions { 19 | 20 | public static T checkNotNull(T object, String errorMessage) { 21 | if (object == null) { 22 | throw new NullPointerException(errorMessage); 23 | } 24 | return object; 25 | } 26 | 27 | public static void checkArgument(boolean expression, String errorMessage) { 28 | if (!expression) { 29 | throw new IllegalArgumentException(errorMessage); 30 | } 31 | } 32 | 33 | public static void checkArgument(boolean expression, String errorMessageFormat, Object... errorMessageArgs) { 34 | if (!expression) { 35 | throw new IllegalArgumentException(String.format(errorMessageFormat, errorMessageArgs)); 36 | } 37 | } 38 | 39 | public static void checkState(boolean expression, String errorMessage) { 40 | if (!expression) { 41 | throw new IllegalStateException(errorMessage); 42 | } 43 | } 44 | 45 | public static void checkState(boolean expression, String errorMessageFormat, Object... errorMessageArgs) { 46 | if (!expression) { 47 | throw new IllegalStateException(String.format(errorMessageFormat, errorMessageArgs)); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /junit4/src/main/java/com/tngtech/java/junit/dataprovider/internal/convert/AbstractObjectConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 TNG Technology Consulting GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.tngtech.java.junit.dataprovider.internal.convert; 17 | 18 | import static com.tngtech.java.junit.dataprovider.common.Preconditions.checkArgument; 19 | import static com.tngtech.java.junit.dataprovider.common.Preconditions.checkNotNull; 20 | 21 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 22 | 23 | @SuppressFBWarnings(value = "NM_SAME_SIMPLE_NAME_AS_SUPERCLASS", justification = "for backwards compatibility and easier migration to core") 24 | public abstract class AbstractObjectConverter 25 | extends com.tngtech.junit.dataprovider.convert.AbstractObjectConverter { 26 | 27 | @Override 28 | public abstract Object[] convert(V data, boolean isVarargs, Class[] parameterTypes); 29 | 30 | @Override 31 | protected void checkIfArgumentsMatchParameterTypes(Object[] arguments, Class[] parameterTypes) { 32 | checkNotNull(arguments, "'arguments' must not be null"); 33 | checkNotNull(parameterTypes, "'testMethod' must not be null"); 34 | checkArgument(parameterTypes.length == arguments.length, "Expected %d arguments for test method but got %d.", 35 | parameterTypes.length, arguments.length); 36 | 37 | super.checkIfArgumentsMatchParameterTypes(arguments, parameterTypes); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/groovy/com/tngtech/test/java/junit/dataprovider/GroovyAcceptanceTest.groovy: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.java.junit.dataprovider 2 | 3 | import com.tngtech.junit.dataprovider.DataProvider 4 | import com.tngtech.junit.dataprovider.UseDataProvider 5 | import org.junit.jupiter.params.ParameterizedTest 6 | 7 | class GroovyAcceptanceTest { 8 | 9 | @DataProvider 10 | static List> dataProviderBooleanLogicAnd() { 11 | // @formatter:off 12 | return [ 13 | [ false, false, false ], 14 | [ true, false, false ], 15 | [ false, true, false ], 16 | [ true, true, true ], 17 | ] 18 | // @formatter:on 19 | } 20 | 21 | @ParameterizedTest 22 | @UseDataProvider('dataProviderBooleanLogicAnd') 23 | void "test boolean logic for 'and'"(op1, op2, expected) { 24 | // Expect: 25 | assert (op1 && op2) == expected 26 | } 27 | 28 | @DataProvider 29 | static List> dataProviderBooleanLogicOr() { 30 | // @formatter:off 31 | return [ 32 | [ false, false, false ], 33 | [ true, false, true ], 34 | [ false, true, true ], 35 | [ true, true, true ], 36 | ] 37 | // @formatter:on 38 | } 39 | 40 | @ParameterizedTest 41 | @UseDataProvider('dataProviderBooleanLogicOr') 42 | void "test boolean logic for 'or'"(op1, op2, expected) { 43 | // Expect: 44 | assert (op1 || op2) == expected 45 | } 46 | 47 | // @formatter:off 48 | @ParameterizedTest 49 | @DataProvider([ 50 | 'false, false, false', 51 | 'true, false, true ', 52 | 'false, true, true ', 53 | 'true, true, false' 54 | ]) 55 | // @formatter:on 56 | void "test boolean logic for 'xor'"(boolean op1, boolean op2, boolean expected) { 57 | // Expect: 58 | assert ((op1 || op2) && (op1 != op2)) == expected 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/DataproviderMethodParameterAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | import static org.assertj.core.util.Preconditions.checkNotNull; 5 | 6 | import java.lang.annotation.ElementType; 7 | import java.lang.annotation.Retention; 8 | import java.lang.annotation.RetentionPolicy; 9 | import java.lang.annotation.Target; 10 | 11 | import org.junit.jupiter.api.TestInfo; 12 | import org.junit.jupiter.api.TestReporter; 13 | import org.junit.jupiter.api.TestTemplate; 14 | import org.junit.jupiter.api.extension.ExtendWith; 15 | 16 | import com.tngtech.junit.dataprovider.DataProvider; 17 | import com.tngtech.junit.dataprovider.UseDataProvider; 18 | import com.tngtech.junit.dataprovider.UseDataProviderExtension; 19 | 20 | @ExtendWith(UseDataProviderExtension.class) 21 | class DataproviderMethodParameterAcceptanceTest { 22 | 23 | @Retention(RetentionPolicy.RUNTIME) 24 | @Target(ElementType.METHOD) 25 | @interface ExternalFile { 26 | enum Format { 27 | CSV, 28 | XML, 29 | XLS; 30 | } 31 | 32 | Format format(); 33 | 34 | String value(); 35 | } 36 | 37 | @DataProvider 38 | static Object[][] loadFromExternalFile(TestInfo testInfo, TestReporter testReporter) { 39 | checkNotNull(testInfo, "'testInfo' is not set"); 40 | checkNotNull(testReporter, "'testReporter' is not set"); 41 | 42 | String testDataFile = testInfo.getTestMethod().get().getAnnotation(ExternalFile.class).value(); 43 | // Load the data from the external file here ... 44 | return new Object[][] { { testDataFile } }; 45 | } 46 | 47 | @TestTemplate 48 | @UseDataProvider("loadFromExternalFile") 49 | @ExternalFile(format = ExternalFile.Format.CSV, value = "testdata.csv") 50 | void testThatUsesUniversalDataProvider(String testData) { 51 | // Expect: 52 | assertThat(testData).isEqualTo("testdata.csv"); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/NewlineAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import org.junit.jupiter.api.TestTemplate; 4 | import org.junit.jupiter.api.extension.ExtendWith; 5 | 6 | import com.tngtech.junit.dataprovider.DataProvider; 7 | import com.tngtech.junit.dataprovider.DataProviderExtension; 8 | import com.tngtech.junit.dataprovider.UseDataProvider; 9 | import com.tngtech.junit.dataprovider.UseDataProviderExtension; 10 | 11 | @ExtendWith(DataProviderExtension.class) 12 | @ExtendWith(UseDataProviderExtension.class) 13 | class NewlineAcceptanceTest { 14 | 15 | private static class NewlinesInToString { 16 | private final int lines; 17 | 18 | NewlinesInToString(int lines) { 19 | this.lines = lines; 20 | } 21 | 22 | @Override 23 | public String toString() { 24 | StringBuilder b = new StringBuilder(); 25 | for (int i = 0; i < lines; i++) { 26 | b.append("Line ").append(i).append("\n"); 27 | } 28 | return b.toString(); 29 | } 30 | 31 | } 32 | 33 | @DataProvider 34 | static Object[][] dataProviderNewlinesWithinParameters() { 35 | // @formatter:off 36 | return new Object[][] { 37 | { new NewlinesInToString(0) }, 38 | { new NewlinesInToString(1) }, 39 | { new NewlinesInToString(2) }, 40 | { new NewlinesInToString(3) }, 41 | }; 42 | // @formatter:on 43 | } 44 | 45 | @TestTemplate 46 | @UseDataProvider("dataProviderNewlinesWithinParameters") 47 | void testNewlinesWithinParameters(@SuppressWarnings("unused") NewlinesInToString object) { 48 | // Check output within IDE 49 | } 50 | 51 | @TestTemplate 52 | @DataProvider({ "Do it.\nOr let it." }) 53 | void testWithStringContainingTabsNewlineAndCarriageReturn(@SuppressWarnings("unused") String string) { 54 | // nothing to do => Just look at the test output in Eclispe's JUnit view if it is displayed correctly 55 | } 56 | } -------------------------------------------------------------------------------- /junit4/src/main/java/com/tngtech/java/junit/dataprovider/internal/convert/ObjectArrayConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 TNG Technology Consulting GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.tngtech.java.junit.dataprovider.internal.convert; 17 | 18 | import static com.tngtech.junit.dataprovider.Preconditions.checkArgument; 19 | import static com.tngtech.junit.dataprovider.Preconditions.checkNotNull; 20 | 21 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 22 | 23 | @SuppressFBWarnings(value = "NM_SAME_SIMPLE_NAME_AS_SUPERCLASS", justification = "for backwards compatibility and easier migration to core") 24 | public class ObjectArrayConverter extends com.tngtech.junit.dataprovider.convert.ObjectArrayConverter { 25 | 26 | @Override 27 | public Object[] convert(Object[] data, boolean isVarargs, Class[] parameterTypes) { 28 | return super.convert(data, isVarargs, parameterTypes); 29 | } 30 | 31 | @Override 32 | protected void checkIfArgumentsMatchParameterTypes(Object[] arguments, Class[] parameterTypes) { 33 | checkNotNull(arguments, "'arguments' must not be null"); 34 | checkNotNull(parameterTypes, "'testMethod' must not be null"); 35 | checkArgument(parameterTypes.length == arguments.length, 36 | "Expected %d arguments for test method but got %d parameters.", arguments.length, 37 | parameterTypes.length); 38 | super.checkIfArgumentsMatchParameterTypes(arguments, parameterTypes); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/CacheDataProviderResultsGloballyAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.java.junit.dataprovider; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | 8 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 9 | import com.tngtech.java.junit.dataprovider.UseDataProvider; 10 | 11 | @RunWith(DataProviderRunner.class) 12 | public class CacheDataProviderResultsGloballyAcceptanceTest { 13 | 14 | @Test 15 | @UseDataProvider(value = "dataProviderCachedDataProviderResults", location = CacheDataProviderResultsAcceptanceTest.class) 16 | public void testCachedDataProviderResultsOne(int noOfDataProvderCalls) { 17 | // Expected: 18 | assertThat(noOfDataProvderCalls).isEqualTo(1); 19 | } 20 | 21 | @Test 22 | @UseDataProvider(value = "dataProviderCachedDataProviderResults", location = CacheDataProviderResultsAcceptanceTest.class) 23 | public void testCachedDataProviderResultsTwo(int noOfDataProvderCalls) { 24 | // Expected: 25 | assertThat(noOfDataProvderCalls).isEqualTo(1); 26 | } 27 | 28 | @Test 29 | @UseDataProvider(value = "dataProviderDoNotCacheDataProviderResults", location = CacheDataProviderResultsAcceptanceTest.class) 30 | public void testDoNotCacheDataProviderResultsOne(int noOfDataProvderCalls) { 31 | // Expected: 32 | assertThat(noOfDataProvderCalls).isEqualTo( 33 | CacheDataProviderResultsAcceptanceTest.noOfTestsCallsUsingNotCachedDataProvider.incrementAndGet()); 34 | } 35 | 36 | @Test 37 | @UseDataProvider(value = "dataProviderDoNotCacheDataProviderResults", location = CacheDataProviderResultsAcceptanceTest.class) 38 | public void testDoNotCacheCachedDataProviderResultsTwo(int noOfDataProvderCalls) { 39 | // Expected: 40 | assertThat(noOfDataProvderCalls).isEqualTo( 41 | CacheDataProviderResultsAcceptanceTest.noOfTestsCallsUsingNotCachedDataProvider.incrementAndGet()); 42 | } 43 | } -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/CacheDataProviderResultsGloballyAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import org.junit.jupiter.api.TestTemplate; 6 | import org.junit.jupiter.api.extension.ExtendWith; 7 | 8 | import com.tngtech.junit.dataprovider.UseDataProvider; 9 | import com.tngtech.junit.dataprovider.UseDataProviderExtension; 10 | 11 | @ExtendWith(UseDataProviderExtension.class) 12 | class CacheDataProviderResultsGloballyAcceptanceTest { 13 | 14 | @TestTemplate 15 | @UseDataProvider(value = "dataProviderCachedDataProviderResults", location = CacheDataProviderResultsAcceptanceTest.class) 16 | void testCachedDataProviderResultsOne(int noOfDataProvderCalls) { 17 | // Expected: 18 | assertThat(noOfDataProvderCalls).isEqualTo(1); 19 | } 20 | 21 | @TestTemplate 22 | @UseDataProvider(value = "dataProviderCachedDataProviderResults", location = CacheDataProviderResultsAcceptanceTest.class) 23 | void testCachedDataProviderResultsTwo(int noOfDataProvderCalls) { 24 | // Expected: 25 | assertThat(noOfDataProvderCalls).isEqualTo(1); 26 | } 27 | 28 | @TestTemplate 29 | @UseDataProvider(value = "dataProviderDoNotCacheDataProviderResults", location = CacheDataProviderResultsAcceptanceTest.class) 30 | public void testDoNotCacheDataProviderResultsOne(int noOfDataProvderCalls) { 31 | // Expected: 32 | assertThat(noOfDataProvderCalls).isEqualTo( 33 | CacheDataProviderResultsAcceptanceTest.noOfTestsCallsUsingNotCachedDataProvider.incrementAndGet()); 34 | } 35 | 36 | @TestTemplate 37 | @UseDataProvider(value = "dataProviderDoNotCacheDataProviderResults", location = CacheDataProviderResultsAcceptanceTest.class) 38 | public void testDoNotCacheCachedDataProviderResultsTwo(int noOfDataProvderCalls) { 39 | // Expected: 40 | assertThat(noOfDataProvderCalls).isEqualTo( 41 | CacheDataProviderResultsAcceptanceTest.noOfTestsCallsUsingNotCachedDataProvider.incrementAndGet()); 42 | } 43 | } -------------------------------------------------------------------------------- /junit-jupiter/src/main/java/com/tngtech/junit/dataprovider/DataProviderExtension.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 TNG Technology Consulting GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.tngtech.junit.dataprovider; 17 | 18 | import org.junit.platform.commons.support.ReflectionSupport; 19 | 20 | import com.tngtech.junit.dataprovider.convert.ConverterContext; 21 | 22 | /** 23 | * Default implementation for using a dataprovider that provides the data directly within itself. 24 | */ 25 | public class DataProviderExtension extends DataProviderInvocationContextProvider { 26 | 27 | DataProviderExtension() { 28 | super(DataProvider.class); 29 | } 30 | 31 | @Override 32 | protected Object getData(DataProvider dataProvider) { 33 | return dataProvider.value(); 34 | } 35 | 36 | @Override 37 | protected ConverterContext getConverterContext(DataProvider dataProvider) { 38 | return new ConverterContext(ReflectionSupport.newInstance(dataProvider.objectArrayConverter()), 39 | ReflectionSupport.newInstance(dataProvider.singleArgConverter()), 40 | ReflectionSupport.newInstance(dataProvider.stringConverter()), dataProvider.splitBy(), 41 | dataProvider.convertNulls(), dataProvider.trimValues(), dataProvider.ignoreEnumCase()); 42 | } 43 | 44 | @Override 45 | protected DisplayNameContext getDisplayNameContext(DataProvider dataProvider) { 46 | return new DisplayNameContext(dataProvider.formatter(), dataProvider.format(), getDefaultPlaceholders()); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /junit4/src/integTest/groovy/com/tngtech/test/java/junit/dataprovider/DataProviderGroovyAcceptanceTest.groovy: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.java.junit.dataprovider 2 | 3 | import org.junit.Test 4 | import org.junit.runner.RunWith 5 | 6 | import com.tngtech.java.junit.dataprovider.DataProvider 7 | import com.tngtech.java.junit.dataprovider.DataProviderRunner 8 | import com.tngtech.java.junit.dataprovider.UseDataProvider 9 | 10 | @RunWith(DataProviderRunner) 11 | class DataProviderGroovyAcceptanceTest { 12 | 13 | @DataProvider 14 | static List> dataProviderBooleanLogicAnd() { 15 | // @formatter:off 16 | return [ 17 | [ false, false, false ], 18 | [ true, false, false ], 19 | [ false, true, false ], 20 | [ true, true, true ], 21 | ] 22 | // @formatter:on 23 | } 24 | 25 | @Test 26 | @UseDataProvider('dataProviderBooleanLogicAnd') 27 | void "test boolean logic for 'and'"(op1, op2, expected) { 28 | // Expect: 29 | assert (op1 && op2) == expected 30 | } 31 | 32 | @DataProvider 33 | static List> dataProviderBooleanLogicOr() { 34 | // @formatter:off 35 | return [ 36 | [ false, false, false ], 37 | [ true, false, true ], 38 | [ false, true, true ], 39 | [ true, true, true ], 40 | ] 41 | // @formatter:on 42 | } 43 | 44 | @Test 45 | @UseDataProvider('dataProviderBooleanLogicOr') 46 | void "test boolean logic for 'or'"(op1, op2, expected) { 47 | // Expect: 48 | assert (op1 || op2) == expected 49 | } 50 | 51 | // @formatter:off 52 | @Test 53 | @DataProvider([ 54 | 'false, false, false', 55 | 'true, false, true ', 56 | 'false, true, true ', 57 | 'true, true, false' 58 | ]) 59 | // @formatter:on 60 | void "test boolean logic for 'xor'"(boolean op1, boolean op2, boolean expected) { 61 | // Expect: 62 | assert ((op1 || op2) && (op1 != op2)) == expected 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/converter/CustomConverterDataProvider.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.converter; 2 | 3 | 4 | import java.lang.annotation.Documented; 5 | import java.lang.annotation.ElementType; 6 | import java.lang.annotation.Retention; 7 | import java.lang.annotation.RetentionPolicy; 8 | import java.lang.annotation.Target; 9 | 10 | import org.junit.jupiter.params.provider.ArgumentsSource; 11 | 12 | import com.tngtech.junit.dataprovider.DataProvider; 13 | import com.tngtech.junit.dataprovider.convert.ObjectArrayConverter; 14 | import com.tngtech.junit.dataprovider.convert.SingleArgConverter; 15 | import com.tngtech.junit.dataprovider.convert.StringConverter; 16 | 17 | /** 18 | * @see DataProvider 19 | */ 20 | @Documented 21 | @Retention(RetentionPolicy.RUNTIME) 22 | @Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD }) 23 | @ArgumentsSource(CustomConverterDataProviderArgumentProvider.class) 24 | @interface CustomConverterDataProvider { 25 | 26 | /** 27 | * @see DataProvider#value() 28 | */ 29 | String[] value() default {}; 30 | 31 | /** 32 | * @see DataProvider#splitBy() 33 | */ 34 | String splitBy() default DataProvider.COMMA; 35 | 36 | /** 37 | * @see DataProvider#convertNulls() 38 | */ 39 | boolean convertNulls() default true; 40 | 41 | /** 42 | * @see DataProvider#trimValues() 43 | */ 44 | boolean trimValues() default true; 45 | 46 | /** 47 | * @see DataProvider#ignoreEnumCase() 48 | */ 49 | boolean ignoreEnumCase() default false; 50 | 51 | /** 52 | * @see DataProvider#objectArrayConverter() 53 | */ 54 | Class objectArrayConverter() default ObjectArrayConverter.class; 55 | 56 | /** 57 | * @see DataProvider#singleArgConverter() 58 | */ 59 | Class singleArgConverter() default SingleArgConverter.class; 60 | 61 | /** 62 | * @see DataProvider#stringConverter() 63 | */ 64 | Class stringConverter() default DateTimeAwareStringConverter.class; 65 | } 66 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/AdditionalTestMethodParameterAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import org.junit.jupiter.api.TestInfo; 6 | import org.junit.jupiter.api.TestTemplate; 7 | import org.junit.jupiter.api.extension.ExtendWith; 8 | 9 | import com.tngtech.junit.dataprovider.DataProvider; 10 | import com.tngtech.junit.dataprovider.DataProviderExtension; 11 | import com.tngtech.junit.dataprovider.UseDataProvider; 12 | import com.tngtech.junit.dataprovider.UseDataProviderExtension; 13 | 14 | @ExtendWith(DataProviderExtension.class) 15 | @ExtendWith(UseDataProviderExtension.class) 16 | class AdditionalTestMethodParameterAcceptanceTest { 17 | 18 | @DataProvider 19 | static Object[][] dataProviderObjectArrayArray() { 20 | // @formatter:off 21 | return new Object[][] { 22 | { 0, 1, "0, 1" }, 23 | { 'a', 'b', "a, b" }, 24 | }; 25 | // @formatter:on 26 | } 27 | 28 | @TestTemplate 29 | @UseDataProvider 30 | void testObjectArrayArray(Object a, Object b, String expected, TestInfo testInfo) { 31 | // Expect: 32 | assertThat(testInfo.getDisplayName()).contains(expected); 33 | } 34 | 35 | @DataProvider 36 | static Object[] dataProviderObjectArray() { 37 | // @formatter:off 38 | return new Object[] { 39 | 'a', 40 | "a", 41 | }; 42 | // @formatter:on 43 | } 44 | 45 | @TestTemplate 46 | @UseDataProvider 47 | void testObjectArray(Object a, TestInfo testInfo) { 48 | // Expect: 49 | assertThat(testInfo.getDisplayName()).contains("a"); 50 | } 51 | 52 | @TestTemplate 53 | // @formatter:off 54 | @DataProvider(value = { 55 | "2 | 3 | 2, 3", 56 | "c | d | c, d", 57 | }, splitBy = "\\|") 58 | // @formatter:on 59 | void testThree(String a, String b, String expected, TestInfo testInfo) { 60 | // Expect: 61 | assertThat(testInfo.getDisplayName()).contains(expected); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /junit4/src/main/java/com/tngtech/java/junit/dataprovider/internal/convert/SingleArgConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 TNG Technology Consulting GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.tngtech.java.junit.dataprovider.internal.convert; 17 | 18 | import static com.tngtech.junit.dataprovider.Preconditions.checkArgument; 19 | import static com.tngtech.junit.dataprovider.Preconditions.checkNotNull; 20 | 21 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 22 | 23 | @SuppressFBWarnings(value = "NM_SAME_SIMPLE_NAME_AS_SUPERCLASS", justification = "for backwards compatibility and easier migration to core") 24 | public class SingleArgConverter extends com.tngtech.junit.dataprovider.convert.SingleArgConverter { 25 | 26 | @Override 27 | public Object[] convert(Object data, boolean isVarargs, Class[] parameterTypes) { 28 | checkArgument(parameterTypes.length == 1, "Object[] dataprovider just supports single argument test method but found %d parameters", 29 | parameterTypes.length); 30 | return super.convert(data, isVarargs, parameterTypes); 31 | } 32 | 33 | @Override 34 | protected void checkIfArgumentsMatchParameterTypes(Object[] arguments, Class[] parameterTypes) { 35 | checkNotNull(arguments, "'arguments' must not be null"); 36 | checkNotNull(parameterTypes, "'testMethod' must not be null"); 37 | checkArgument(parameterTypes.length == arguments.length, 38 | "Expected %d arguments for test method but got %d parameters.", arguments.length, 39 | parameterTypes.length); 40 | super.checkIfArgumentsMatchParameterTypes(arguments, parameterTypes); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are very welcome. The following will provide some helpful guidelines. 4 | 5 | 6 | ## JUnit dataprovider Contributor License Agreement 7 | 8 | * You will only submit contributions using your real name (sorry, no pseudonyms or anonymous contributions). 9 | * You will only submit contributions where you have authored 100% of the content. 10 | * You will only submit contributions to which you have the necessary rights. 11 | This means in particular, that if you are employed you have received the necessary permissions 12 | from your employer to make the contributions. 13 | * Whatever content you contribute will be provided under the project license(s) (see [LICENSE](LICENSE)) 14 | 15 | 16 | ## Accept Developer Certificate of Origin 17 | 18 | In order for your contributions to be accepted, you must [sign off](https://git-scm.com/docs/git-commit#git-commit---signoff) 19 | your Git commits to indicate that you agree to the terms of [Developer Certificate of Origin](https://developercertificate.org/). 20 | 21 | 22 | ## Follow the Code of Conduct 23 | 24 | Contributors must follow the Code of Conduct outlined at [CODE-OF-CONDUCT.md](CODE-OF-CONDUCT.md). 25 | 26 | 27 | ## How to contribute 28 | 29 | We love pull requests. Here is a quick guide: 30 | 31 | 1. You need to have a JDK (at least version 1.8) installed. 32 | 2. Fork the repo (see https://help.github.com/articles/fork-a-repo). 33 | 3. Create a new branch from `main`. 34 | 4. Ensure that you have a clean state by running `./gradlew clean build`. 35 | 5. Add your change together with a test (tests are not needed for refactorings and documentation changes). 36 | 6. Run `./gradlew clean build` again and ensure all tests are passing. 37 | 7. Push to your fork/branch and submit a pull request. 38 | 8. Add the following to your Pull Request description: 39 | ``` 40 | --- 41 | 42 | I hereby agree to the terms of the JUnit dataprovider Contributor License Agreement. 43 | ``` 44 | 9. Now you are waiting on us. We review your pull request and at least leave some comments. 45 | 46 | *Note:* If you are thinking of providing a fix for one of the bugs or feature requests, it is usually 47 | a good idea to add a comment to the bug to make sure that there is agreement on how we should proceed. 48 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/NestedAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static com.tngtech.junit.dataprovider.DataProviders.$; 4 | import static com.tngtech.junit.dataprovider.DataProviders.$$; 5 | import static org.assertj.core.api.Assertions.assertThat; 6 | 7 | import org.junit.jupiter.api.BeforeEach; 8 | import org.junit.jupiter.api.Nested; 9 | import org.junit.jupiter.params.ParameterizedTest; 10 | 11 | import com.tngtech.junit.dataprovider.DataProvider; 12 | import com.tngtech.junit.dataprovider.UseDataProvider; 13 | 14 | class NestedAcceptanceTest { 15 | 16 | private int zero = -1; 17 | 18 | @BeforeEach 19 | public void setup() { 20 | zero = 0; 21 | } 22 | 23 | @DataProvider 24 | static Object[][] dataProviderAdd() { 25 | //@formatter:off 26 | return new Object[][] { 27 | { 0, 0, 0 }, 28 | { 0, 1, 1 }, 29 | { 1, 0, 1 }, 30 | { 1, 1, 2 }, 31 | 32 | { 0, -1, -1 }, 33 | { -1, -1, -2 }, 34 | }; 35 | //@formatter:on 36 | } 37 | 38 | @ParameterizedTest 39 | @UseDataProvider 40 | void testAdd(int a, int b, int expected) { 41 | // Expect: 42 | assertThat(a + b).isEqualTo(expected); 43 | 44 | assertThat(zero).isEqualTo(0); 45 | } 46 | 47 | @DataProvider 48 | static Object[][] dataProviderMinus() { 49 | // @formatter:off 50 | return $$( 51 | $( 0, 0, 0 ), 52 | $( 0, 1, -1 ), 53 | $( 0, -1, 1 ), 54 | $( 1, 0, 1 ), 55 | $( 1, 1, 0 ), 56 | $( -1, 0, -1 ), 57 | $( -1, -1, 0 ) 58 | ); 59 | // @formatter:on 60 | } 61 | 62 | @Nested 63 | class NestedTests { 64 | @ParameterizedTest 65 | @UseDataProvider(location = NestedAcceptanceTest.class) 66 | void testMinus(long a, long b, long expected) { 67 | // When: 68 | long result = a - b; 69 | 70 | // Then: 71 | assertThat(result).isEqualTo(expected); 72 | assertThat(zero).isEqualTo(0); 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /junit-jupiter/src/main/java/com/tngtech/junit/dataprovider/DataProviderParameterResolver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 TNG Technology Consulting GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.tngtech.junit.dataprovider; 17 | 18 | import static com.tngtech.junit.dataprovider.Preconditions.checkNotNull; 19 | 20 | import java.lang.reflect.Executable; 21 | import java.lang.reflect.Method; 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | import org.junit.jupiter.api.extension.ExtensionContext; 26 | import org.junit.jupiter.api.extension.ParameterContext; 27 | import org.junit.jupiter.api.extension.ParameterResolutionException; 28 | import org.junit.jupiter.api.extension.ParameterResolver; 29 | 30 | class DataProviderParameterResolver implements ParameterResolver { 31 | 32 | private final List arguments; 33 | 34 | DataProviderParameterResolver(List arguments) { 35 | this.arguments = new ArrayList<>(checkNotNull(arguments, "'arguments' must not be null")); 36 | } 37 | 38 | @Override 39 | public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { 40 | Executable declaringExecutable = parameterContext.getParameter().getDeclaringExecutable(); 41 | Method testMethod = extensionContext.getTestMethod().orElse(null); 42 | return declaringExecutable.equals(testMethod) && parameterContext.getIndex() < arguments.size(); 43 | } 44 | 45 | @Override 46 | public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) 47 | throws ParameterResolutionException { 48 | return arguments.get(parameterContext.getIndex()); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /junit-jupiter/src/main/java/com/tngtech/junit/dataprovider/DisplayNameContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 TNG Technology Consulting GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.tngtech.junit.dataprovider; 17 | 18 | import static com.tngtech.junit.dataprovider.Preconditions.checkNotNull; 19 | import static java.util.Collections.unmodifiableList; 20 | 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | 24 | import com.tngtech.junit.dataprovider.format.DataProviderTestNameFormatter; 25 | import com.tngtech.junit.dataprovider.placeholder.BasePlaceholder; 26 | 27 | public class DisplayNameContext { 28 | 29 | private final Class formatter; 30 | private final String format; 31 | private final List placeholders; 32 | 33 | public DisplayNameContext(String format, List placeholders) { 34 | this(null, format, placeholders); 35 | } 36 | 37 | public DisplayNameContext(Class formatter, String format, 38 | List placeholders) { 39 | this.formatter = formatter; 40 | this.format = checkNotNull(format, "'format' must not be null"); 41 | this.placeholders = new ArrayList<>(checkNotNull(placeholders, "'placeholders' must not be null")); 42 | } 43 | 44 | public Class getFormatter() { 45 | return formatter; 46 | } 47 | 48 | public String getFormat() { 49 | return format; 50 | } 51 | 52 | public List getPlaceholders() { 53 | return unmodifiableList(placeholders); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /core/src/test/java/com/tngtech/junit/dataprovider/convert/ConverterContextTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.junit.dataprovider.convert; 2 | 3 | import org.junit.Rule; 4 | import org.junit.Test; 5 | import org.junit.rules.ExpectedException; 6 | 7 | public class ConverterContextTest { 8 | 9 | @SuppressWarnings("deprecation") 10 | @Rule 11 | public ExpectedException expectedException = ExpectedException.none(); 12 | 13 | @Test 14 | public void testConverterContextShouldThrowNullPointerExceptionIfObjectArrayConverterIsNull() { 15 | // Given 16 | expectedException.expect(NullPointerException.class); 17 | expectedException.expectMessage("'objectArrayConverter' must not be null"); 18 | 19 | // When: 20 | new ConverterContext(null, new SingleArgConverter(), new StringConverter(), "\\|", false, false, false); 21 | 22 | // Then: expect exception 23 | } 24 | 25 | @Test 26 | public void testConverterContextShouldThrowNullPointerExceptionIfSingleArgConverterIsNull() { 27 | // Given 28 | expectedException.expect(NullPointerException.class); 29 | expectedException.expectMessage("'singleArgConverter' must not be null"); 30 | 31 | // When: 32 | new ConverterContext(new ObjectArrayConverter(), null, new StringConverter(), "\\|", false, false, false); 33 | 34 | // Then: expect exception 35 | } 36 | 37 | @Test 38 | public void testConverterContextShouldThrowNullPointerExceptionIfStringConverterIsNull() { 39 | // Given 40 | expectedException.expect(NullPointerException.class); 41 | expectedException.expectMessage("'stringConverter' must not be null"); 42 | 43 | // When: 44 | new ConverterContext(new ObjectArrayConverter(), new SingleArgConverter(), null, "\\|", false, false, false); 45 | 46 | // Then: expect exception 47 | } 48 | 49 | @Test 50 | public void testConverterContextShouldThrowNullPointerExceptionIfSplitByIsNull() { 51 | // Given 52 | expectedException.expect(NullPointerException.class); 53 | expectedException.expectMessage("'splitBy' must not be null"); 54 | 55 | // When: 56 | new ConverterContext(null, false, false, false); 57 | 58 | // Then: expect exception 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/DataProviderListArgAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.java.junit.dataprovider; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.util.Arrays; 6 | import java.util.Collections; 7 | import java.util.List; 8 | 9 | import org.junit.Test; 10 | import org.junit.runner.RunWith; 11 | 12 | import com.tngtech.java.junit.dataprovider.DataProvider; 13 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 14 | import com.tngtech.java.junit.dataprovider.UseDataProvider; 15 | 16 | @RunWith(DataProviderRunner.class) 17 | public class DataProviderListArgAcceptanceTest { 18 | 19 | @DataProvider 20 | public static Object[][] dataProviderListArg() { 21 | // @formatter:off 22 | return new Object[][] { 23 | { Arrays.asList("a", "b"), "c" }, 24 | }; 25 | // @formatter:on 26 | } 27 | 28 | @Test 29 | @UseDataProvider 30 | public void testListArg(List list, String string) { 31 | // Expected: 32 | assertThat(list).doesNotContain(string); 33 | } 34 | 35 | @DataProvider 36 | public static Object[][] stringsData() { 37 | return new Object[][] { 38 | { Arrays.asList("string1", "stringValue"), "stringValue" } 39 | }; 40 | } 41 | 42 | @Test 43 | @UseDataProvider("stringsData") 44 | public void test(List strings, String expectedValue) { 45 | // Expected: 46 | assertThat(strings).contains(expectedValue); 47 | } 48 | 49 | public interface UnaryOperator { 50 | T apply(T arg); 51 | } 52 | 53 | @DataProvider 54 | public static List>> listOfListOfUnaryOperator() { 55 | return Collections.singletonList(Collections.>singletonList(new UnaryOperator() { 56 | @Override 57 | public String apply(String arg) { 58 | return "merged-" + arg; 59 | } 60 | })); 61 | } 62 | 63 | @Test 64 | @UseDataProvider("listOfListOfUnaryOperator") 65 | public void testListOfListOfUnaryOperator(UnaryOperator operator) { 66 | assertThat(operator.apply("test")).isEqualTo("merged-test"); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /junit4/src/test/java/com/tngtech/java/junit/dataprovider/internal/convert/SingleArgConverterTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.java.junit.dataprovider.internal.convert; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.mockito.InjectMocks; 8 | import org.mockito.junit.MockitoJUnitRunner; 9 | 10 | import com.tngtech.java.junit.dataprovider.BaseTest; 11 | 12 | @RunWith(MockitoJUnitRunner.class) 13 | public class SingleArgConverterTest extends BaseTest { 14 | 15 | @InjectMocks 16 | private SingleArgConverter underTest; 17 | 18 | @Test(expected = IllegalArgumentException.class) 19 | public void testConvertShouldThrowIllegalArgumentExceptionIfParameterTypesSizeIsZero() { 20 | // Given: 21 | Object data = 1; 22 | Class[] parameterTypes = new Class[] {}; 23 | 24 | // When: 25 | underTest.convert(data, false, parameterTypes); 26 | 27 | // Then: expect exception 28 | } 29 | 30 | @Test(expected = IllegalArgumentException.class) 31 | public void testConvertShouldThrowIllegalArgumentExceptionIfParameterTypesSizeGreaterThanOne() { 32 | // Given: 33 | Object data = 2L; 34 | Class[] parameterTypes = new Class[] { int.class, String.class, Long.class }; 35 | 36 | // When: 37 | underTest.convert(data, false, parameterTypes); 38 | 39 | // Then: expect exception 40 | } 41 | 42 | @Test(expected = IllegalArgumentException.class) 43 | public void testConvertShouldThrowIllegalArgumentExceptionIfVarargsIsTrue() { 44 | // Given: 45 | Object data = 3.0; 46 | Class[] parameterTypes = new Class[] { double.class }; 47 | 48 | // When: 49 | underTest.convert(data, true, parameterTypes); 50 | 51 | // Then: expect exception 52 | } 53 | 54 | @Test 55 | public void testConvertShouldWrapInputIntoArryAndReturnItIfPreconditionsMet() { 56 | // Given: 57 | Object data = "4"; 58 | Class[] parameterTypes = new Class[] { String.class }; 59 | 60 | // When: 61 | Object[] result = underTest.convert(data, false, parameterTypes); 62 | 63 | // Then: 64 | assertThat(result).containsExactly("4"); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/meta/DataProviderTestProvider.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.meta; 2 | 3 | import static com.tngtech.junit.dataprovider.resolver.DataProviderResolverContext.generateLocations; 4 | import static java.util.Arrays.asList; 5 | 6 | import org.junit.jupiter.api.extension.ExtensionContext; 7 | import org.junit.platform.commons.support.ReflectionSupport; 8 | 9 | import com.tngtech.junit.dataprovider.AbstractUseDataProviderArgumentProvider; 10 | import com.tngtech.junit.dataprovider.DataProvider; 11 | import com.tngtech.junit.dataprovider.convert.ConverterContext; 12 | import com.tngtech.junit.dataprovider.resolver.DataProviderResolverContext; 13 | 14 | class DataProviderTestProvider extends AbstractUseDataProviderArgumentProvider { 15 | 16 | DataProviderTestProvider() { 17 | super(DataProvider.class); 18 | } 19 | 20 | @Override 21 | public void accept(DataProviderTest sourceAnnotation) { 22 | this.sourceAnnotation = sourceAnnotation; 23 | } 24 | 25 | @Override 26 | protected DataProviderResolverContext getDataProviderResolverContext(ExtensionContext extensionContext, 27 | DataProviderTest annotation) { 28 | return new DataProviderResolverContext(extensionContext.getRequiredTestMethod(), asList(annotation.resolver()), 29 | annotation.resolveStrategy(), 30 | generateLocations(extensionContext.getRequiredTestClass(), annotation.location()), 31 | DataProvider.class, annotation.value()); 32 | } 33 | 34 | @Override 35 | protected ConverterContext getConverterContext(DataProvider dataProvider) { 36 | return new ConverterContext(ReflectionSupport.newInstance(dataProvider.objectArrayConverter()), 37 | ReflectionSupport.newInstance(dataProvider.singleArgConverter()), 38 | ReflectionSupport.newInstance(dataProvider.stringConverter()), dataProvider.splitBy(), 39 | dataProvider.convertNulls(), dataProvider.trimValues(), dataProvider.ignoreEnumCase()); 40 | } 41 | 42 | @Override 43 | protected boolean cacheDataProviderResult(DataProvider dataProviderAnnotation) { 44 | return dataProviderAnnotation.cache(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/groovy/com/tngtech/test/java/junit/dataprovider/GroovyAcceptanceTest.groovy: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.java.junit.dataprovider 2 | 3 | import com.tngtech.junit.dataprovider.DataProvider 4 | import com.tngtech.junit.dataprovider.DataProviderExtension 5 | import com.tngtech.junit.dataprovider.UseDataProvider 6 | import com.tngtech.junit.dataprovider.UseDataProviderExtension 7 | import org.junit.jupiter.api.TestTemplate 8 | import org.junit.jupiter.api.extension.ExtendWith 9 | 10 | class GroovyAcceptanceTest { 11 | 12 | @DataProvider 13 | static List> dataProviderBooleanLogicAnd() { 14 | // @formatter:off 15 | return [ 16 | [ false, false, false ], 17 | [ true, false, false ], 18 | [ false, true, false ], 19 | [ true, true, true ], 20 | ] 21 | // @formatter:on 22 | } 23 | 24 | @TestTemplate 25 | @ExtendWith(UseDataProviderExtension) 26 | @UseDataProvider('dataProviderBooleanLogicAnd') 27 | void "test boolean logic for 'and'"(op1, op2, expected) { 28 | // Expect: 29 | assert (op1 && op2) == expected 30 | } 31 | 32 | @DataProvider 33 | static List> dataProviderBooleanLogicOr() { 34 | // @formatter:off 35 | return [ 36 | [ false, false, false ], 37 | [ true, false, true ], 38 | [ false, true, true ], 39 | [ true, true, true ], 40 | ] 41 | // @formatter:on 42 | } 43 | 44 | @TestTemplate 45 | @ExtendWith(UseDataProviderExtension) 46 | @UseDataProvider('dataProviderBooleanLogicOr') 47 | void "test boolean logic for 'or'"(op1, op2, expected) { 48 | // Expect: 49 | assert (op1 || op2) == expected 50 | } 51 | 52 | // @formatter:off 53 | @TestTemplate 54 | @ExtendWith(DataProviderExtension) 55 | @DataProvider([ 56 | 'false, false, false', 57 | 'true, false, true ', 58 | 'false, true, true ', 59 | 'true, true, false' 60 | ]) 61 | // @formatter:on 62 | void "test boolean logic for 'xor'"(boolean op1, boolean op2, boolean expected) { 63 | // Expect: 64 | assert ((op1 || op2) && (op1 != op2)) == expected 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/NonStaticDataProviderAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static com.tngtech.junit.dataprovider.DataProviders.$; 4 | import static com.tngtech.junit.dataprovider.DataProviders.$$; 5 | import static org.assertj.core.api.Assertions.assertThat; 6 | 7 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 8 | import org.junit.jupiter.api.Nested; 9 | import org.junit.jupiter.api.TestInstance; 10 | import org.junit.jupiter.api.TestInstance.Lifecycle; 11 | import org.junit.jupiter.params.ParameterizedTest; 12 | 13 | import com.tngtech.junit.dataprovider.DataProvider; 14 | import com.tngtech.junit.dataprovider.UseDataProvider; 15 | 16 | @TestInstance(Lifecycle.PER_CLASS) 17 | class NonStaticDataProviderAcceptanceTest { 18 | 19 | @DataProvider 20 | Object[][] dataProviderDivide() { 21 | // @formatter:off 22 | return $$( 23 | $( 0, 1, 0 ), 24 | $( 0, -1, 0 ), 25 | $( 1, 1, 1 ), 26 | $( 1, -1, -1 ), 27 | $( 2, 1, 2 ) 28 | ); 29 | // @formatter:on 30 | } 31 | 32 | @ParameterizedTest 33 | @UseDataProvider 34 | void testDivide(int a, int b, int expected) { 35 | // Given: 36 | 37 | // When: 38 | int result = a / b; 39 | 40 | // Then: 41 | assertThat(result).isEqualTo(expected); 42 | } 43 | 44 | @SuppressFBWarnings(value = "SIC_INNER_SHOULD_BE_STATIC", justification = "Must be non-static for JUnit Jupiter") 45 | @TestInstance(Lifecycle.PER_CLASS) 46 | @Nested 47 | class NestedTests { 48 | 49 | @DataProvider 50 | Object[][] dataProviderPow() { 51 | // @formatter:off 52 | return $$( 53 | $( 0, 0 ), 54 | $( 1, 1 ), 55 | $( 2, 4 ), 56 | $( 4, 16 ) 57 | ); 58 | // @formatter:on 59 | } 60 | 61 | @ParameterizedTest 62 | @UseDataProvider 63 | void testPow(int a, int expected) { 64 | // Given: 65 | 66 | // When: 67 | int result = a * a; 68 | 69 | // Then: 70 | assertThat(result).isEqualTo(expected); 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /core/src/main/java/com/tngtech/junit/dataprovider/placeholder/ArgumentPlaceholder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 TNG Technology Consulting GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.tngtech.junit.dataprovider.placeholder; 17 | 18 | import java.util.List; 19 | 20 | /** 21 | * This placeholder format the arguments of a dataprovider test as comma-separated {@link String} according to the given 22 | * index or range subscript. For a list of special argument treatments, see {@link AbstractArgumentPlaceholder}. 23 | * 24 | * @see AbstractArgumentPlaceholder 25 | */ 26 | public class ArgumentPlaceholder extends AbstractArgumentPlaceholder { 27 | 28 | public ArgumentPlaceholder() { 29 | super("%[ap]\\[(-?[0-9]+|-?[0-9]+\\.\\.-?[0-9]+)\\]"); 30 | } 31 | 32 | @Override 33 | protected String getReplacementFor(String placeholder, ReplacementData data) { 34 | FromAndTo fromAndTo = calcFromAndToForSubscriptAndArguments(placeholder, 3, data.getArguments().size()); 35 | return formatAll(data.getArguments().subList(fromAndTo.from, fromAndTo.to)); 36 | } 37 | 38 | /** 39 | * Formats the given arguments by retrieving it's {@link String} representation and separate it by comma (= 40 | * {@code ,}). 41 | * 42 | * @param arguments to be formatted 43 | * @return the {@link String} representation of the given {@link List}{@code } 44 | */ 45 | protected String formatAll(List arguments) { 46 | StringBuilder stringBuilder = new StringBuilder(); 47 | for (int i = 0; i < arguments.size(); i++) { 48 | stringBuilder.append(format(arguments.get(i))); 49 | if (i < arguments.size() - 1) { 50 | stringBuilder.append(", "); 51 | } 52 | } 53 | return stringBuilder.toString(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/NestedAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static com.tngtech.junit.dataprovider.DataProviders.$; 4 | import static com.tngtech.junit.dataprovider.DataProviders.$$; 5 | import static org.assertj.core.api.Assertions.assertThat; 6 | 7 | import org.junit.jupiter.api.BeforeEach; 8 | import org.junit.jupiter.api.Nested; 9 | import org.junit.jupiter.api.TestTemplate; 10 | import org.junit.jupiter.api.extension.ExtendWith; 11 | 12 | import com.tngtech.junit.dataprovider.DataProvider; 13 | import com.tngtech.junit.dataprovider.UseDataProvider; 14 | import com.tngtech.junit.dataprovider.UseDataProviderExtension; 15 | 16 | @ExtendWith(UseDataProviderExtension.class) 17 | class NestedAcceptanceTest { 18 | 19 | private int zero = -1; 20 | 21 | @BeforeEach 22 | public void setup() { 23 | zero = 0; 24 | } 25 | 26 | @DataProvider 27 | static Object[][] dataProviderAdd() { 28 | //@formatter:off 29 | return new Object[][] { 30 | { 0, 0, 0 }, 31 | { 0, 1, 1 }, 32 | { 1, 0, 1 }, 33 | { 1, 1, 2 }, 34 | 35 | { 0, -1, -1 }, 36 | { -1, -1, -2 }, 37 | }; 38 | //@formatter:on 39 | } 40 | 41 | @TestTemplate 42 | @UseDataProvider 43 | void testAdd(int a, int b, int expected) { 44 | // Expect: 45 | assertThat(a + b).isEqualTo(expected); 46 | 47 | assertThat(zero).isEqualTo(0); 48 | } 49 | 50 | @DataProvider 51 | static Object[][] dataProviderMinus() { 52 | // @formatter:off 53 | return $$( 54 | $( 0, 0, 0 ), 55 | $( 0, 1, -1 ), 56 | $( 0, -1, 1 ), 57 | $( 1, 0, 1 ), 58 | $( 1, 1, 0 ), 59 | $( -1, 0, -1 ), 60 | $( -1, -1, 0 ) 61 | ); 62 | // @formatter:on 63 | } 64 | 65 | @Nested 66 | class NestedTests { 67 | @TestTemplate 68 | @UseDataProvider(location = NestedAcceptanceTest.class) 69 | void testMinus(long a, long b, long expected) { 70 | // When: 71 | long result = a - b; 72 | 73 | // Then: 74 | assertThat(result).isEqualTo(expected); 75 | assertThat(zero).isEqualTo(0); 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/resolver/CustomResolverUseDataProviderArgumentProvider.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.resolver; 2 | 3 | import static com.tngtech.junit.dataprovider.resolver.DataProviderResolverContext.generateLocations; 4 | import static java.util.Arrays.asList; 5 | 6 | import org.junit.jupiter.api.extension.ExtensionContext; 7 | import org.junit.platform.commons.support.ReflectionSupport; 8 | 9 | import com.tngtech.junit.dataprovider.AbstractUseDataProviderArgumentProvider; 10 | import com.tngtech.junit.dataprovider.DataProvider; 11 | import com.tngtech.junit.dataprovider.convert.ConverterContext; 12 | import com.tngtech.junit.dataprovider.resolver.DataProviderResolverContext; 13 | 14 | class CustomResolverUseDataProviderArgumentProvider 15 | extends AbstractUseDataProviderArgumentProvider { 16 | 17 | CustomResolverUseDataProviderArgumentProvider() { 18 | super(DataProvider.class); 19 | } 20 | 21 | @Override 22 | public void accept(CustomResolverUseDataProvider sourceAnnotation) { 23 | this.sourceAnnotation = sourceAnnotation; 24 | } 25 | 26 | @Override 27 | protected DataProviderResolverContext getDataProviderResolverContext(ExtensionContext extensionContext, 28 | CustomResolverUseDataProvider annotation) { 29 | return new DataProviderResolverContext(extensionContext.getRequiredTestMethod(), asList(annotation.resolver()), 30 | annotation.resolveStrategy(), 31 | generateLocations(extensionContext.getRequiredTestClass(), annotation.location()), 32 | DataProvider.class, annotation.value()); 33 | } 34 | 35 | @Override 36 | protected ConverterContext getConverterContext(DataProvider dataProvider) { 37 | return new ConverterContext(ReflectionSupport.newInstance(dataProvider.objectArrayConverter()), 38 | ReflectionSupport.newInstance(dataProvider.singleArgConverter()), 39 | ReflectionSupport.newInstance(dataProvider.stringConverter()), dataProvider.splitBy(), 40 | dataProvider.convertNulls(), dataProvider.trimValues(), dataProvider.ignoreEnumCase()); 41 | } 42 | 43 | @Override 44 | protected boolean cacheDataProviderResult(DataProvider dataProviderAnnotation) { 45 | return dataProviderAnnotation.cache(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/ListDataProviderAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.util.ArrayList; 6 | import java.util.Collections; 7 | import java.util.List; 8 | import java.util.function.UnaryOperator; 9 | 10 | import org.junit.jupiter.params.ParameterizedTest; 11 | 12 | import com.tngtech.junit.dataprovider.DataProvider; 13 | import com.tngtech.junit.dataprovider.UseDataProvider; 14 | 15 | class ListDataProviderAcceptanceTest { 16 | 17 | @DataProvider 18 | static List> dataProviderNumberFormat() { 19 | List> result = new ArrayList<>(); 20 | List first = new ArrayList<>(); 21 | first.add(Integer.valueOf(101)); 22 | first.add("%5d"); 23 | first.add(" 101"); 24 | result.add(first); 25 | List second = new ArrayList<>(); 26 | second.add(125); 27 | second.add("%06d"); 28 | second.add("000125"); 29 | result.add(second); 30 | return result; 31 | } 32 | 33 | @ParameterizedTest 34 | @UseDataProvider("dataProviderNumberFormat") 35 | void testNumberFormat(Number number, String format, String expected) { 36 | // Given: 37 | 38 | // When: 39 | String result = String.format(format, number); 40 | 41 | // Then: 42 | assertThat(result).isEqualTo(expected); 43 | } 44 | 45 | @DataProvider 46 | static List dataProviderIsNumber() { 47 | List result = new ArrayList<>(); 48 | result.add(101); 49 | result.add(125L); 50 | result.add(125.0); 51 | return result; 52 | } 53 | 54 | @ParameterizedTest 55 | @UseDataProvider 56 | void testIsNumber(Number number) { 57 | // Expect: 58 | assertThat(number).isInstanceOf(Number.class); 59 | } 60 | 61 | @DataProvider 62 | public static List>> listOfListOfUnaryOperator() { 63 | return Collections.singletonList(Collections.singletonList((string) -> "merged-" + string)); 64 | } 65 | 66 | @ParameterizedTest 67 | @UseDataProvider("listOfListOfUnaryOperator") 68 | public void testListOfListOfUnaryOperator(UnaryOperator operator) { 69 | // Expect: 70 | assertThat(operator.apply("test")).isEqualTo("merged-test"); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/meta/DataProviderTestExtension.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.meta; 2 | 3 | import static com.tngtech.junit.dataprovider.resolver.DataProviderResolverContext.generateLocations; 4 | import static java.util.Arrays.asList; 5 | 6 | import org.junit.jupiter.api.extension.ExtensionContext; 7 | import org.junit.platform.commons.support.ReflectionSupport; 8 | 9 | import com.tngtech.junit.dataprovider.DataProvider; 10 | import com.tngtech.junit.dataprovider.DisplayNameContext; 11 | import com.tngtech.junit.dataprovider.UseDataProviderInvocationContextProvider; 12 | import com.tngtech.junit.dataprovider.convert.ConverterContext; 13 | import com.tngtech.junit.dataprovider.resolver.DataProviderResolverContext; 14 | 15 | class DataProviderTestExtension extends UseDataProviderInvocationContextProvider { 16 | 17 | DataProviderTestExtension() { 18 | super(DataProviderTest.class, DataProvider.class); 19 | } 20 | 21 | @Override 22 | protected DataProviderResolverContext getDataProviderResolverContext(ExtensionContext extensionContext, 23 | DataProviderTest annotation) { 24 | return new DataProviderResolverContext(extensionContext.getRequiredTestMethod(), asList(annotation.resolver()), 25 | annotation.resolveStrategy(), 26 | generateLocations(extensionContext.getRequiredTestClass(), annotation.location()), DataProvider.class, 27 | annotation.value()); 28 | } 29 | 30 | @Override 31 | protected ConverterContext getConverterContext(DataProvider dataProvider) { 32 | return new ConverterContext(ReflectionSupport.newInstance(dataProvider.objectArrayConverter()), 33 | ReflectionSupport.newInstance(dataProvider.singleArgConverter()), 34 | ReflectionSupport.newInstance(dataProvider.stringConverter()), dataProvider.splitBy(), 35 | dataProvider.convertNulls(), dataProvider.trimValues(), dataProvider.ignoreEnumCase()); 36 | } 37 | 38 | @Override 39 | protected boolean cacheDataProviderResult(DataProvider dataProviderAnnotation) { 40 | return dataProviderAnnotation.cache(); 41 | } 42 | 43 | @Override 44 | protected DisplayNameContext getDisplayNameContext(DataProvider dataProvider) { 45 | return new DisplayNameContext(dataProvider.formatter(), dataProvider.format(), getDefaultPlaceholders()); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/converter/CustomConverterDataProvider.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.converter; 2 | 3 | 4 | import java.lang.annotation.Documented; 5 | import java.lang.annotation.ElementType; 6 | import java.lang.annotation.Retention; 7 | import java.lang.annotation.RetentionPolicy; 8 | import java.lang.annotation.Target; 9 | 10 | import com.tngtech.junit.dataprovider.DataProvider; 11 | import com.tngtech.junit.dataprovider.convert.ObjectArrayConverter; 12 | import com.tngtech.junit.dataprovider.convert.SingleArgConverter; 13 | import com.tngtech.junit.dataprovider.convert.StringConverter; 14 | import com.tngtech.junit.dataprovider.format.DataProviderPlaceholderFormatter; 15 | import com.tngtech.junit.dataprovider.format.DataProviderTestNameFormatter; 16 | 17 | /** 18 | * @see DataProvider 19 | */ 20 | @Documented 21 | @Retention(RetentionPolicy.RUNTIME) 22 | @Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD }) 23 | @interface CustomConverterDataProvider { 24 | 25 | /** 26 | * @see DataProvider#value() 27 | */ 28 | String[] value() default {}; 29 | 30 | /** 31 | * @see DataProvider#splitBy() 32 | */ 33 | String splitBy() default DataProvider.COMMA; 34 | 35 | /** 36 | * @see DataProvider#convertNulls() 37 | */ 38 | boolean convertNulls() default true; 39 | 40 | /** 41 | * @see DataProvider#trimValues() 42 | */ 43 | boolean trimValues() default true; 44 | 45 | /** 46 | * @see DataProvider#ignoreEnumCase() 47 | */ 48 | boolean ignoreEnumCase() default false; 49 | 50 | /** 51 | * @see DataProvider#format() 52 | */ 53 | String format() default DataProvider.DEFAULT_FORMAT; 54 | 55 | /** 56 | * @see DataProvider#formatter() 57 | */ 58 | Class formatter() default DataProviderPlaceholderFormatter.class; 59 | 60 | /** 61 | * @see DataProvider#objectArrayConverter() 62 | */ 63 | Class objectArrayConverter() default ObjectArrayConverter.class; 64 | 65 | /** 66 | * @see DataProvider#singleArgConverter() 67 | */ 68 | Class singleArgConverter() default SingleArgConverter.class; 69 | 70 | /** 71 | * @see DataProvider#stringConverter() 72 | */ 73 | Class stringConverter() default DateTimeAwareStringConverter.class; 74 | } 75 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/NonStaticDataProviderAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static com.tngtech.junit.dataprovider.DataProviders.$; 4 | import static com.tngtech.junit.dataprovider.DataProviders.$$; 5 | import static org.assertj.core.api.Assertions.assertThat; 6 | 7 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 8 | import org.junit.jupiter.api.Nested; 9 | import org.junit.jupiter.api.TestInstance; 10 | import org.junit.jupiter.api.TestInstance.Lifecycle; 11 | import org.junit.jupiter.api.TestTemplate; 12 | import org.junit.jupiter.api.extension.ExtendWith; 13 | 14 | import com.tngtech.junit.dataprovider.DataProvider; 15 | import com.tngtech.junit.dataprovider.UseDataProvider; 16 | import com.tngtech.junit.dataprovider.UseDataProviderExtension; 17 | 18 | @TestInstance(Lifecycle.PER_CLASS) 19 | @ExtendWith(UseDataProviderExtension.class) 20 | class NonStaticDataProviderAcceptanceTest { 21 | 22 | @DataProvider 23 | Object[][] dataProviderDivide() { 24 | // @formatter:off 25 | return $$( 26 | $( 0, 1, 0 ), 27 | $( 0, -1, 0 ), 28 | $( 1, 1, 1 ), 29 | $( 1, -1, -1 ), 30 | $( 2, 1, 2 ) 31 | ); 32 | // @formatter:on 33 | } 34 | 35 | @TestTemplate 36 | @UseDataProvider 37 | void testDivide(int a, int b, int expected) { 38 | // Given: 39 | 40 | // When: 41 | int result = a / b; 42 | 43 | // Then: 44 | assertThat(result).isEqualTo(expected); 45 | } 46 | 47 | @SuppressFBWarnings(value = "SIC_INNER_SHOULD_BE_STATIC", justification = "Must be non-static for JUnit Jupiter") 48 | @TestInstance(Lifecycle.PER_CLASS) 49 | @Nested 50 | class NestedTests { 51 | 52 | @DataProvider 53 | Object[][] dataProviderPow() { 54 | // @formatter:off 55 | return $$( 56 | $( 0, 0 ), 57 | $( 1, 1 ), 58 | $( 2, 4 ), 59 | $( 4, 16 ) 60 | ); 61 | // @formatter:on 62 | } 63 | 64 | @TestTemplate 65 | @UseDataProvider 66 | void testPow(int a, int expected) { 67 | // Given: 68 | 69 | // When: 70 | int result = a * a; 71 | 72 | // Then: 73 | assertThat(result).isEqualTo(expected); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/SingleArgumentDataProviderAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.util.Arrays; 6 | import java.util.Collections; 7 | import java.util.HashSet; 8 | import java.util.List; 9 | import java.util.Set; 10 | import java.util.function.UnaryOperator; 11 | 12 | import org.junit.jupiter.params.ParameterizedTest; 13 | 14 | import com.tngtech.junit.dataprovider.DataProvider; 15 | import com.tngtech.junit.dataprovider.UseDataProvider; 16 | 17 | class SingleArgumentDataProviderAcceptanceTest { 18 | 19 | @DataProvider 20 | static Object[] dataProviderSingleArgObjectArray() { 21 | // @formatter:off 22 | return new Object[] { 23 | 0, 24 | 1, 25 | 123, 26 | }; 27 | // @formatter:on 28 | } 29 | 30 | @ParameterizedTest 31 | @UseDataProvider("dataProviderSingleArgObjectArray") 32 | void testSingleArgObjectArray(long l) { 33 | // Expected: 34 | assertThat(l).isNotEqualTo(1234); 35 | } 36 | 37 | @DataProvider 38 | static List dataProviderSingleArgListOfObject() { 39 | return Arrays.asList(null, "", "1", "123"); 40 | } 41 | 42 | @ParameterizedTest 43 | @UseDataProvider("dataProviderSingleArgListOfObject") 44 | void testSingleArgListOfObject(String string) { 45 | // Expected: 46 | assertThat(string).isNotEqualTo("1234"); 47 | } 48 | 49 | @DataProvider 50 | static Iterable dataProviderSingleArgIterableOfString() { 51 | Set result = new HashSet<>(); 52 | result.add(null); 53 | result.add(""); 54 | result.add("1"); 55 | result.add("123"); 56 | return result; 57 | } 58 | 59 | @ParameterizedTest 60 | @UseDataProvider 61 | void testSingleArgIterableOfString(String string) { 62 | // Expected: 63 | assertThat(string).isNotEqualTo("1234"); 64 | } 65 | 66 | @DataProvider 67 | public static List> listOfUnaryOperator() { 68 | return Collections.singletonList((string) -> "merged" + string); 69 | } 70 | 71 | @ParameterizedTest 72 | @UseDataProvider("listOfUnaryOperator") 73 | public void testListOfUnaryOperator(UnaryOperator operator) { 74 | // Expected: 75 | assertThat(operator.apply("test")).isEqualTo("mergedtest"); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/meta/MetaAnnotationAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.meta; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 6 | import org.junit.jupiter.api.Nested; 7 | import org.junit.jupiter.params.ParameterizedTest; 8 | 9 | import com.tngtech.junit.dataprovider.DataProvider; 10 | import com.tngtech.junit.dataprovider.UseDataProvider; 11 | 12 | class MetaAnnotationAcceptanceTest { 13 | 14 | // @formatter:off 15 | @StringDataProvider({ 16 | " | 0", 17 | "a | 1", 18 | "abc | 3", 19 | "veryLongString | 14", 20 | }) 21 | // @formatter:off 22 | void testStringLength(String str, int expectedLength) { 23 | // Expect: 24 | assertThat(str).hasSize(expectedLength); 25 | } 26 | 27 | @FixedLocationDataProviderTest 28 | void testAdd(int a, int b, int expected) { 29 | // Expect: 30 | assertThat(a + b).isEqualTo(expected); 31 | } 32 | 33 | @SuppressFBWarnings(value = "SIC_INNER_SHOULD_BE_STATIC", justification = "Must be non-static for JUnit Jupiter") 34 | @Nested 35 | class NestedTests { 36 | @FixedLocationDataProviderTest 37 | void testAdd(int a, int b, int expected) { 38 | // Expect: 39 | assertThat(a + b).isEqualTo(expected); 40 | } 41 | } 42 | 43 | @DataProviderTest(value = "dataProviderMinus", location = DataProviderLocation.class) 44 | void testMinus(long a, long b, long expected) { 45 | // Given: 46 | 47 | // When: 48 | long result = a - b; 49 | 50 | // Then: 51 | assertThat(result).isEqualTo(expected); 52 | } 53 | 54 | // @formatter:off 55 | @ParameterizedTest 56 | @DataProvider({ 57 | "null", 58 | }) 59 | // @formatter:on 60 | void testIsNull(String string) { 61 | // Expect: 62 | assertThat(string).isNull(); 63 | } 64 | 65 | @DataProvider 66 | static String[] dataProviderNonNull() { 67 | // @formatter:off 68 | return new String[] { 69 | "NULL", 70 | "Null", 71 | }; 72 | // @formatter:on 73 | } 74 | 75 | @ParameterizedTest 76 | @UseDataProvider 77 | void testNonNull(String nonNull) { 78 | // Expect: 79 | assertThat(nonNull).isNotNull(); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/meta/MetaAnnotationAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.meta; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 6 | import org.junit.jupiter.api.Nested; 7 | import org.junit.jupiter.api.TestTemplate; 8 | 9 | import com.tngtech.junit.dataprovider.DataProvider; 10 | import com.tngtech.junit.dataprovider.UseDataProvider; 11 | 12 | @ExtendWithDataProvider 13 | class MetaAnnotationAcceptanceTest { 14 | 15 | // @formatter:off 16 | @StringDataProvider({ 17 | " | 0", 18 | "a | 1", 19 | "abc | 3", 20 | "veryLongString | 14", 21 | }) 22 | // @formatter:off 23 | void testStringLength(String str, int expectedLength) { 24 | // Expect: 25 | assertThat(str).hasSize(expectedLength); 26 | } 27 | 28 | @FixedLocationDataProviderTest 29 | void testAdd(int a, int b, int expected) { 30 | // Expect: 31 | assertThat(a + b).isEqualTo(expected); 32 | } 33 | 34 | @SuppressFBWarnings(value = "SIC_INNER_SHOULD_BE_STATIC", justification = "Must be non-static for JUnit Jupiter") 35 | @Nested 36 | class NestedTests { 37 | @FixedLocationDataProviderTest 38 | void testAdd(int a, int b, int expected) { 39 | // Expect: 40 | assertThat(a + b).isEqualTo(expected); 41 | } 42 | } 43 | 44 | @DataProviderTest(value = "dataProviderMinus", location = DataProviderLocation.class) 45 | void testMinus(long a, long b, long expected) { 46 | // Given: 47 | 48 | // When: 49 | long result = a - b; 50 | 51 | // Then: 52 | assertThat(result).isEqualTo(expected); 53 | } 54 | 55 | // @formatter:off 56 | @TestTemplate 57 | @DataProvider({ 58 | "null", 59 | }) 60 | // @formatter:on 61 | void testIsNull(String string) { 62 | // Expect: 63 | assertThat(string).isNull(); 64 | } 65 | 66 | @DataProvider 67 | static String[] dataProviderNonNull() { 68 | // @formatter:off 69 | return new String[] { 70 | "NULL", 71 | "Null", 72 | }; 73 | // @formatter:on 74 | } 75 | 76 | @TestTemplate 77 | @UseDataProvider 78 | void testNonNull(String nonNull) { 79 | // Expect: 80 | assertThat(nonNull).isNotNull(); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /junit4/src/main/java/com/tngtech/java/junit/dataprovider/format/DataProviderPlaceholderFormatter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 TNG Technology Consulting GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.tngtech.java.junit.dataprovider.format; 17 | 18 | import java.lang.reflect.Method; 19 | import java.util.List; 20 | 21 | import com.tngtech.junit.dataprovider.format.DataProviderTestNameFormatter; 22 | import com.tngtech.junit.dataprovider.placeholder.BasePlaceholder; 23 | import com.tngtech.junit.dataprovider.placeholder.ReplacementData; 24 | 25 | public class DataProviderPlaceholderFormatter implements DataProviderTestNameFormatter { 26 | 27 | private final String nameFormat; 28 | private final List placeholders; 29 | 30 | public DataProviderPlaceholderFormatter(String nameFormat, List placeholders) { 31 | this.nameFormat = nameFormat; 32 | this.placeholders = placeholders; 33 | } 34 | 35 | @Override 36 | public String format(Method testMethod, int invocationIndex, List arguments) { 37 | String result = nameFormat; 38 | for (BasePlaceholder placeholder : placeholders) { 39 | if (placeholder instanceof com.tngtech.java.junit.dataprovider.internal.placeholder.BasePlaceholder) { 40 | com.tngtech.java.junit.dataprovider.internal.placeholder.BasePlaceholder placeHolder = (com.tngtech.java.junit.dataprovider.internal.placeholder.BasePlaceholder) placeholder; 41 | synchronized (placeHolder) { 42 | placeHolder.setContext(testMethod, invocationIndex, arguments.toArray()); 43 | result = placeHolder.process(result); 44 | } 45 | 46 | } else { 47 | ReplacementData data = ReplacementData.of(testMethod, invocationIndex, arguments); 48 | result = placeholder.process(data, result); 49 | } 50 | } 51 | return result; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/resolver/CustomResolverAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.resolver; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.util.Arrays; 6 | import java.util.Collections; 7 | import java.util.LinkedHashSet; 8 | import java.util.Set; 9 | import java.util.concurrent.atomic.AtomicInteger; 10 | 11 | import org.junit.jupiter.api.AfterAll; 12 | import org.junit.jupiter.api.BeforeAll; 13 | import org.junit.jupiter.params.ParameterizedTest; 14 | 15 | import com.tngtech.junit.dataprovider.DataProvider; 16 | import com.tngtech.junit.dataprovider.UseDataProvider; 17 | 18 | class CustomResolverAcceptanceTest { 19 | 20 | private static AtomicInteger counterOne; 21 | private static AtomicInteger counterTwo; 22 | 23 | @BeforeAll 24 | static void setupClass() { 25 | counterOne = new AtomicInteger(0); 26 | counterTwo = new AtomicInteger(0); 27 | } 28 | 29 | @AfterAll 30 | static void tearDownClass() { 31 | assertThat(counterOne.get()).isEqualTo(6); 32 | assertThat(counterTwo.get()).isEqualTo(6); 33 | } 34 | 35 | @DataProvider 36 | static Object[][] testNumberA() { 37 | // @formatter:off 38 | return new Object[][] { 39 | { (byte) 1 }, 40 | }; 41 | // @formatter:on 42 | } 43 | 44 | @DataProvider 45 | static Iterable> testNumberB() { 46 | // @formatter:off 47 | return Arrays.asList( 48 | Collections.singleton(2), 49 | Collections.singleton(Integer.valueOf(3)) 50 | ); 51 | // @formatter:on 52 | } 53 | 54 | @DataProvider 55 | static Set testNumberC() { 56 | Set result = new LinkedHashSet<>(); 57 | result.add(4); 58 | result.add(5L); 59 | result.add(6.0); 60 | return result; 61 | } 62 | 63 | @ParameterizedTest 64 | @UseDataProvider(resolver = DataProviderStartWithTestMethodNameResolver.class) 65 | void testNumber(Number number) { 66 | // When: 67 | int count = counterOne.incrementAndGet(); 68 | 69 | // Then: 70 | assertThat(count).isEqualTo(number.intValue()); 71 | } 72 | 73 | @ParameterizedTest 74 | @CustomResolverUseDataProvider 75 | void testNumber(double d) { 76 | // When: 77 | double count = counterTwo.incrementAndGet(); 78 | 79 | // Then: 80 | assertThat(count).isEqualTo(d); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/VarargsAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static com.tngtech.junit.dataprovider.DataProviders.$; 4 | import static com.tngtech.junit.dataprovider.DataProviders.$$; 5 | import static org.assertj.core.api.Assertions.assertThat; 6 | import static org.junit.jupiter.api.Assertions.assertNotNull; 7 | 8 | import org.junit.jupiter.params.ParameterizedTest; 9 | 10 | import com.tngtech.junit.dataprovider.DataProvider; 11 | import com.tngtech.junit.dataprovider.UseDataProvider; 12 | 13 | class VarargsAcceptanceTest { 14 | 15 | @DataProvider 16 | static Object[][] dataProviderLongVarargs() { 17 | // @formatter:off 18 | return new Object[][] { 19 | {}, 20 | { new Long[0] }, 21 | { 1L }, 22 | { 111L, 222L, 333L }, 23 | }; 24 | // @formatter:on 25 | } 26 | 27 | @ParameterizedTest 28 | @UseDataProvider("dataProviderLongVarargs") 29 | void testLongVarargs(Long... longs) { 30 | for (Long l : longs) { 31 | assertNotNull(l); 32 | } 33 | } 34 | 35 | // @formatter:off 36 | @ParameterizedTest 37 | @DataProvider({ 38 | "", 39 | "a", 40 | "x, y, z", 41 | }) 42 | // @formatter:on 43 | void testStringVarargs(String... strings) { 44 | for (String s : strings) { 45 | assertNotNull(s); 46 | } 47 | } 48 | 49 | @DataProvider 50 | static Object[][] dataProviderIntVarargsWithLeadingCharArgument() { 51 | // @formatter:off 52 | return $$( 53 | $('a'), 54 | $('b', 0), 55 | $('c', 1, 2, 3) 56 | ); 57 | // @formatter:on 58 | } 59 | 60 | @ParameterizedTest 61 | @UseDataProvider("dataProviderIntVarargsWithLeadingCharArgument") 62 | void testIntVarargsWithLeadingCharArgument(char c, int... is) { 63 | assertThat(c).isNotNull(); 64 | for (int i : is) { 65 | assertThat(i).isNotNegative(); 66 | } 67 | } 68 | 69 | // @formatter:off 70 | @ParameterizedTest 71 | @DataProvider({ 72 | "0", 73 | "1, a", 74 | "3, aa, bb, cc", 75 | }) 76 | // @formatter:on 77 | void testStringVarargsWithLeadingSizeArgument(int i, String... strings) { 78 | assertThat(strings).hasSize(i); 79 | for (String s : strings) { 80 | assertNotNull(s); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/resolver/CustomResolverDataProviderTestExtension.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider.custom.resolver; 2 | 3 | import static com.tngtech.junit.dataprovider.resolver.DataProviderResolverContext.generateLocations; 4 | import static java.util.Arrays.asList; 5 | 6 | import org.junit.jupiter.api.extension.ExtensionContext; 7 | import org.junit.platform.commons.support.ReflectionSupport; 8 | 9 | import com.tngtech.junit.dataprovider.DataProvider; 10 | import com.tngtech.junit.dataprovider.DisplayNameContext; 11 | import com.tngtech.junit.dataprovider.UseDataProviderInvocationContextProvider; 12 | import com.tngtech.junit.dataprovider.convert.ConverterContext; 13 | import com.tngtech.junit.dataprovider.resolver.DataProviderResolverContext; 14 | 15 | class CustomResolverDataProviderTestExtension 16 | extends UseDataProviderInvocationContextProvider { 17 | 18 | CustomResolverDataProviderTestExtension() { 19 | super(CustomResolverDataProviderTest.class, DataProvider.class); 20 | } 21 | 22 | @Override 23 | protected DataProviderResolverContext getDataProviderResolverContext(ExtensionContext extensionContext, 24 | CustomResolverDataProviderTest annotation) { 25 | return new DataProviderResolverContext(extensionContext.getRequiredTestMethod(), asList(annotation.resolver()), 26 | annotation.resolveStrategy(), 27 | generateLocations(extensionContext.getRequiredTestClass(), annotation.location()), 28 | DataProvider.class, annotation.value()); 29 | } 30 | 31 | @Override 32 | protected ConverterContext getConverterContext(DataProvider dataProvider) { 33 | return new ConverterContext(ReflectionSupport.newInstance(dataProvider.objectArrayConverter()), 34 | ReflectionSupport.newInstance(dataProvider.singleArgConverter()), 35 | ReflectionSupport.newInstance(dataProvider.stringConverter()), dataProvider.splitBy(), 36 | dataProvider.convertNulls(), dataProvider.trimValues(), dataProvider.ignoreEnumCase()); 37 | } 38 | 39 | @Override 40 | protected boolean cacheDataProviderResult(DataProvider dataProviderAnnotation) { 41 | return dataProviderAnnotation.cache(); 42 | } 43 | 44 | @Override 45 | protected DisplayNameContext getDisplayNameContext(DataProvider dataProvider) { 46 | return new DisplayNameContext(dataProvider.formatter(), dataProvider.format(), getDefaultPlaceholders()); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /junit-jupiter/src/test/java/com/tngtech/junit/dataprovider/AbstractDataProviderInvocationContextProviderTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.junit.dataprovider; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | import static org.junit.jupiter.api.Assertions.assertThrows; 5 | import static org.mockito.Mockito.when; 6 | 7 | import java.lang.annotation.Annotation; 8 | import java.lang.reflect.Method; 9 | import java.util.stream.Stream; 10 | 11 | import org.junit.jupiter.api.BeforeEach; 12 | import org.junit.jupiter.api.Test; 13 | import org.junit.jupiter.api.extension.ExtensionConfigurationException; 14 | import org.junit.jupiter.api.extension.ExtensionContext; 15 | import org.junit.jupiter.api.extension.TestTemplateInvocationContext; 16 | import org.mockito.Mock; 17 | import org.mockito.MockitoAnnotations; 18 | 19 | import com.tngtech.junit.dataprovider.convert.DataConverter; 20 | 21 | class AbstractDataProviderInvocationContextProviderTest { 22 | 23 | private AbstractDataProviderInvocationContextProvider underTest; 24 | 25 | private final Annotation testAnnotation = () -> Annotation.class; 26 | @SuppressWarnings("unchecked") 27 | private final Class testAnnotationClass = (Class) testAnnotation.getClass(); 28 | 29 | @Mock 30 | private DataConverter dataConverter; 31 | @Mock 32 | private ExtensionContext extensionContext; 33 | 34 | private Method testMethod; 35 | 36 | @BeforeEach 37 | void setup() throws Exception { 38 | underTest = new AbstractDataProviderInvocationContextProvider(testAnnotationClass, dataConverter) { 39 | @Override 40 | protected Stream provideInvocationContexts(ExtensionContext extensionContext, 41 | Annotation testAnnotation) { 42 | return null; 43 | } 44 | }; 45 | testMethod = this.getClass().getDeclaredMethod("setup"); 46 | 47 | MockitoAnnotations.initMocks(this); 48 | } 49 | 50 | @Test 51 | void testProvideTestTemplateInvocationContextsShouldThrowProperExceptionIfAnnotationIsNotPresent() { 52 | // Given: 53 | when(extensionContext.getRequiredTestMethod()).thenReturn(testMethod); 54 | 55 | // When: 56 | Exception result = assertThrows(ExtensionConfigurationException.class, 57 | () -> underTest.provideTestTemplateInvocationContexts(extensionContext)); 58 | 59 | // Then: 60 | assertThat(result).hasMessageMatching("Could not find annotation '.*' on test method '.*'\\."); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/ListDataProviderAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.util.ArrayList; 6 | import java.util.Collections; 7 | import java.util.List; 8 | import java.util.function.UnaryOperator; 9 | 10 | import org.junit.jupiter.api.TestTemplate; 11 | import org.junit.jupiter.api.extension.ExtendWith; 12 | 13 | import com.tngtech.junit.dataprovider.DataProvider; 14 | import com.tngtech.junit.dataprovider.UseDataProvider; 15 | import com.tngtech.junit.dataprovider.UseDataProviderExtension; 16 | 17 | @ExtendWith(UseDataProviderExtension.class) 18 | class ListDataProviderAcceptanceTest { 19 | 20 | @DataProvider 21 | static List> dataProviderNumberFormat() { 22 | List> result = new ArrayList<>(); 23 | List first = new ArrayList<>(); 24 | first.add(Integer.valueOf(101)); 25 | first.add("%5d"); 26 | first.add(" 101"); 27 | result.add(first); 28 | List second = new ArrayList<>(); 29 | second.add(125); 30 | second.add("%06d"); 31 | second.add("000125"); 32 | result.add(second); 33 | return result; 34 | } 35 | 36 | @TestTemplate 37 | @UseDataProvider("dataProviderNumberFormat") 38 | void testNumberFormat(Number number, String format, String expected) { 39 | // Given: 40 | 41 | // When: 42 | String result = String.format(format, number); 43 | 44 | // Then: 45 | assertThat(result).isEqualTo(expected); 46 | } 47 | 48 | @DataProvider 49 | static List dataProviderIsNumber() { 50 | List result = new ArrayList<>(); 51 | result.add(101); 52 | result.add(125L); 53 | result.add(125.0); 54 | return result; 55 | } 56 | 57 | @TestTemplate 58 | @UseDataProvider 59 | void testIsNumber(Number number) { 60 | // Expect: 61 | assertThat(number).isInstanceOf(Number.class); 62 | } 63 | 64 | @DataProvider 65 | public static List>> listOfListOfUnaryOperator() { 66 | return Collections.singletonList(Collections.singletonList((string) -> "merged-" + string)); 67 | } 68 | 69 | @TestTemplate 70 | @UseDataProvider("listOfListOfUnaryOperator") 71 | public void testListOfListOfUnaryOperator(UnaryOperator operator) { 72 | // Expect: 73 | assertThat(operator.apply("test")).isEqualTo("merged-test"); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/CacheDataProviderResultsAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.util.concurrent.atomic.AtomicInteger; 6 | 7 | import org.junit.jupiter.params.ParameterizedTest; 8 | 9 | import com.tngtech.junit.dataprovider.DataProvider; 10 | import com.tngtech.junit.dataprovider.UseDataProvider; 11 | 12 | class CacheDataProviderResultsAcceptanceTest { 13 | 14 | // works if test discovery order is equal to execution order 15 | static final AtomicInteger noOfTestsCallsUsingNotCachedDataProvider = new AtomicInteger(0); 16 | 17 | private static final AtomicInteger noOfCachedDataProviderCalls = new AtomicInteger(0); 18 | private static final AtomicInteger noOfNotCachedDataProviderCalls = new AtomicInteger(0); 19 | 20 | @DataProvider 21 | static Object[][] dataProviderCachedDataProviderResults() { 22 | // @formatter:off 23 | return new Object[][] { 24 | { noOfCachedDataProviderCalls.incrementAndGet() }, 25 | }; 26 | // @formatter:on 27 | } 28 | 29 | @ParameterizedTest 30 | @UseDataProvider("dataProviderCachedDataProviderResults") 31 | void testCachedDataProviderResultsOne(int noOfDataProviderCalls) { 32 | // Expected: 33 | assertThat(noOfDataProviderCalls).isEqualTo(1); 34 | } 35 | 36 | @ParameterizedTest 37 | @UseDataProvider("dataProviderCachedDataProviderResults") 38 | void testCachedDataProviderResultsTwo(int noOfDataProviderCalls) { 39 | // Expected: 40 | assertThat(noOfDataProviderCalls).isEqualTo(1); 41 | } 42 | 43 | @DataProvider(cache = false) 44 | public static Object[][] dataProviderDoNotCacheDataProviderResults() { 45 | // @formatter:off 46 | return new Object[][] { 47 | { noOfNotCachedDataProviderCalls.incrementAndGet() }, 48 | }; 49 | // @formatter:on 50 | } 51 | 52 | @ParameterizedTest 53 | @UseDataProvider("dataProviderDoNotCacheDataProviderResults") 54 | void testDoNotCacheDataProviderResultsOne(int noOfDataProvderCalls) { 55 | // Expected: 56 | assertThat(noOfDataProvderCalls).isEqualTo(noOfTestsCallsUsingNotCachedDataProvider.incrementAndGet()); 57 | } 58 | 59 | @ParameterizedTest 60 | @UseDataProvider("dataProviderDoNotCacheDataProviderResults") 61 | void testDoNotCacheCachedDataProviderResultsTwo(int noOfDataProvderCalls) { 62 | // Expected: 63 | assertThat(noOfDataProvderCalls).isEqualTo(noOfTestsCallsUsingNotCachedDataProvider.incrementAndGet()); 64 | } 65 | } -------------------------------------------------------------------------------- /junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/SingleArgumentDataProviderAcceptanceTest.java: -------------------------------------------------------------------------------- 1 | package com.tngtech.test.junit.dataprovider; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.util.ArrayList; 6 | import java.util.Arrays; 7 | import java.util.Collections; 8 | import java.util.HashSet; 9 | import java.util.List; 10 | import java.util.Set; 11 | import java.util.function.UnaryOperator; 12 | 13 | import org.junit.jupiter.api.TestTemplate; 14 | import org.junit.jupiter.api.extension.ExtendWith; 15 | 16 | import com.tngtech.junit.dataprovider.DataProvider; 17 | import com.tngtech.junit.dataprovider.UseDataProvider; 18 | import com.tngtech.junit.dataprovider.UseDataProviderExtension; 19 | 20 | @ExtendWith(UseDataProviderExtension.class) 21 | class SingleArgumentDataProviderAcceptanceTest { 22 | 23 | @DataProvider 24 | static Object[] dataProviderSingleArgObjectArray() { 25 | // @formatter:off 26 | return new Object[] { 27 | 0, 28 | 1, 29 | 123, 30 | }; 31 | // @formatter:on 32 | } 33 | 34 | @TestTemplate 35 | @UseDataProvider("dataProviderSingleArgObjectArray") 36 | void testSingleArgObjectArray(long l) { 37 | // Expected: 38 | assertThat(l).isNotEqualTo(1234); 39 | } 40 | 41 | @DataProvider 42 | static List dataProviderSingleArgListOfObject() { 43 | return Arrays.asList(null, "", "1", "123"); 44 | } 45 | 46 | @TestTemplate 47 | @UseDataProvider("dataProviderSingleArgListOfObject") 48 | void testSingleArgListOfObject(String string) { 49 | // Expected: 50 | assertThat(string).isNotEqualTo("1234"); 51 | } 52 | 53 | @DataProvider 54 | static Iterable dataProviderSingleArgIterableOfString() { 55 | Set result = new HashSet<>(); 56 | result.add(null); 57 | result.add(""); 58 | result.add("1"); 59 | result.add("123"); 60 | return result; 61 | } 62 | 63 | @TestTemplate 64 | @UseDataProvider 65 | void testSingleArgIterableOfString(String string) { 66 | // Expected: 67 | assertThat(string).isNotEqualTo("1234"); 68 | } 69 | 70 | @DataProvider 71 | public static List> listOfUnaryOperator() { 72 | return Collections.singletonList((string) -> "merged" + string); 73 | } 74 | 75 | @TestTemplate 76 | @UseDataProvider("listOfUnaryOperator") 77 | public void testListOfUnaryOperator(UnaryOperator operator) { 78 | // Expected: 79 | assertThat(operator.apply("test")).isEqualTo("mergedtest"); 80 | } 81 | } 82 | --------------------------------------------------------------------------------