相关疑难解决方法(0)

IllegalStateException:没有在单元测试中为范围'session'注册的Scope

我有一个mkyong MVC教程的修改版本.

我添加了一个业务层类Counter.

public class Counter {

    private int i;


    public int count()
    {
        return (this.i++);
    }

    //getters and setters and constructors
}
Run Code Online (Sandbox Code Playgroud)

在mvc-dispatcher-servlet.xml中:

<bean id="counter" class="com.mkyong.common.Counter" scope="session">
    <property name="i" value="0"></property>
</bean>
Run Code Online (Sandbox Code Playgroud)

这很好用.

我现在想为这个类创建一个单元测试

@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());

    }

}
Run Code Online (Sandbox Code Playgroud)

如果我像这样运行它,我会得到的

SEVERE: Caught exception while allowing …
Run Code Online (Sandbox Code Playgroud)

java spring-mvc junit4

8
推荐指数
1
解决办法
1万
查看次数

标签 统计

java ×1

junit4 ×1

spring-mvc ×1