有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用SpringJUnit4ClassRunner将这个面向“Spring3.1”的junit4测试转换为面向spring的junit3。基于8的测试?

这段代码使用Spring3.1和junit4以及SpringTest3.1。我想使用和加载junit3来转换这段代码。8.x.这是由于遗留构建系统造成的。我该怎么做?spring的大多数在线文档都围绕以下方法展开。我需要能够“加载spring类”。在本例中,我有一个XML文件,rest-servlet.xml,并且对“services”类进行了注释。我希望能够在每次测试之前加载RESTServletSpring配置文件并设置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>

TestActivityLog:

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);        
    }  

}

共 (2) 个答案

  1. # 1 楼答案

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

    假设我们有一个要测试的控制器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());
        }
    }
    
  2. # 2 楼答案

    对于将JUnit3.8与Spring一起使用,可以使用以下模板编写测试用例。(见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" };
        }
    }