Fix JDK7 method order-dependent issues in SRCCT
SpringRunnerContextCacheTests suffers from JDK7-related non-determinism in values returned from Class#getDeclaredMethods(), which in turn affects JUnit and its execution of @Test methods. This commit addresses this issue by introducing an OrderedMethodsSpringJUnit4ClassRunner that sorts the test methods alphabetically, which is actually required for SpringRunnerContextCacheTests to work properly. Issue: SPR-9789
This commit is contained in:
parent
21ebbb9c02
commit
300d41840b
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2011 the original author or authors.
|
* Copyright 2002-2012 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,18 +16,24 @@
|
||||||
|
|
||||||
package org.springframework.test.context;
|
package org.springframework.test.context;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.AfterClass;
|
import org.junit.AfterClass;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.model.FrameworkMethod;
|
||||||
|
import org.junit.runners.model.InitializationError;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.test.annotation.DirtiesContext;
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
|
import org.springframework.test.context.SpringRunnerContextCacheTests.OrderedMethodsSpringJUnit4ClassRunner;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||||
import static org.junit.Assert.*;
|
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JUnit 4 based unit test which verifies correct {@link ContextCache
|
* JUnit 4 based unit test which verifies correct {@link ContextCache
|
||||||
|
@ -40,9 +46,9 @@ import static org.junit.Assert.*;
|
||||||
* @since 2.5
|
* @since 2.5
|
||||||
* @see TestContextCacheKeyTests
|
* @see TestContextCacheKeyTests
|
||||||
*/
|
*/
|
||||||
@Ignore
|
@RunWith(OrderedMethodsSpringJUnit4ClassRunner.class)
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class })
|
||||||
@ContextConfiguration("/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml")
|
@ContextConfiguration("junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml")
|
||||||
public class SpringRunnerContextCacheTests {
|
public class SpringRunnerContextCacheTests {
|
||||||
|
|
||||||
private static ApplicationContext dirtiedApplicationContext;
|
private static ApplicationContext dirtiedApplicationContext;
|
||||||
|
@ -124,4 +130,30 @@ public class SpringRunnerContextCacheTests {
|
||||||
SpringRunnerContextCacheTests.dirtiedApplicationContext, this.applicationContext);
|
SpringRunnerContextCacheTests.dirtiedApplicationContext, this.applicationContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 3.2
|
||||||
|
*/
|
||||||
|
public static class OrderedMethodsSpringJUnit4ClassRunner extends SpringJUnit4ClassRunner {
|
||||||
|
|
||||||
|
public OrderedMethodsSpringJUnit4ClassRunner(Class<?> clazz) throws InitializationError {
|
||||||
|
super(clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected List<FrameworkMethod> computeTestMethods() {
|
||||||
|
List<FrameworkMethod> testMethods = super.computeTestMethods();
|
||||||
|
|
||||||
|
java.util.Collections.sort(testMethods, new Comparator<FrameworkMethod>() {
|
||||||
|
|
||||||
|
public int compare(FrameworkMethod method1, FrameworkMethod method2) {
|
||||||
|
return method1.getName().compareTo(method2.getName());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return testMethods;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue