[英]How can I turn this 'spring 3.1' oriented junit4 test with SpringJUnit4ClassRunner into a spring oriented junit3.8 based test?
This code uses Spring 3.1 and junit4 and spring-test 3.1. 此代码使用Spring 3.1和junit4以及spring-test 3.1。 I want to turn this code using and loading junit3.8.x. 我想使用和加载junit3.8.x来转换此代码。 This is due to a legacy build system. 这是由于遗留构建系统。 How can I do this? 我怎样才能做到这一点? Most of the online documentation for spring is centered around the approach below. 弹簧的大多数在线文档都围绕下面的方法。 I need to be able to 'load the spring classes'. 我需要能够'加载弹簧类'。 In this case I have a XML file, rest-servlet.xml
and the 'services' classes are annotated. 在这种情况下,我有一个XML文件, rest-servlet.xml
和'services'类被注释。 I want to be able to load that rest-servlet spring configuration file and setup spring before each test. 我希望能够在每次测试之前加载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>
TestActivityLog: 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);
}
}
This can be achieved by simulating SpringJUnitRunner
. 这可以通过模拟SpringJUnitRunner
来实现。 This class loads the application context from the provided config locations (in classpath) and autowiring the fields in test case. 此类从提供的配置位置(在类路径中)加载应用程序上下文,并在测试用例中自动装配字段。
Suppose we have a controller bean (defined in beans.xml) which we want to test. 假设我们要测试一个控制器bean(在beans.xml中定义)。
public class Controller {
public String message() {
return "Hello";
}
}
Test Case: 测试用例:
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());
}
}
For using JUnit 3.8 with Spring, can use the following template to write the test cases. 对于在Spring中使用JUnit 3.8,可以使用以下模板编写测试用例。 (See http://docs.spring.io/autorepo/docs/spring/3.0.x/reference/testing.html ) (见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" };
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.