类没有@TestExecutionListeners

时间:2013-07-04 13:41:54

标签: spring unit-testing junit4

我将尝试测试我的端点中的一个方法(spring 3.1,junit 4.11)这是我的代码: 的applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns: p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc     
http://www.springframework.org/schem...ng-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schem...ontext-3.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schem...ring-cache.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:component-scan base-package="app.controller, app.samples" />
<context:annotation-config/>
<annotation-driven />

</beans>

和测试类:

package app.tests;
import app.samples.TableEndpoint;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autow ired;
import org.springframework.test.context.ContextConfigurat ion;
import org.springframework.test.context.junit4.SpringJUni t4ClassRunner;

@ContextConfiguration(locations = {"classpath:/WEB-INF/applicationContext.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class TableTest {

public TableTest() {
}
@Autowired
TableEndpoint tableEndpoint;
@Test
public void testTableEndpoint(){
String result = tableEndpoint.getDane().get(0);
String expResult = "Learn python in 7 days";
if(!result.equals(expResult)){
fail("not equals");
}
assertTrue(result.equals(expResult));
}
}

如果我进行测试我得到:
org.springframework.test.context.TestContextManage r retrieveTestExecutionListeners
INFO:@TestExecutionListeners不存在于类[class app.tests.TableTest]:使用默认值。 我搜索了它,但没有找到一些信息。感谢帮助!

1 个答案:

答案 0 :(得分:8)

你错过了TestExecutionListeners。将此注释添加到您的班级

@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class })
@ContextConfiguration(locations = {"classpath:/WEB-INF/applicationContext.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class TableTest {
...
}