Skip to content

Commit

Permalink
Merge branch 'issue106-double-execution'
Browse files Browse the repository at this point in the history
* issue106-double-execution:
  filter synthetic methods for dataprovider tests (#106)
  add test cases for implementing test cases for junit-jupiter* (#106)
  • Loading branch information
aaschmid committed Apr 2, 2018
2 parents 0f6c1da + 93b5da4 commit 3056635
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.tngtech.junit.dataprovider.DataProvider;
import com.tngtech.junit.dataprovider.UseDataProvider;

abstract class ToBeOverriddenAcceptanceTest {
abstract class AbstractAcceptanceBaseTest {

@DataProvider
static Object[][] dataProviderBase() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.tngtech.test.junit.dataprovider.override;

public interface AcceptanceTestInterface<T> {

void testToBeImplemented();

void testToBeImplemented(T value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.tngtech.test.junit.dataprovider.override;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.concurrent.atomic.AtomicInteger;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;

import com.tngtech.junit.dataprovider.DataProvider;
import com.tngtech.junit.dataprovider.UseDataProvider;

class ImplementingAcceptanceTest implements AcceptanceTestInterface<Integer> {

private static final AtomicInteger noOfNormalTestsCalls = new AtomicInteger(0);
private static final AtomicInteger noOfDataProviderTestsCalls = new AtomicInteger(0);

@DataProvider
public static Object[][] dataProviderToBeImplemented() {
// @formatter:off
return new Object[][] {
{ 1 },
{ 2 },
};
// @formatter:on
}

@Test
@Override
public void testToBeImplemented() {
assertThat(1).isEqualTo(noOfNormalTestsCalls.incrementAndGet());
}

@ParameterizedTest
@UseDataProvider
@Override
public void testToBeImplemented(Integer value) {
assertThat(value).isEqualTo(noOfDataProviderTestsCalls.incrementAndGet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.tngtech.junit.dataprovider.DataProvider;
import com.tngtech.junit.dataprovider.UseDataProvider;

class OverridingAcceptanceTest extends ToBeOverriddenAcceptanceTest {
class OverridingAcceptanceTest extends AbstractAcceptanceBaseTest {

@Override
@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.tngtech.junit.dataprovider.DataProvider;
import com.tngtech.junit.dataprovider.UseDataProvider;

abstract class ToBeOverriddenAcceptanceTest {
abstract class AbstractAcceptanceBaseTest {

@DataProvider
static Object[][] dataProviderBase() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.tngtech.test.junit.dataprovider.override;

public interface AcceptanceTestInterface<T> {

void testToBeImplemented();

void testToBeImplemented(T value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.tngtech.test.junit.dataprovider.override;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.concurrent.atomic.AtomicInteger;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;

import com.tngtech.junit.dataprovider.DataProvider;
import com.tngtech.junit.dataprovider.UseDataProvider;
import com.tngtech.junit.dataprovider.UseDataProviderExtension;

@ExtendWith(UseDataProviderExtension.class)
class ImplementingAcceptanceTest implements AcceptanceTestInterface<Integer> {

private static final AtomicInteger noOfNormalTestsCalls = new AtomicInteger(0);
private static final AtomicInteger noOfDataProviderTestsCalls = new AtomicInteger(0);

@DataProvider
public static Object[][] dataProviderToBeImplemented() {
// @formatter:off
return new Object[][] {
{ 1 },
{ 2 },
};
// @formatter:on
}

@Test
@Override
public void testToBeImplemented() {
assertThat(1).isEqualTo(noOfNormalTestsCalls.incrementAndGet());
}

@TestTemplate
@UseDataProvider
@Override
public void testToBeImplemented(Integer value) {
assertThat(value).isEqualTo(noOfDataProviderTestsCalls.incrementAndGet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.tngtech.junit.dataprovider.UseDataProviderExtension;

@ExtendWith(UseDataProviderExtension.class)
class OverridingAcceptanceTest extends ToBeOverriddenAcceptanceTest {
class OverridingAcceptanceTest extends AbstractAcceptanceBaseTest {

@Override
@TestTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.UseDataProvider;

public abstract class DataProviderOverrideAcceptanceBaseTest {
public abstract class DataProviderAbstractAcceptanceBaseTest {
@DataProvider
public static Object[][] dataProviderBase() {
return new Object[][] { { "1" } };
Expand All @@ -21,9 +21,9 @@ public void testBase(@SuppressWarnings("unused") String one) {
}

// Does not work since v1.12.0 any more as new dataprovider resolver mechanism uses "Method#getDeclaringClass()"
// instead of "testClass" (= JUnit4 Frameworks "TestClass"); Workaround by specifying "location" explicitely ...
// instead of "testClass" (= JUnit4 Frameworks "TestClass"); Workaround by specifying "location" explicitly ...
@Test
@UseDataProvider(location = DataProviderOverrideAcceptanceTest.class)
@UseDataProvider(location = DataProviderOverridingAcceptanceTest.class)
public void testChild(String one) {
assertThat(one).isEqualTo("1");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.tngtech.test.java.junit.dataprovider.override;

public interface DataProviderAcceptanceTestInterface<T> {

void testToBeImplemented();

void testToBeImplemented(T value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.tngtech.test.java.junit.dataprovider.override;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Test;
import org.junit.runner.RunWith;

import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;

@RunWith(DataProviderRunner.class)
public class DataProviderImplementingAcceptanceTest implements DataProviderAcceptanceTestInterface<Integer> {

private static final AtomicInteger noOfNormalTestsCalls = new AtomicInteger(0);
private static final AtomicInteger noOfDataProviderTestsCalls = new AtomicInteger(0);

@DataProvider
public static Object[][] dataProviderToBeImplemented() {
// @formatter:off
return new Object[][] {
{ 1 },
{ 2 },
};
// @formatter:on
}

@Test
@Override
public void testToBeImplemented() {
assertThat(1).isEqualTo(noOfNormalTestsCalls.incrementAndGet());
}

@Test
@UseDataProvider
@Override
public void testToBeImplemented(Integer value) {
assertThat(value).isEqualTo(noOfDataProviderTestsCalls.incrementAndGet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.tngtech.java.junit.dataprovider.UseDataProvider;

@RunWith(DataProviderRunner.class)
public class DataProviderOverrideAcceptanceTest extends DataProviderOverrideAcceptanceBaseTest {
public class DataProviderOverridingAcceptanceTest extends DataProviderAbstractAcceptanceBaseTest {

@Override
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public List<FrameworkMethod> generateExplodedTestMethodsFor(FrameworkMethod test
if (testMethod == null) {
return Collections.emptyList();
}
// do not create dataprovider tests for synthetic methods (should normally be handled by JUnit 4 but isn't)
if (testMethod.getMethod().isSynthetic()) {
return Collections.emptyList();
}
if (dataProviderMethod != null) {
try {
return explodeTestMethod(testMethod, dataProviderMethod);
Expand Down

0 comments on commit 3056635

Please sign in to comment.