Skip to content

Commit

Permalink
enabled compatibility to junit-4.8.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Schmid committed Mar 7, 2013
1 parent 14e05e3 commit ebec987
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A TestNG like dataprovider runner for JUnit.
Reqirements
-----------

This JUnit dataprovider requires JUnit in version 4.11+. If you are using a former version, please let us know opening an issue.
This JUnit dataprovider requires JUnit in version 4.8.2+. If you are using a former version, please let us know opening an issue.

Download
--------
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repositories {
}

dependencies {
compile group: 'junit', name: 'junit-dep', version: '4.11' /* provided scope */
compile group: 'junit', name: 'junit-dep', version: '4.8.2' /* provided scope */
testCompile group: 'org.easytesting', name: 'fest-assert-core', version: '2.+'
testCompile group: 'org.mockito', name: 'mockito-core', version: '1.9.+'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tngtech.java.junit.dataprovider;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -162,10 +163,10 @@ FrameworkMethod getDataProviderMethod(FrameworkMethod testMethod) {
boolean isValidDataProviderMethod(FrameworkMethod dataProviderMethod) {
// @formatter:off
return dataProviderMethod != null
&& dataProviderMethod.isPublic()
&& dataProviderMethod.isStatic()
&& Modifier.isPublic(dataProviderMethod.getMethod().getModifiers())
&& Modifier.isStatic(dataProviderMethod.getMethod().getModifiers())
&& dataProviderMethod.getMethod().getParameterTypes().length == 0
&& dataProviderMethod.getReturnType().equals(Object[][].class);
&& dataProviderMethod.getMethod().getReturnType().equals(Object[][].class);
// @formatter:on
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,18 +335,13 @@ public void testIsValidDataProviderMethodShouldReturnFalseIfDataProviderMethodIs
// Then:
assertThat(result).isFalse();
}

@Test
public void testIsValidDataProviderMethodShouldReturnFalseIfItIsNotPublic() {

// Given:
FrameworkMethod dataProviderMethod = mock(FrameworkMethod.class);

doReturn(false).when(dataProviderMethod).isPublic();
doReturn(true).when(dataProviderMethod).isStatic();
// doReturn(new Class<?>[0]).when(dataProviderMethod).getParameterTypes(); // not visible and mockable :(
doReturn(anyNoArgMethod()).when(dataProviderMethod).getMethod(); // instead of stubbing getParameterTypes()
doReturn(Object[][].class).when(dataProviderMethod).getReturnType();
doReturn(getMethod("nonPublicDataProviderMethod")).when(dataProviderMethod).getMethod();

// When:
boolean result = underTest.isValidDataProviderMethod(dataProviderMethod);
Expand All @@ -361,11 +356,7 @@ public void testIsValidDataProviderMethodShouldReturnFalseIfItIsNotStatic() {
// Given:
FrameworkMethod dataProviderMethod = mock(FrameworkMethod.class);

doReturn(true).when(dataProviderMethod).isPublic();
doReturn(false).when(dataProviderMethod).isStatic();
// doReturn(new Class<?>[0]).when(dataProviderMethod).getParameterTypes(); // not visible and mockable :(
doReturn(anyNoArgMethod()).when(dataProviderMethod).getMethod(); // instead of stubbing getParameterTypes()
doReturn(Object[][].class).when(dataProviderMethod).getReturnType();
doReturn(getMethod("nonStaticDataProviderMethod")).when(dataProviderMethod).getMethod();

// When:
boolean result = underTest.isValidDataProviderMethod(dataProviderMethod);
Expand All @@ -377,16 +368,10 @@ public void testIsValidDataProviderMethodShouldReturnFalseIfItIsNotStatic() {
@Test
public void testIsValidDataProviderMethodShouldReturnFalseIfItRequiresAnyParameter() {

final Object arg = new Object();

// Given:
FrameworkMethod dataProviderMethod = mock(FrameworkMethod.class);

doReturn(true).when(dataProviderMethod).isPublic();
doReturn(true).when(dataProviderMethod).isStatic();
// doReturn(new Class<?>[] { String.class }).when(dataProviderMethod).getParameterTypes();
doReturn(anyOneArgMethod(arg)).when(dataProviderMethod).getMethod(); // instead of stubbing getParameterTypes()
doReturn(Object[][].class).when(dataProviderMethod).getReturnType();
doReturn(getMethod("nonNoArgDataProviderMethod", Object.class)).when(dataProviderMethod).getMethod();

// When:
boolean result = underTest.isValidDataProviderMethod(dataProviderMethod);
Expand All @@ -401,11 +386,7 @@ public void testIsValidDataProviderMethodShouldReturnFalseIfItDoesNotReturnObjec
// Given:
FrameworkMethod dataProviderMethod = mock(FrameworkMethod.class);

doReturn(true).when(dataProviderMethod).isPublic();
doReturn(true).when(dataProviderMethod).isStatic();
// doReturn(new Class<?>[0]).when(dataProviderMethod).getParameterTypes(); // not visible and mockedable :(
doReturn(anyNoArgMethod()).when(dataProviderMethod).getMethod(); // instead of stubbing getParameterTypes()
doReturn(void.class).when(dataProviderMethod).getReturnType();
doReturn(getMethod("nonObjectArrayArrayReturningDataProviderMethod")).when(dataProviderMethod).getMethod();

// When:
boolean result = underTest.isValidDataProviderMethod(dataProviderMethod);
Expand All @@ -420,11 +401,7 @@ public void testIsValidDataProviderMethodShouldReturnTrueIfItIsPublicStaticNoArg
// Given:
FrameworkMethod dataProviderMethod = mock(FrameworkMethod.class);

doReturn(true).when(dataProviderMethod).isPublic();
doReturn(true).when(dataProviderMethod).isStatic();
// doReturn(0).when(dataProviderMethod).getParameterTypes();
doReturn(anyNoArgMethod()).when(dataProviderMethod).getMethod(); // instead of stubbing getParameterTypes()
doReturn(Object[][].class).when(dataProviderMethod).getReturnType();
doReturn(getMethod("validDataProviderMethod")).when(dataProviderMethod).getMethod();

// When:
boolean result = underTest.isValidDataProviderMethod(dataProviderMethod);
Expand Down Expand Up @@ -537,29 +514,35 @@ public void testExplodeTestMethodsShouldReturnMultipleDataProviderFrameworkMetho
assertThat(actual2.parameters).isEqualTo(dataProviderMethodResult[2]);
}

private Method anyOneArgMethod(Object arg) {
private Method getMethod(String methodName, Class<?>... args) {
final Class<? extends DataProviderRunnerTest> clazz = this.getClass();
final String methodName = "anyOneArgMethod";
final Class<? extends Object> argClass = arg.getClass();

try {
return clazz.getDeclaredMethod(methodName, argClass);
return clazz.getDeclaredMethod(methodName, args);
} catch (Exception e) {
fail(String.format("No method with name '%s' and arg of class '%s' found in %s", methodName, argClass,
clazz));
fail(String.format("No method with name '%s' found in %s", methodName, clazz));
return null; // fool compiler
}
}

private Method anyNoArgMethod() {
final Class<? extends DataProviderRunnerTest> clazz = this.getClass();
final String methodName = "anyNoArgMethod";
// Methods used to test isValidDataProviderMethod
static Object[][] nonPublicDataProviderMethod() {
return null;
}

try {
return clazz.getDeclaredMethod(methodName);
} catch (Exception e) {
fail(String.format("No method with name '%s' found in %s", methodName, clazz));
return null; // fool compiler
}
public Object[][] nonStaticDataProviderMethod() {
return null;
}

public static Object[][] nonNoArgDataProviderMethod(@SuppressWarnings("unused") Object obj) {
return null;
}

public static String nonObjectArrayArrayReturningDataProviderMethod() {
return null;
}

public static Object[][] validDataProviderMethod() {
return null;
}

}

0 comments on commit ebec987

Please sign in to comment.