我怎样才能将这个使用 SpringJUnit4ClassRunner 的面向 'spring 3.1' 的 junit4 测试转换为基于 spring 的基于 junit3.8 的测试?

How can I turn this 'spring 3.1' oriented junit4 test with SpringJUnit4ClassRunner into a spring oriented junit3.8 based test?

此代码使用 Spring 3.1 和 junit4 以及 spring-test 3.1。我想使用并加载 junit3.8.x 来转换此代码。这是由于遗留构建系统。我怎样才能做到这一点? spring 的大部分在线文档都围绕以下方法展开。我需要能够 'load the spring classes'。在这种情况下,我有一个 XML 文件, rest-servlet.xml 和 'services' 类 被注释。我希望能够在每次测试之前加载 rest-servlet spring 配置文件和设置 spring。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd">        
       <context:component-scan base-package="com.ca.services.rest.*,com.ca.services.test.*" />
       <mvc:annotation-driven />   
  </beans>

测试活动日志:

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.ca.services.rest.activity.services.ActivityDaoRepository;
import com.ca.services.rest.activity.services.ActivityService;
import com.ca.services.rest.activity.services.impl.ActivityServiceImpl;
import com.ca.services.test.mock.MockActivityDaoRepository;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:**/WEB-INF/rest-servlet.xml"})
public class TestActivityLog {

    @Autowired
    @Qualifier("mockActivityDaoRepository")
    private MockActivityDaoRepository repository;

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    public TestActivityLog() {
        super();
    }

    @Before
    public void setup() throws Exception {       
    }

    @Test
    public void testOne() {    
        Assert.assertEquals("abc", "abc");
    }    

    public void testService2() {
        final ActivityDaoRepository repo = repository;
        final String chk1 = "[POL.ActivityAPI:as1.0.0]";
        final String chk2 = String.valueOf(repo.getVersion());
        Assert.assertEquals(chk1, chk2);
    }


    public void testService3() {
        final ActivityService service = new ActivityServiceImpl(repository);        
    }  

}

这可以通过模拟SpringJUnitRunner来实现。 class 从提供的配置位置(在 class 路径中)加载应用程序上下文并自动装配测试用例中的字段。

假设我们有一个要测试的控制器 bean(在 beans.xml 中定义)。

public class Controller {

    public String message() {
        return "Hello";
    }

}

测试用例:

public class Spring38TestCase extends TestCase {

    private Controller controller;
    private ApplicationContext context;

    @Override
    protected void setUp() throws Exception {
        //Initializing spring application context.
        context = new ClassPathXmlApplicationContext("beans.xml");
        //Setting fields in test case explicitly in case of auto wiring
        controller = context.getBean(Controller.class);
    }

    public void testController() {
        assertEquals("Hello", controller.message());
    }
}

对于Spring使用JUnit 3.8,可以使用下面的模板来编写测试用例。 (参见 http://docs.spring.io/autorepo/docs/spring/3.0.x/reference/testing.html

public class MyServiceTestCase 
       extends AbstractDependencyInjectionSpringContextTests {

    private myService MyService;

    @Test
    public void testAddService() {
        // a test case
    }

    /**
     * The spring context configuration
     */
    @Override
    protected String[] getConfigLocations() {
        return new String[] { "rest-servlet.xml" };
    }
}