java IllegalStateException:在单元测试中没有为范围“会话”注册范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29641084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IllegalStateException: No Scope registered for scope 'session' on unit test
提问by dwjohnston
I have a modified version of the mkyong MVC tutorial.
我有mkyong MVC 教程的修改版本。
I've added a business layer class Counter
.
我添加了一个业务层类Counter
。
public class Counter {
private int i;
public int count()
{
return (this.i++);
}
//getters and setters and constructors
}
In mvc-dispatcher-servlet.xml:
在 mvc-dispatcher-servlet.xml 中:
<bean id="counter" class="com.mkyong.common.Counter" scope="session">
<property name="i" value="0"></property>
</bean>
This works fine.
这工作正常。
I now want to create a unit test for this class
我现在想为这个类创建一个单元测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration()
public class TestCounter {
@Configuration
static class TestConfig
{
@Bean
public Counter c()
{
return new Counter();
}
}
@Autowired
private Counter c;
@Test
public void count_from1_returns2()
{
c.setI(1);
assertEquals(2, c.count());
}
}
If I run it like this, I'll get
如果我像这样运行它,我会得到
SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@655bf451] to prepare test instance [com.mkyong.common.TestCounter@780525d3]
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [com/mkyong/common/TestCounter-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [com/mkyong/common/TestCounter-context.xml] cannot be opened because it does not exist
So we need to specify where our context is:
所以我们需要指定我们的上下文在哪里:
@ContextConfiguration(locations="file:src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml")
Now if I run this I get:
现在,如果我运行它,我会得到:
SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@5a2611a6] to prepare test instance [com.mkyong.common.TestCounter@7950d786]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.mkyong.common.TestCounter': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mkyong.common.Counter com.mkyong.common.TestCounter.c; nested exception is java.lang.IllegalStateException: No Scope registered for scope 'session'
Why is this happening, and how do I resolve it?
为什么会发生这种情况,我该如何解决?
回答by codependent
You just need to add @WebAppConfiguration
in your test class to enable MVC scopes (request, session...)
您只需要@WebAppConfiguration
在您的测试类中添加以启用 MVC 范围(请求、会话...)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration()
@WebAppConfiguration
public class TestCounter {