Migrate JUnit 3 tests to JUnit 4

This commit migrates all remaining tests from JUnit 3 to JUnit 4, with
the exception of Spring's legacy JUnit 3.8 based testing framework that
is still in use in the spring-orm module.

Issue: SPR-13514
This commit is contained in:
Sam Brannen
2015-09-26 00:10:58 +02:00
parent 1580288815
commit d5ee787e1e
213 changed files with 4879 additions and 3939 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,18 +16,24 @@
package org.springframework.orm.hibernate3.support;
import junit.framework.TestCase;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.scope.ScopedObject;
import static org.junit.Assert.*;
/**
* Unit tests for {@link ScopedBeanInterceptor}.
*
* @author Costin Leau
*/
public class ScopedBeanInterceptorTests extends TestCase {
public class ScopedBeanInterceptorTests {
public void testInterceptorWithPlainObject() throws Exception {
ScopedBeanInterceptor interceptor = new ScopedBeanInterceptor();
private final ScopedBeanInterceptor interceptor = new ScopedBeanInterceptor();
@Test
public void interceptorWithPlainObject() throws Exception {
final Object realObject = new Object();
ScopedObject scoped = new ScopedObject() {
@@ -42,12 +48,12 @@ public class ScopedBeanInterceptorTests extends TestCase {
};
// default contract is to return null for default behavior
assertEquals(null, interceptor.getEntityName(realObject));
assertNull(interceptor.getEntityName(realObject));
assertEquals(realObject.getClass().getName(), interceptor.getEntityName(scoped));
}
public void testInterceptorWithCglibProxy() throws Exception {
ScopedBeanInterceptor interceptor = new ScopedBeanInterceptor();
@Test
public void interceptorWithCglibProxy() throws Exception {
final Object realObject = new Object();
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTarget(realObject);

View File

@@ -14,12 +14,11 @@
* limitations under the License.
*/
package org.springframework.test.annotation;
package org.springframework.test;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionAttributeSource;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -212,8 +212,8 @@ public abstract class AbstractDependencyInjectionSpringContextTests extends Abst
}
private void initManagedVariableNames() throws IllegalAccessException {
List managedVarNames = new LinkedList();
Class clazz = getClass();
List<String> managedVarNames = new LinkedList<>();
Class<?> clazz = getClass();
do {
Field[] fields = clazz.getDeclaredFields();
if (this.logger.isDebugEnabled()) {
@@ -243,7 +243,7 @@ public abstract class AbstractDependencyInjectionSpringContextTests extends Abst
clazz = clazz.getSuperclass();
} while (!clazz.equals(AbstractDependencyInjectionSpringContextTests.class));
this.managedVariableNames = (String[]) managedVarNames.toArray(new String[managedVarNames.size()]);
this.managedVariableNames = managedVarNames.toArray(new String[managedVarNames.size()]);
}
private boolean isProtectedInstanceField(Field field) {
@@ -277,12 +277,12 @@ public abstract class AbstractDependencyInjectionSpringContextTests extends Abst
}
}
private Field findField(Class clazz, String name) throws NoSuchFieldException {
private Field findField(Class<?> clazz, String name) throws NoSuchFieldException {
try {
return clazz.getDeclaredField(name);
}
catch (NoSuchFieldException ex) {
Class superclass = clazz.getSuperclass();
Class<?> superclass = clazz.getSuperclass();
if (superclass != AbstractSpringContextTests.class) {
return findField(superclass, name);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -71,7 +71,7 @@ import org.springframework.util.StringUtils;
* ({@link org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests})
*/
@Deprecated
public abstract class AbstractSingleSpringContextTests extends AbstractSpringContextTests {
abstract class AbstractSingleSpringContextTests extends AbstractSpringContextTests {
/** Application context this test will run against */
protected ConfigurableApplicationContext applicationContext;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,11 @@ package org.springframework.test;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
@@ -54,6 +59,7 @@ import org.springframework.util.StringUtils;
* @author Juergen Hoeller
* @author Sam Brannen
* @since 1.1.1
* @see #isDisabledInThisEnvironment
* @see AbstractSingleSpringContextTests
* @see AbstractDependencyInjectionSpringContextTests
* @see AbstractTransactionalSpringContextTests
@@ -62,7 +68,10 @@ import org.springframework.util.StringUtils;
* ({@link org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests})
*/
@Deprecated
public abstract class AbstractSpringContextTests extends ConditionalTestCase {
abstract class AbstractSpringContextTests extends TestCase {
/** Logger available to subclasses */
protected final Log logger = LogFactory.getLog(getClass());
/**
* Map of context keys returned by subclasses of this class, to Spring
@@ -72,6 +81,14 @@ public abstract class AbstractSpringContextTests extends ConditionalTestCase {
private static Map<String, ConfigurableApplicationContext> contextKeyToContextMap =
new HashMap<String, ConfigurableApplicationContext>();
private static int disabledTestCount;
/**
* Return the number of tests disabled in this environment.
*/
public static int getDisabledTestCount() {
return disabledTestCount;
}
/**
* Default constructor for AbstractSpringContextTests.
@@ -86,6 +103,39 @@ public abstract class AbstractSpringContextTests extends ConditionalTestCase {
super(name);
}
@Override
public void runBare() throws Throwable {
// getName will return the name of the method being run
if (isDisabledInThisEnvironment(getName())) {
recordDisabled();
this.logger.info("**** " + getClass().getName() + "." + getName()
+ " is disabled in this environment: " + "Total disabled tests = "
+ getDisabledTestCount());
return;
}
// Let JUnit handle execution
super.runBare();
}
/**
* Should this test run?
*
* @param testMethodName name of the test method
* @return whether the test should execute in the current environment
*/
protected boolean isDisabledInThisEnvironment(String testMethodName) {
return false;
}
/**
* Record a disabled test.
*
* @return the current disabled test count
*/
protected int recordDisabled() {
return ++disabledTestCount;
}
/**
* Explicitly add an ApplicationContext instance under a given key.

View File

@@ -43,7 +43,7 @@ import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
* ({@link org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests})
*/
@Deprecated
public abstract class AbstractTransactionalDataSourceSpringContextTests extends AbstractTransactionalSpringContextTests {
abstract class AbstractTransactionalDataSourceSpringContextTests extends AbstractTransactionalSpringContextTests {
protected JdbcTemplate jdbcTemplate;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -83,7 +83,7 @@ import org.springframework.transaction.support.DefaultTransactionDefinition;
* ({@link org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests})
*/
@Deprecated
public abstract class AbstractTransactionalSpringContextTests extends AbstractDependencyInjectionSpringContextTests {
abstract class AbstractTransactionalSpringContextTests extends AbstractDependencyInjectionSpringContextTests {
/** The transaction manager to use */
protected PlatformTransactionManager transactionManager;

View File

@@ -1,102 +0,0 @@
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test;
import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* This class is only used within tests in the spring-orm module.
*
* <p>Superclass for JUnit 3.8 based tests that allows conditional test execution
* at the individual test method level. The
* {@link #isDisabledInThisEnvironment(String) isDisabledInThisEnvironment()}
* method is invoked before the execution of each test method. Subclasses can
* override that method to return whether or not the given test should be
* executed. Note that the tests will still appear to have executed and passed;
* however, log output will show that the test was not executed.
*
* @author Rod Johnson
* @since 2.0
* @see #isDisabledInThisEnvironment
* @deprecated as of Spring 3.0, in favor of using the listener-based test context framework
* ({@link org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests})
*/
@Deprecated
public abstract class ConditionalTestCase extends TestCase {
private static int disabledTestCount;
/**
* Return the number of tests disabled in this environment.
*/
public static int getDisabledTestCount() {
return disabledTestCount;
}
/** Logger available to subclasses */
protected final Log logger = LogFactory.getLog(getClass());
/**
* Default constructor for ConditionalTestCase.
*/
public ConditionalTestCase() {
}
/**
* Constructor for ConditionalTestCase with a JUnit name.
*/
public ConditionalTestCase(String name) {
super(name);
}
@Override
public void runBare() throws Throwable {
// getName will return the name of the method being run
if (isDisabledInThisEnvironment(getName())) {
recordDisabled();
this.logger.info("**** " + getClass().getName() + "." + getName() + " is disabled in this environment: "
+ "Total disabled tests = " + getDisabledTestCount());
return;
}
// Let JUnit handle execution
super.runBare();
}
/**
* Should this test run?
* @param testMethodName name of the test method
* @return whether the test should execute in the current environment
*/
protected boolean isDisabledInThisEnvironment(String testMethodName) {
return false;
}
/**
* Record a disabled test.
* @return the current disabled test count
*/
protected int recordDisabled() {
return ++disabledTestCount;
}
}

View File

@@ -23,6 +23,7 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
@@ -45,7 +46,8 @@ import org.springframework.orm.jpa.ExtendedEntityManagerCreator;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.SharedEntityManagerCreator;
import org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager;
import org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests;
import org.springframework.test.AbstractAnnotationAwareTransactionalTests;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
/**
@@ -261,6 +263,7 @@ public abstract class AbstractJpaTests extends AbstractAnnotationAwareTransactio
/* AbstractSpringContextTests.addContext(Object, ApplicationContext) */
Class applicationContextClass = shadowingClassLoader.loadClass(ConfigurableApplicationContext.class.getName());
Method addContextMethod = shadowedTestClass.getMethod("addContext", Object.class, applicationContextClass);
ReflectionUtils.makeAccessible(addContextMethod);
addContextMethod.invoke(shadowedTestCase, configLocations, cachedContext);
// Invoke tests on shadowed test case