DataProviderMethodResolverHelper { public static List<Method> findDataProviderMethods(DataProviderResolverContext context) { checkNotNull(context, "'context' must not be null"); List<Method> result = new ArrayList<Method>(); for (Class<? extends DataProviderMethodResolver> resolverClass : context.getResolverClasses()) { DataProviderMethodResolver resolver = newInstance(resolverClass); List<Method> dataProviderMethods = resolver.resolve(context); if (context.getResolveStrategy() == ResolveStrategy.UNTIL_FIRST_MATCH && !dataProviderMethods.isEmpty()) { result.addAll(dataProviderMethods); break; } else if (context.getResolveStrategy() == ResolveStrategy.AGGREGATE_ALL_MATCHES) { result.addAll(dataProviderMethods); } } return result; }  static List<Method> findDataProviderMethods(DataProviderResolverContext context);  }
@Test public void testFindDataProviderMethodsShouldThrowNullPointerExceptionIfContextIsNull() { expectedException.expect(NullPointerException.class); expectedException.expectMessage("'context' must not be null"); DataProviderMethodResolverHelper.findDataProviderMethods(null); }
@Test public void testFindDataProviderMethodsShouldReturnEmptyListIfContextDoesNotContainAnyResolver() { when(context.getResolverClasses()) .thenReturn(Collections.<Class<? extends DataProviderMethodResolver>>emptyList()); List<Method> result = DataProviderMethodResolverHelper.findDataProviderMethods(context); assertThat(result).isEmpty(); }
@Test public void testFindDataProviderMethodsShouldReturnEmptyListIfSingleResolverReturnsEmptyList() { @SuppressWarnings("unchecked") List<Class<? extends DataProviderMethodResolver>> resolverClasses = Collections.<Class<? extends DataProviderMethodResolver>>singletonList(TestResolver.class); when(context.getResolverClasses()).thenReturn(resolverClasses); TestResolver.methods = emptyList(); List<Method> result = DataProviderMethodResolverHelper.findDataProviderMethods(context); assertThat(result).isEmpty(); assertThat(TestResolver.usedContexts).containsOnly(context); }
@Test public void testFindDataProviderMethodsShouldReturnFirstMatchIfMutlipleResolverReturnNonEmptyList() { @SuppressWarnings("unchecked") final List<Class<? extends DataProviderMethodResolver>> resolverClasses = Arrays .<Class<? extends DataProviderMethodResolver>>asList(TestResolver.class, TestResolver.class); final Method method = Methods.anyMethod(); when(context.getResolverClasses()).thenReturn(resolverClasses); when(context.getResolveStrategy()).thenReturn(ResolveStrategy.UNTIL_FIRST_MATCH); TestResolver.methods = singletonList(method); List<Method> result = DataProviderMethodResolverHelper.findDataProviderMethods(context); assertThat(result).containsOnly(method); assertThat(TestResolver.usedContexts).containsOnly(context); }
@Test public void testFindDataProviderMethodsShouldReturnFirstMatchEvenIfMutlipleResolverReturnNonEmptyList() { @SuppressWarnings("unchecked") final List<Class<? extends DataProviderMethodResolver>> resolverClasses = Arrays .<Class<? extends DataProviderMethodResolver>>asList(TestResolver.class, TestResolver.class); when(context.getResolverClasses()).thenReturn(resolverClasses); when(context.getResolveStrategy()).thenReturn(ResolveStrategy.UNTIL_FIRST_MATCH); TestResolver.methods = emptyList(); List<Method> result = DataProviderMethodResolverHelper.findDataProviderMethods(context); assertThat(result).isEmpty(); assertThat(TestResolver.usedContexts).containsOnly(context, context); }
@Test public void testFindDataProviderMethodsShouldReturnAllMatchesIfMutlipleResolverReturnNonEmptyList() { @SuppressWarnings("unchecked") final List<Class<? extends DataProviderMethodResolver>> resolverClasses = Arrays .<Class<? extends DataProviderMethodResolver>>asList(TestResolver.class, TestResolver.class); final Method method = Methods.anyMethod(); when(context.getResolverClasses()).thenReturn(resolverClasses); when(context.getResolveStrategy()).thenReturn(ResolveStrategy.AGGREGATE_ALL_MATCHES); TestResolver.methods = singletonList(method); List<Method> result = DataProviderMethodResolverHelper.findDataProviderMethods(context); assertThat(result).containsOnly(method, method); assertThat(TestResolver.usedContexts).containsOnly(context, context); }
@Test public void testFindDataProviderMethodsShouldThrowIllegalArgumentExceptionIfResolverClassHasNoDefaultConstructorExists() { @SuppressWarnings("unchecked") final List<Class<? extends DataProviderMethodResolver>> resolverClasses = Collections.<Class<? extends DataProviderMethodResolver>>singletonList(NoDefaultConstructor.class); when(context.getResolverClasses()).thenReturn(resolverClasses); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Could not find default constructor to instantiate"); DataProviderMethodResolverHelper.findDataProviderMethods(context); }
@Test public void testFindDataProviderMethodsShouldThrowIllegalArgumentExceptionIfResolverClassHasIsAbstract() { @SuppressWarnings("unchecked") final List<Class<? extends DataProviderMethodResolver>> resolverClasses = Collections.<Class<? extends DataProviderMethodResolver>>singletonList(AbstractClass.class); when(context.getResolverClasses()).thenReturn(resolverClasses); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Could not instantiate"); expectedException.expectMessage("using default constructor"); DataProviderMethodResolverHelper.findDataProviderMethods(context); }
@Test public void testFindDataProviderMethodsShouldThrowIllegalArgumentExceptionIfResolverClassesDefaultConstructorThrowsException() { @SuppressWarnings("unchecked") final List<Class<? extends DataProviderMethodResolver>> resolverClasses = Collections.<Class<? extends DataProviderMethodResolver>>singletonList(ExceptionInDefaultConstructor.class); when(context.getResolverClasses()).thenReturn(resolverClasses); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("default constructor of "); expectedException.expectMessage("has thrown an exception"); DataProviderMethodResolverHelper.findDataProviderMethods(context); }