Skip to content

Commit dfe0530

Browse files
committed
Upgrade to JUnit 4.11 snapshot in support of JDK7
Class#getDeclaredMembers returns arbitrary results under JDK7. This results in non-deterministic execution of JUnit test methods, often revealing unintended dependencies between methods that rely on a specific order to succeed. JUnit 4.11 contains support for predictable test ordering [1], but at the time of this commit, JUnit 4.11 has not yet been released. Therefore we are testing against a snapshot version [2], which has been uploaded to repo.springsource.org [3] for easy access. Note that this artifact may be removed when JUnit 4.11 goes GA. - Care has been taken to ensure that spring-test's compile-time dependency on JUnit remains at 4.10. This means that the spring-test pom.xml will continue to have an optional <dependency> on JUnit 4.10, instead of the 4.11 snapshot. - For reasons not fully understood, the upgrade to the 4.11 snapshot of junit-dep caused NoSuchMethodErrors around certain Hamcrest types, particularly CoreMatchers and Matchers. import statements have been updated accordingly throughout affected test cases. - Runtime errors also occurred around uses of JUnit @rule and ExpectedException. These have been reverted to use simpler mechanisms like @test(expected) in the meantime. - Some test methods with order-based dependencies on one another have been renamed in order to fall in line with JUnit 4.11's new method ordering (as opposed to actually fixing the inter-test dependencies). In other areas, the fix was as simple as adding a tearDown method and cleaning up state. - For no apparent reason, the timeout in AspectJAutoProxyCreatorTests' testAspectsAndAdvisorNotAppliedToPrototypeIsFastEnough method begins to be exceeded. Prior to this commit the timeout value was 3000 ms; on the CI server under Linux/JDK6 and JDK7, the test begins taking anywhere from 3500-5500 ms with this commit. It is presumed that this is an incidental artifact of the upgrade to JUnit 4.11. In any case, there are no changes to src/main in this commit, so this should not actually represent a performance risk for Spring Framework users. The timeout has been increased to 6000 ms to accommodate this situation. [1]: https://github.com/KentBeck/junit/pull/293 [2]: https://github.com/downloads/KentBeck/junit/junit-dep-4.11-SNAPSHOT-20120805-1225.jar [3]: https://repo.springsource.org/simple/ext-release-local/junit/junit-dep/4.11.20120805.1225 Issue: SPR-9783
1 parent a9a90ca commit dfe0530

File tree

15 files changed

+70
-103
lines changed

15 files changed

+70
-103
lines changed

build.gradle

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ configure(allprojects) {
1919

2020
ext.aspectjVersion = '1.6.12'
2121
ext.hsqldbVersion='1.8.0.10'
22-
ext.junitVersion = '4.10'
22+
ext.junitVersion = '4.11.20120805.1225' // temporary use of snapshot; spring-test
23+
// still builds against on 4.10
2324

2425
[compileJava, compileTestJava]*.options*.compilerArgs = ['-Xlint:none']
2526

@@ -33,9 +34,6 @@ configure(allprojects) {
3334
}
3435

3536
dependencies {
36-
testCompile ("junit:junit-dep:${junitVersion}") {
37-
exclude group: 'org.hamcrest', module: 'hamcrest-core'
38-
}
3937
testCompile "org.hamcrest:hamcrest-all:1.1"
4038
testCompile "org.easymock:easymock:2.5.1"
4139
}
@@ -48,6 +46,14 @@ configure(allprojects) {
4846
}
4947
}
5048

