Skip to content

Commit

Permalink
Merge pull request #28 from barancev/master
Browse files Browse the repository at this point in the history
Implementing ability to pass information about test method to a data provider method
  • Loading branch information
aaschmid committed Jul 7, 2014
2 parents 0f40ab2 + ddf8ac9 commit eea5327
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ public List<FrameworkMethod> generateExplodedTestMethodsFor(FrameworkMethod test
List<FrameworkMethod> explodeTestMethod(FrameworkMethod testMethod, FrameworkMethod dataProviderMethod) {
Object data;
try {
data = dataProviderMethod.invokeExplosively(null);
Class<?>[] parameterTypes = dataProviderMethod.getMethod().getParameterTypes();
if (parameterTypes.length > 0) {
data = dataProviderMethod.invokeExplosively(null, testMethod);
} else {
data = dataProviderMethod.invokeExplosively(null);
}
} catch (Throwable t) {
throw new Error(String.format("Exception while invoking data provider method '%s': %s",
dataProviderMethod.getName(), t.getMessage()), t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ public void validateDataProviderMethod(FrameworkMethod dataProviderMethod, DataP
errors.add(new Exception(messageBasePart + " be static"));
}
if (method.getParameterTypes().length != 0) {
errors.add(new Exception(messageBasePart + " have no parameters"));
if (! method.getParameterTypes()[0].equals(FrameworkMethod.class)) {
errors.add(new Exception(messageBasePart + " have no parameters or a single FrameworkMethod parameter"));
}
}
if (!dataConverter.canConvert(method.getGenericReturnType())) {
errors.add(new Exception(messageBasePart + " either return Object[][] or List<List<Object>>"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,25 @@ public void testValidateDataProviderMethodShouldAddNoErrorIfDataProviderMethodIs
assertThat(errors).isEmpty();
}

@Test
public void testValidateDataProviderMethodShouldAddNoErrorIfDataProviderMethodWithTestMethodParameterIsValid() {
// Given:
String dataProviderName = "validDataProvider";

List<Throwable> errors = new ArrayList<Throwable>();

doReturn(dataProviderName).when(dataProviderMethod).getName();
doReturn(getMethod("validDataProviderMethodWithTestMethodParameter")).when(dataProviderMethod).getMethod();
doReturn(true).when(dataConverter).canConvert(any(Type.class));
doReturn(new String[0]).when(dataProvider).value();

// When:
underTest.validateDataProviderMethod(dataProviderMethod, dataProvider, errors);

// Then:
assertThat(errors).isEmpty();
}

@Test
public void testValidateDataProviderMethodShouldAddErrorIfDataProviderMethodIsNotPublic() {
// Given:
Expand Down Expand Up @@ -398,4 +417,8 @@ String nonPublicNonStaticNonNoArgDataProviderMethod(String arg1) {
public static Object[][] validDataProviderMethod() {
return null;
}

public static Object[][] validDataProviderMethodWithTestMethodParameter(FrameworkMethod testMethod) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import org.junit.runners.model.FrameworkMethod;

@RunWith(DataProviderRunner.class)
public class DataProviderJavaAcceptanceTest {
Expand Down Expand Up @@ -230,4 +231,19 @@ public void testIsEmptyString2(String str) {
// Then:
assertThat(isEmpty).isTrue();
}

@Test
@UseDataProvider("loadFromExternalFile")
@ExternalFile(format = ExternalFile.Format.CSV, value = "testdata.csv")
public void testThatUsesUniversalDataProvider(String testData) {
assertThat(testData).isEqualTo("testdata.csv");
}

@DataProvider
public static Object[][] loadFromExternalFile(FrameworkMethod testMethod) {
String testDataFile = testMethod.getAnnotation(ExternalFile.class).value();
return new Object[][]{
{ testDataFile }
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.tngtech.test.java.junit.dataprovider;

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ExternalFile {

public enum Format {
CSV, XML, XLS
}

Format format();
String value();
}

0 comments on commit eea5327

Please sign in to comment.