/** * {@inheritDoc} * * @see DefaultDataProviderMethodResolver */ @Override public List<Method> resolve(DataProviderResolverContext context) { checkNotNull(context, "'context' must not be null"); List<Method> dataProviderMethods = findAnnotatedMethods(context.getLocations(), context.getDataProviderAnnotationClass()); List<Method> result = new ArrayList<Method>(); for (Method dataProviderMethod : dataProviderMethods) { if (context.useDataProviderNameConvention()) { if (isMatchingNameConvention(context.getTestMethod().getName(), dataProviderMethod.getName())) { result.add(dataProviderMethod); } } else if (dataProviderMethod.getName().equals(context.getDataProviderName())) { result.add(dataProviderMethod); } } return result; }
Class<?> currentClass = location; while (currentClass != null) { for (Method method : sorted(currentClass.getDeclaredMethods())) { Annotation foundAnnotation = method.getAnnotation(annotationClass); if (foundAnnotation != null && !isMethodShadowedBy(method, intermediateResult)) { intermediateResult.add(method);
@Test public void testResolveShouldReturnEmptyListForNotFoundDataProviderMethods() { // Given: doReturn(Collections.<Method>emptyList()).when(underTest).findAnnotatedMethods( ArgumentMatchers.<List<Class<?>>>any(), ArgumentMatchers.<Class<? extends Annotation>>any()); // When: List<Method> result = underTest.resolve(context); // Then: assertThat(result).isEmpty(); }
@Test public void testResolveShouldReturnListContainingOnlyConventionMatchingDataProviderMethods() { // Given: final Method method2 = Methods.anyMethod(); final Method method3 = Methods.anyMethod(); doReturn(asList(method, method2, method3)).when(underTest).findAnnotatedMethods( ArgumentMatchers.<List<Class<?>>>any(), ArgumentMatchers.<Class<? extends Annotation>>any()); doReturn(false).doReturn(true).doReturn(true).when(underTest).isMatchingNameConvention(any(String.class), any(String.class)); when(context.useDataProviderNameConvention()).thenReturn(true); when(context.getTestMethod()).thenReturn(Methods.anyMethod()); // When: List<Method> result = underTest.resolve(context); // Then: assertThat(result).containsOnly(method2, method3); }
@Test public void testIsMatchingNameConventionShouldReturnTrueIfDataProviderMethodHavingSameNameAsTestMethod() { // Given: final String testMethodName = "testMethod"; // When: boolean result = underTest.isMatchingNameConvention(testMethodName, testMethodName); // Then: assertThat(result).isTrue(); }
@Test public void testFindAnnotatedMethodsShouldReturnMethodsForAllLocations() { // Given: final List<Class<?>> locations = Arrays.<Class<?>>asList(ShadowingTestChild.class, ShadowingTestParent.class); // When: List<Method> result = underTest.findAnnotatedMethods(locations, TestAnnotation.class); // Then: assertThat(result).hasSize(11); }
@Test public void testResolveShouldThrowNullPointerExceptionIfContextIsNull() { // Given: expectedException.expect(NullPointerException.class); expectedException.expectMessage("'context' must not be null"); // When: underTest.resolve(null); // Then: expect exception }
@Test public void testIsMatchingNameConventionShouldReturnFalseForNamesNotStickToSupportedConventions() { // Given: final String testMethodName = "testMethod"; final String dataProviderMethodName = "notMatchingDataProviderMethod"; // When: boolean result = underTest.isMatchingNameConvention(testMethodName, dataProviderMethodName); // Then: assertThat(result).isFalse(); }
@Test public void testFindAnnotatedMethodsShouldNotShadowedMethodsHavingAnnotation() { // Given: final List<Class<?>> locations = Arrays.<Class<?>>asList(ShadowingTestChild.class); // When: List<Method> result = underTest.findAnnotatedMethods(locations, TestAnnotation.class); // Then: // @formatter:off assertThat(result).hasSize(7) .has(name("privateWithAnnotation"), atIndex(0)) .has(name("notShadowedDueToParameterLength"), atIndex(1)) .has(name("notShadowedDueToParameterTypes"), atIndex(2)) .has(name("shadowed"), atIndex(3)) .has(name("notShadowedDueToParameterLength"), atIndex(4)) .has(name("notShadowedDueToParameterTypes"), atIndex(5)) .has(name("shadowedButChildHasNoAnnotation"), atIndex(6)) ; // @formatter:on }
@Test public void testIsMatchingNameConventionShouldReturnTrueIfDataProviderMethodHavingDataProviderPrefixInsteadOfTest() { // Given: final String testMethodName = "testMethod"; final String dataProviderMethodName = "dataProviderMethod"; // When: boolean result = underTest.isMatchingNameConvention(testMethodName, dataProviderMethodName); // Then: assertThat(result).isTrue(); }
@Test public void testResolveShouldReturnListContainingExplicitelySpecifiedDataProviderMethod() { // Given: final Method method2 = Methods.getMethod(this.getClass(), "dataProviderMethod"); final Method method3 = Methods.anyMethod(); doReturn(asList(method, method2, method3)).when(underTest).findAnnotatedMethods( ArgumentMatchers.<List<Class<?>>>any(), ArgumentMatchers.<Class<? extends Annotation>>any()); when(context.useDataProviderNameConvention()).thenReturn(false); when(context.getDataProviderName()).thenReturn("dataProviderMethod"); // When: List<Method> result = underTest.resolve(context); // Then: assertThat(result).containsOnly(method2); }
@Test public void testIsMatchingNameConventionShouldReturnTrueIfDataProviderMethodHavingDataPrefixInsteadOfTest() { // Given: final String testMethodName = "testMethod"; final String dataProviderMethodName = "dataMethod"; // When: boolean result = underTest.isMatchingNameConvention(testMethodName, dataProviderMethodName); // Then: assertThat(result).isTrue(); }
@Test public void testIsMatchingNameConventionShouldReturnTrueIfDataProviderMethodHavingDataProviderPrefix() { // Given: final String testMethodName = "testMethod"; final String dataProviderMethodName = "dataProviderTestMethod"; // When: boolean result = underTest.isMatchingNameConvention(testMethodName, dataProviderMethodName); // Then: assertThat(result).isTrue(); }
@Test public void testIsMatchingNameConventionShouldReturnTrueIfDataProviderMethodHavingDataPrefix() { // Given: final String testMethodName = "testMethod"; final String dataProviderMethodName = "dataTestMethod"; // When: boolean result = underTest.isMatchingNameConvention(testMethodName, dataProviderMethodName); // Then: assertThat(result).isTrue(); }