Skip to content

Commit

Permalink
version 1.5.0 with data provider from another class
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Schmid committed May 3, 2013
1 parent 6144f9d commit cbc6276
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ apply plugin: 'maven'
apply plugin: 'signing'

group = 'com.tngtech.java'
version = '1.4.2-SNAPSHOT'
version = '1.5.0'

task javadocJar(type: Jar, dependsOn: javadoc) {
from 'build/docs/javadoc'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,32 @@ FrameworkMethod getDataProviderMethod(FrameworkMethod testMethod) {
if (testMethod == null) {
throw new IllegalArgumentException("testMethod must not be null");
}
UseDataProvider testUsingDataProvider = testMethod.getAnnotation(UseDataProvider.class);
if (testUsingDataProvider == null) {
UseDataProvider useDataProvider = testMethod.getAnnotation(UseDataProvider.class);
if (useDataProvider == null) {
return null;
}
for (FrameworkMethod method : getTestClassInt().getAnnotatedMethods(DataProvider.class)) {
if (method.getName().equals(testUsingDataProvider.value())) {

TestClass dataProviderLocation = findDataProviderLocation(useDataProvider);
for (FrameworkMethod method : dataProviderLocation.getAnnotatedMethods(DataProvider.class)) {
if (method.getName().equals(useDataProvider.value())) {
return method;
}
}
return null;
}

/**
* <p>
* This method is package private (= visible) for testing.
* </p>
*/
TestClass findDataProviderLocation(UseDataProvider useDataProvider) {
if (useDataProvider.location().length == 0) {
return getTestClassInt();
}
return new TestClass(useDataProvider.location()[0]);
}

/**
* Checks if the given method is a valid data provider. A method is a valid data provider if and only if the method
* <ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
/**
* Mark a test method for use with a data provider. The value must be the name of a {@code @}{@link DataProvider}
* method.
* <p>
* Copyright by TNG Technology Consulting GmbH, Germany
* </p>
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface UseDataProvider {

/** The name of the data provider method to use test data from. */
/** The required name of the data provider method to use test data from. */
String value();

/** The class holding the data provider method, defaults to the test class (just first class will be considered). */
Class<?>[] location() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ public void testGetDataProviderMethodShouldReturnNullForNotFoundDataProviderMeth
doReturn(useDataProvider).when(testMethod).getAnnotation(UseDataProvider.class);
doReturn("notAvailableDataProviderMethod").when(useDataProvider).value();

doReturn(testClass).when(underTest).findDataProviderLocation(useDataProvider);

// When:
FrameworkMethod result = underTest.getDataProviderMethod(testMethod);

Expand All @@ -355,6 +357,8 @@ public void testGetDataProviderMethodShouldReturnDataProviderMethodIfItExists()
doReturn(useDataProvider).when(testMethod).getAnnotation(UseDataProvider.class);
doReturn(dataProviderMethodName).when(useDataProvider).value();

doReturn(testClass).when(underTest).findDataProviderLocation(useDataProvider);

doReturn(asList(dataProviderMethod)).when(testClass).getAnnotatedMethods(DataProvider.class);
doReturn(dataProviderMethodName).when(dataProviderMethod).getName();

Expand All @@ -365,6 +369,38 @@ public void testGetDataProviderMethodShouldReturnDataProviderMethodIfItExists()
assertThat(result).isEqualTo(dataProviderMethod);
}

@Test
public void testFindDataProviderLocationShouldReturnTestClassForNotSetLocationInUseDataProviderAnnotation() {

// Given:
UseDataProvider useDataProvider = mock(UseDataProvider.class);
doReturn(new Class<?>[0]).when(useDataProvider).location();

// When:
TestClass result = underTest.findDataProviderLocation(useDataProvider);

// Then:
assertThat(result).isEqualTo(testClass);
}

@Test
public void testFindDataProviderLocationShouldReturnTestClassContainingSetLocationInUseDataProviderAnnotation() {

final Class<?> dataProviderLocation = DataProviderRunnerTest.class;

// Given:
UseDataProvider useDataProvider = mock(UseDataProvider.class);
doReturn(new Class<?>[] { dataProviderLocation }).when(useDataProvider).location();

// When:
TestClass result = underTest.findDataProviderLocation(useDataProvider);

// Then:
assertThat(result).isNotNull();
// assertThat(result.getJavaClass()).isEqualTo(dataProviderLocation);
assertThat(result.getName()).isEqualTo(dataProviderLocation.getName());
}

@Test
public void testIsValidDataProviderMethodShouldReturnFalseIfDataProviderMethodIsNull() {

Expand All @@ -376,6 +412,7 @@ public void testIsValidDataProviderMethodShouldReturnFalseIfDataProviderMethodIs
// Then:
assertThat(result).isFalse();
}

@Test
public void testIsValidDataProviderMethodShouldReturnFalseIfItIsNotPublic() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ public void testIsEmptyString(String str) {
assertThat(isEmpty).isTrue();
}

@Test
@UseDataProvider(value = "dataProviderIsStringLengthGreaterTwo", location = StringDataProvider.class)
public void testIsStringLengthGreaterThanTwo(String str, boolean expected) {

// Given:

// When:
boolean isGreaterThanTwo = (str == null) ? false : str.length() > 2;

// Then:
assertThat(isGreaterThanTwo).isEqualTo(expected);
}

@DataProvider
public static Object[][] dataProviderAdd() {
// @formatter:off
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.tngtech.test.java.junit.dataprovider;

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

public class StringDataProvider {

@DataProvider
public static Object[][] dataProviderIsStringLengthGreaterTwo() {
// @formatter:off
return new Object[][] {
{ "", false },
{ "1", false },
{ "12", false },
{ "123", true },
{ "Test", true },
};
// @formatter:on
}
}

0 comments on commit cbc6276

Please sign in to comment.