Fix broken test in SpringJUnit4ConcurrencyTests

Prior to this commit, SpringJUnit4ConcurrencyTests was broken due to
recent additions of test methods in SampleTests.

This commit fixes the broken test by removing hard coded constants and
replacing them with dynamic lookups for JUnit 4 @Test methods.
This commit is contained in:
Sam Brannen 2017-01-25 05:56:02 +01:00
parent 1443c0808d
commit 4e65c10272
1 changed files with 50 additions and 36 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,6 +16,14 @@
package org.springframework.test.context.junit4.concurrency; package org.springframework.test.context.junit4.concurrency;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.experimental.ParallelComputer; import org.junit.experimental.ParallelComputer;
@ -37,8 +45,11 @@ import org.springframework.test.web.servlet.samples.context.JavaConfigTests;
import org.springframework.test.web.servlet.samples.context.WebAppResourceTests; import org.springframework.test.web.servlet.samples.context.WebAppResourceTests;
import org.springframework.tests.Assume; import org.springframework.tests.Assume;
import org.springframework.tests.TestGroup; import org.springframework.tests.TestGroup;
import org.springframework.util.ReflectionUtils;
import static org.springframework.test.context.junit4.JUnitTestingUtils.runTestsAndAssertCounters; import static java.util.stream.Collectors.*;
import static org.springframework.core.annotation.AnnotatedElementUtils.*;
import static org.springframework.test.context.junit4.JUnitTestingUtils.*;
/** /**
* Concurrency tests for the {@link SpringRunner}, {@link SpringClassRule}, and * Concurrency tests for the {@link SpringRunner}, {@link SpringClassRule}, and
@ -68,52 +79,55 @@ import static org.springframework.test.context.junit4.JUnitTestingUtils.runTests
public class SpringJUnit4ConcurrencyTests { public class SpringJUnit4ConcurrencyTests {
// @formatter:off // @formatter:off
private static final Class<?>[] testClasses = new Class[] { private final Class<?>[] testClasses = new Class[] {
// Basics // Basics
/* 9 */ SpringJUnit4ClassRunnerAppCtxTests.class, SpringJUnit4ClassRunnerAppCtxTests.class,
/* 9 */ InheritedConfigSpringJUnit4ClassRunnerAppCtxTests.class, InheritedConfigSpringJUnit4ClassRunnerAppCtxTests.class,
/* 2 */ SpringJUnit47ClassRunnerRuleTests.class, SpringJUnit47ClassRunnerRuleTests.class,
/* 2 */ ParameterizedSpringRuleTests.class, ParameterizedSpringRuleTests.class,
// Transactional // Transactional
/* 2 */ MethodLevelTransactionalSpringRunnerTests.class, MethodLevelTransactionalSpringRunnerTests.class,
/* 4 */ TimedTransactionalSpringRunnerTests.class, TimedTransactionalSpringRunnerTests.class,
// Web and Scopes // Web and Scopes
/* 1 */ DispatcherWacRootWacEarTests.class, /* 2 ignored */ DispatcherWacRootWacEarTests.class,
/* 3 */ BasicAnnotationConfigWacSpringRuleTests.class, BasicAnnotationConfigWacSpringRuleTests.class,
/* 2 */ RequestAndSessionScopedBeansWacTests.class, RequestAndSessionScopedBeansWacTests.class,
/* 1 */ WebSocketServletServerContainerFactoryBeanTests.class, WebSocketServletServerContainerFactoryBeanTests.class,
// Spring MVC Test // Spring MVC Test
/* 2 */ JavaConfigTests.class, JavaConfigTests.class,
/* 3 */ WebAppResourceTests.class, WebAppResourceTests.class,
/* 4 */ SampleTests.class SampleTests.class
}; };
// @formatter:on // @formatter:on
/** @BeforeClass
* The number of tests in all {@link #testClasses}. public static void abortIfLongRunningTestGroupIsNotEnabled() {
* Assume.group(TestGroup.LONG_RUNNING);
* <p>The current number of tests per test class is tracked as a comment }
* before each class reference above. The following constant must therefore
* be the sum of those values.
*
* <p>This is admittedly fragile, but there's unfortunately not really a
* better way to count the number of tests without re-implementing JUnit 4's
* discovery algorithm. Plus, the presence of parameterized tests makes it
* even more difficult to count programmatically.
*/
private static final int TESTS = 44;
private static final int FAILED = 0;
private static final int IGNORED = 2;
private static final int ABORTED = 0;
@Test @Test
public void runAllTestsConcurrently() throws Exception { public void runAllTestsConcurrently() throws Exception {
final int FAILED = 0;
Assume.group(TestGroup.LONG_RUNNING); final int ABORTED = 0;
final int IGNORED = countAnnotatedMethods(Ignore.class);
// +1 since ParameterizedSpringRuleTests is parameterized
final int TESTS = countAnnotatedMethods(Test.class) + 1 - IGNORED;
runTestsAndAssertCounters(new ParallelComputer(true, true), TESTS, FAILED, TESTS, IGNORED, ABORTED, runTestsAndAssertCounters(new ParallelComputer(true, true), TESTS, FAILED, TESTS, IGNORED, ABORTED,
testClasses); this.testClasses);
}
private int countAnnotatedMethods(Class<? extends Annotation> annotationType) {
return Arrays.stream(this.testClasses)
.map(testClass -> getAnnotatedMethods(testClass, annotationType))
.flatMapToInt(list -> IntStream.of(list.size()))
.sum();
}
private List<Method> getAnnotatedMethods(Class<?> clazz, Class<? extends Annotation> annotationType) {
return Arrays.stream(ReflectionUtils.getUniqueDeclaredMethods(clazz))
.filter(method -> hasAnnotation(method, annotationType))
.collect(toList());
} }
} }