/** * Returns the dataprovider method that belongs to the given test method or {@code null} if no such dataprovider * exists or the test method is not marked for usage of a dataprovider * <p> * This method is package private (= visible) for testing. * </p> * * @param testMethod test method that uses a dataprovider * @return the dataprovider or {@code null} (if dataprovider does not exist or test method does not use any) * @throws IllegalArgumentException if given {@code testMethod} is {@code null} */ FrameworkMethod getDataProviderMethod(FrameworkMethod testMethod) { if (testMethod == null) { throw new IllegalArgumentException("testMethod must not be null"); } UseDataProvider useDataProvider = testMethod.getAnnotation(UseDataProvider.class); if (useDataProvider == null) { return null; } TestClass dataProviderLocation = findDataProviderLocation(useDataProvider); List<FrameworkMethod> dataProviderMethods = dataProviderLocation.getAnnotatedMethods(DataProvider.class); return findDataProviderMethod(dataProviderMethods, useDataProvider.value(), testMethod.getName()); }
@Test public void testResolveShouldReturnListContainingFoundDataProviderMethods() { // Given: final String testMethodName = "testMethodName"; final String dataProviderMethodName = "availableDataProviderMethodName"; doReturn(testMethodName).when(testMethod).getName(); doReturn(dataProviderMethodName).when(useDataProvider).value(); doReturn(new Class<?>[0]).when(useDataProvider).location(); doReturn(asList(testClass)).when(underTest).findDataProviderLocations(testMethod, new Class<?>[0]); doReturn(asList(dataProviderMethod)).when(underTest).findDataProviderMethods(asList(testClass), testMethodName, dataProviderMethodName); // When: List<FrameworkMethod> result = underTest.resolve(testMethod, useDataProvider); // Then: assertThat(result).containsOnly(dataProviderMethod); }
@Test public void testResolveShouldReturnEmptyListForNotFoundDataProviderMethod() { // Given: final String testMethodName = "testMethodName"; final String dataProviderMethodName = "notAvailableDataProviderMethodName"; doReturn(testMethodName).when(testMethod).getName(); doReturn(dataProviderMethodName).when(useDataProvider).value(); doReturn(new Class<?>[0]).when(useDataProvider).location(); doReturn(asList(testClass)).when(underTest).findDataProviderLocations(testMethod, new Class<?>[0]); doReturn(emptyList()).when(underTest).findDataProviderMethods(asList(testClass), testMethodName, dataProviderMethodName); // When: List<FrameworkMethod> result = underTest.resolve(testMethod, useDataProvider); // Then: assertThat(result).isEmpty(); }
@Test public void testGetDataProviderMethodShouldReturnEmptyListForNotFoundDataProviderMethod() { // Given: doReturn(useDataProvider).when(testMethod).getAnnotation(UseDataProvider.class); doReturn("notAvailableDataProviderMethod").when(useDataProvider).value(); doReturn(asList(dataProviderMethod)).when(testClass).getAnnotatedMethods(DataProvider.class); doReturn("availableDataProviderMethod").when(dataProviderMethod).getName(); doReturn(dataProviderMethodResolver).when(underTest).getResolverInstanceInt(any(Class.class)); // When: List<FrameworkMethod> result = underTest.getDataProviderMethods(testMethod); // Then: assertThat(result).isEmpty(); assertThat(underTest.dataProviderMethods).containsEntry(testMethod, result); }
@SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD") @Before public void setup() throws Exception { classSetupException = null; underTest = new DataProviderRunner(DataProviderRunnerTest.class); MockitoAnnotations.initMocks(this); underTest.dataConverter = dataConverter; underTest.testValidator = testValidator; underTest.testGenerator = testGenerator; doReturn(testClass).when(underTest).getTestClassInt(); doReturn(useDataProvider).when(testMethod).getAnnotation(UseDataProvider.class); doReturn(anyMethod()).when(testMethod).getMethod(); doReturn("testMethod").when(testMethod).getName(); doReturn(UseDataProvider.DEFAULT_VALUE).when(useDataProvider).value(); doReturn(new Class<?>[] { DataProviderMethodResolver.class }).when(useDataProvider).resolver(); }
/** * {@inheritDoc} * <p> * Look at the classes java doc for detailed description of the applied strategies. * * @return the resolved dataprovider method or an empty {@link List} if dataprovider could not be found (never null) * @see DefaultDataProviderMethodResolver */ @Override public List<FrameworkMethod> resolve(FrameworkMethod testMethod, UseDataProvider useDataProvider) { checkNotNull(testMethod, "testMethod must not be null"); checkNotNull(useDataProvider, "useDataProvider must not be null"); List<TestClass> dataProviderLocations = findDataProviderLocations(testMethod, useDataProvider.location()); return findDataProviderMethods(dataProviderLocations, testMethod.getName(), useDataProvider.value()); }
/** * {@inheritDoc} * <p> * Look at the classes java doc for detailed description of the applied strategies. * * @return the resolved dataprovider method or an empty {@link List} if dataprovider could not be found (never null) * @see DefaultDataProviderMethodResolver */ @Override public List<FrameworkMethod> resolve(FrameworkMethod testMethod, UseDataProvider useDataProvider) { checkNotNull(testMethod, "testMethod must not be null"); checkNotNull(useDataProvider, "useDataProvider must not be null"); List<TestClass> dataProviderLocations = findDataProviderLocations(testMethod, useDataProvider.location()); return findDataProviderMethods(dataProviderLocations, testMethod.getName(), useDataProvider.value()); }
/** * {@inheritDoc} * <p> * Look at the classes java doc for detailed description of the applied strategies. * * @return the resolved dataprovider method or an empty {@link List} if dataprovider could not be found (never null) * @see DefaultDataProviderMethodResolver */ @Override public List<FrameworkMethod> resolve(FrameworkMethod testMethod, UseDataProvider useDataProvider) { checkNotNull(testMethod, "testMethod must not be null"); checkNotNull(useDataProvider, "useDataProvider must not be null"); List<TestClass> dataProviderLocations = findDataProviderLocations(testMethod, useDataProvider.location()); return findDataProviderMethods(dataProviderLocations, testMethod.getName(), useDataProvider.value()); }