Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing ability to pass information about test method to a data provider method #28

Merged
merged 3 commits into from
Jul 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}