49+
configure(subprojects - project(":spring-test")) {
50+
dependencies {
51+
testCompile ("junit:junit-dep:${junitVersion}") {
52+
exclude group: 'org.hamcrest', module: 'hamcrest-core'
53+
}
54+
}
55+
}
56+
5157
configure(subprojects) { subproject ->
5258
apply from: "${rootProject.projectDir}/publish-maven.gradle"
5359

@@ -521,7 +527,7 @@ project('spring-test') {
521527
compile(project(":spring-web"), optional)
522528
compile(project(":spring-webmvc"), optional)
523529
compile(project(":spring-webmvc-portlet"), optional)
524-
compile("junit:junit-dep:${junitVersion}") { dep ->
530+
compile("junit:junit-dep:4.10") { dep ->
525531
optional dep
526532
// We already have hamcrest-all as a global testCompile dependency.
527533
exclude group: 'org.hamcrest', module: 'hamcrest-core'

spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactoryTests.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,9 +40,7 @@
4040
import org.aspectj.lang.annotation.Pointcut;
4141
import org.aspectj.lang.reflect.MethodSignature;
4242

43-
import org.junit.Rule;
4443
import org.junit.Test;
45-
import org.junit.rules.ExpectedException;
4644

4745
import org.springframework.aop.Advisor;
4846
import org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor;
@@ -73,9 +71,6 @@
7371
*/
7472
public abstract class AbstractAspectJAdvisorFactoryTests {
7573

76-
@Rule
77-
public ExpectedException thrown = ExpectedException.none();
78-
7974
/**
8075
* To be overridden by concrete test subclasses.
8176
* @return the fixture
@@ -586,11 +581,9 @@ public void testFailureWithoutExplicitDeclarePrecedence() {
586581
itb.getAge();
587582
}
588583

589-
@Test
584+
@Test(expected=IllegalArgumentException.class)
590585
public void testDeclarePrecedenceNotSupported() {
591586
TestBean target = new TestBean();
592-
thrown.expect(IllegalArgumentException.class);
593-
thrown.expectMessage("DeclarePrecendence not presently supported in Spring AOP");
594587
MetadataAwareAspectInstanceFactory aspectInstanceFactory = new SingletonMetadataAwareAspectInstanceFactory(
595588
new DeclarePrecedenceShouldSucceed(), "someBean");
596589
createProxy(target, getFixture().getAdvisors(aspectInstanceFactory),

spring-context/src/test/java/org/springframework/aop/aspectj/autoproxy/AspectJAutoProxyCreatorTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public void testAspectsAndAdvisorNotAppliedToPrototypeIsFastEnough() {
151151

152152
// What's a reasonable expectation for _any_ server or developer machine load?
153153
// 3 seconds?
154-
assertStopWatchTimeLimit(sw, 3000);
154+
assertStopWatchTimeLimit(sw, 6000);
155155
}
156156

157157
@Test

spring-context/src/test/java/org/springframework/context/annotation/AnnotationConfigApplicationContextTests.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,20 @@
1616

1717
package org.springframework.context.annotation;
1818

19-
import static java.lang.String.format;
2019
import java.util.Map;
2120

22-
import static org.hamcrest.CoreMatchers.*;
23-
import static org.junit.Assert.*;
2421
import org.junit.Test;
25-
import static org.junit.matchers.JUnitMatchers.*;
26-
2722
import org.springframework.beans.factory.FactoryBean;
2823
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2924
import org.springframework.beans.factory.annotation.Autowired;
3025
import org.springframework.beans.factory.config.BeanPostProcessor;
3126
import org.springframework.context.annotation6.ComponentForScanning;
3227
import org.springframework.context.annotation6.ConfigForScanning;
3328
import org.springframework.context.annotation6.Jsr330NamedForScanning;
29+
30+
import static java.lang.String.*;
31+
import static org.hamcrest.Matchers.*;
32+
import static org.junit.Assert.*;
3433
import static org.springframework.util.StringUtils.*;
3534

3635
/**
@@ -229,13 +228,13 @@ static class AutowiredConfig {
229228
}
230229
}
231230

232-
static class UntypedFactoryBean implements FactoryBean {
231+
static class UntypedFactoryBean implements FactoryBean<Object> {
233232

234233
public Object getObject() {
235234
return null;
236235
}
237236

238-
public Class getObjectType() {
237+
public Class<?> getObjectType() {
239238
return null;
240239
}
241240

spring-context/src/test/java/org/springframework/context/config/ContextNamespaceHandlerTests.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616

1717
package org.springframework.context.config;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertFalse;
21-
2219
import java.util.Calendar;
2320
import java.util.Date;
2421
import java.util.Map;
2522

23+
import org.junit.After;
2624
import org.junit.Test;
25+
2726
import org.springframework.beans.factory.config.PlaceholderConfigurerSupport;
2827
import org.springframework.beans.factory.config.PropertyOverrideConfigurer;
2928
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
@@ -33,6 +32,8 @@
3332
import org.springframework.core.io.ClassPathResource;
3433
import org.springframework.mock.env.MockEnvironment;
3534

35+
import static org.junit.Assert.*;
36+
3637
/**
3738
* @author Arjen Poutsma
3839
* @author Dave Syer
@@ -41,6 +42,11 @@
4142
*/
4243
public class ContextNamespaceHandlerTests {
4344

45+
@After
46+
public void tearDown() {
47+
System.getProperties().remove("foo");
48+
}
49+
4450
@Test
4551
public void propertyPlaceholder() throws Exception {
4652
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(

spring-core/src/test/java/org/springframework/core/convert/converter/ConvertingComparatorTests.java

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,65 +16,52 @@
1616

1717
package org.springframework.core.convert.converter;
1818

19-
import static org.hamcrest.Matchers.is;
20-
import static org.junit.Assert.*;
2119
import java.util.ArrayList;
2220
import java.util.Collections;
2321
import java.util.Comparator;
2422
import java.util.LinkedHashMap;
2523
import java.util.Map;
2624
import java.util.Map.Entry;
2725

28-
import org.junit.Rule;
2926
import org.junit.Test;
30-
import org.junit.rules.ExpectedException;
27+
3128
import org.springframework.core.convert.ConversionService;
32-
import org.springframework.core.convert.converter.Converter;
33-
import org.springframework.core.convert.converter.ConvertingComparator;
3429
import org.springframework.core.convert.support.DefaultConversionService;
3530
import org.springframework.util.comparator.ComparableComparator;
3631

32+
import static org.hamcrest.CoreMatchers.*;
33+
import static org.junit.Assert.*;
34+
3735
/**
3836
* Tests for {@link ConvertingComparator}.
3937
*
4038
* @author Phillip Webb
4139
*/
4240
public class ConvertingComparatorTests {
4341

44-
@Rule
45-
public ExpectedException thown = ExpectedException.none();
46-
4742
private final StringToInteger converter = new StringToInteger();
4843

4944
private final ConversionService conversionService = new DefaultConversionService();
5045

5146
private final TestComparator comparator = new TestComparator();
5247

53-
@Test
48+
@Test(expected=IllegalArgumentException.class)
5449
public void shouldThrowOnNullComparator() throws Exception {
55-
thown.expect(IllegalArgumentException.class);
56-
thown.expectMessage("Comparator must not be null");
5750
new ConvertingComparator<String, Integer>(null, this.converter);
5851
}
5952

60-
@Test
53+
@Test(expected=IllegalArgumentException.class)
6154
public void shouldThrowOnNullConverter() throws Exception {
62-
thown.expect(IllegalArgumentException.class);
63-
thown.expectMessage("Converter must not be null");
6455
new ConvertingComparator<String, Integer>(this.comparator, null);
6556
}
6657

67-
@Test
58+
@Test(expected=IllegalArgumentException.class)
6859
public void shouldThrowOnNullConversionService() throws Exception {
69-
thown.expect(IllegalArgumentException.class);
70-
thown.expectMessage("ConversionService must not be null");
7160
new ConvertingComparator<String, Integer>(this.comparator, null, Integer.class);
7261
}
7362

74-
@Test
63+
@Test(expected=IllegalArgumentException.class)
7564
public void shouldThrowOnNullType() throws Exception {
76-
thown.expect(IllegalArgumentException.class);
77-
thown.expectMessage("TargetType must not be null");
7865
new ConvertingComparator<String, Integer>(this.comparator,
7966
this.conversionService, null);
8067
}
@@ -145,8 +132,8 @@ private static class TestComparator extends ComparableComparator<Integer> {
145132
private boolean called;
146133

147134
public int compare(Integer o1, Integer o2) {
148-
assertThat(o1, is(Integer.class));
149-
assertThat(o2, is(Integer.class));
135+
assertThat(o1, instanceOf(Integer.class));
136+
assertThat(o2, instanceOf(Integer.class));
150137
this.called = true;
151138
return super.compare(o1, o2);
152139
};

spring-core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
import java.util.Map;
2121
import java.util.Properties;
2222

23-
import org.hamcrest.Matchers;
23+
import org.hamcrest.CoreMatchers;
24+
2425
import org.junit.Before;
2526
import org.junit.Test;
2627

2728
import org.springframework.core.convert.ConversionException;
2829
import org.springframework.mock.env.MockPropertySource;
2930

30-
import static org.hamcrest.CoreMatchers.*;
31+
import static org.hamcrest.Matchers.*;
3132
import static org.junit.Assert.*;
3233

3334
/**
@@ -371,7 +372,7 @@ public void resolveNestedPropertyPlaceholders() {
371372
try {
372373
pr.getProperty("p5");
373374
} catch (IllegalArgumentException ex) {
374-
assertThat(ex.getMessage(), Matchers.containsString(
375+
assertThat(ex.getMessage(), containsString(
375376
"Could not resolve placeholder 'bogus' in string value [${p1}:${p2}:${bogus}]"));
376377
}
377378
assertThat(pr.getProperty("p6"), equalTo("v1:v2:def"));

spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,12 @@
2626
import java.util.Map;
2727

2828
import org.junit.Test;
29-
3029
import org.springframework.mock.env.MockPropertySource;
3130

32-
import static java.lang.String.*;
33-
34-
import static org.hamcrest.CoreMatchers.*;
3531

32+
import static java.lang.String.*;
33+
import static org.hamcrest.Matchers.*;
3634
import static org.junit.Assert.*;
37-
import static org.junit.matchers.JUnitMatchers.*;
38-
3935
import static org.springframework.core.env.AbstractEnvironment.*;
4036

4137
/**

spring-core/src/test/java/org/springframework/core/io/ClassPathResourceTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,21 @@
1616

1717
package org.springframework.core.io;
1818

19-
import static org.hamcrest.CoreMatchers.instanceOf;
2019
import static org.junit.Assert.assertEquals;
2120
import static org.junit.Assert.assertThat;
2221
import static org.junit.Assert.assertTrue;
2322
import static org.junit.Assert.fail;
24-
import static org.junit.internal.matchers.StringContains.containsString;
2523

2624
import java.io.FileNotFoundException;
2725
import java.io.IOException;
26+
2827
import java.util.regex.Matcher;
2928
import java.util.regex.Pattern;
3029

3130
import org.junit.Test;
3231

32+
import static org.hamcrest.Matchers.*;
33+
3334
/**
3435
* Unit tests that serve as regression tests for the bugs described in SPR-6888
3536
* and SPR-9413.

spring-core/src/test/java/org/springframework/util/comparator/BooleanComparatorTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.util.comparator;
1818

19-
import static org.hamcrest.Matchers.is;
19+
import static org.hamcrest.CoreMatchers.is;
2020
import static org.junit.Assert.*;
2121

2222
import java.util.Comparator;

0 commit comments

Comments
 (0)