Note that there are some explanatory texts on larger screens.

plurals
  1. POrequest scoped beans in spring testing
    primarykey
    data
    text
    <p>I would like to make use of request scoped beans in my app. I use JUnit4 for testing. If I try to create one in a test like this:</p> <pre><code>@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:spring/TestScopedBeans-context.xml" }) public class TestScopedBeans { protected final static Logger logger = Logger .getLogger(TestScopedBeans.class); @Resource private Object tObj; @Test public void testBean() { logger.debug(tObj); } @Test public void testBean2() { logger.debug(tObj); } </code></pre> <p>With the following bean definition:</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"&gt; &lt;bean class="java.lang.Object" id="tObj" scope="request" /&gt; &lt;/beans&gt; </code></pre> <p>And I get:</p> <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gov.nasa.arc.cx.sor.query.TestScopedBeans': Injection of resource fields failed; nested exception is java.lang.IllegalStateException: No Scope registered for scope 'request' &lt;...SNIP...&gt; Caused by: java.lang.IllegalStateException: No Scope registered for scope 'request' </code></pre> <p>So I found this blog that seemed helpful: <a href="http://www.javathinking.com/2009/06/no-scope-registered-for-scope-request_5.html" rel="noreferrer">http://www.javathinking.com/2009/06/no-scope-registered-for-scope-request_5.html</a></p> <p>But I noticed he uses <a href="http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/test/AbstractDependencyInjectionSpringContextTests.html" rel="noreferrer">AbstractDependencyInjectionSpringContextTests</a> which seems to be deprecated in Spring 3.0. I use Spring 2.5 at this time, but thought it shouldn't be too hard to switch this method to use AbstractJUnit4SpringContextTests as the docs suggest (ok the docs link to the 3.8 version but I'm using 4.4). So I change the test to extend AbstractJUnit4SpringContextTests... same message. Same problem. And now the prepareTestInstance() method I want to override is not defined. OK, maybe I'll put those registerScope calls somewhere else... So I read more about <a href="http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/test/context/TestExecutionListeners.html" rel="noreferrer">TestExecutionListeners</a> and think that would be better since I don't want to have to inherit the spring package structure. So I changed my Test to:</p> <pre><code>@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:spring/TestScopedBeans-context.xml" }) @TestExecutionListeners({}) public class TestScopedBeans { </code></pre> <p>expecting I would have to create a custom listener but I when I ran it. It works! Great, but why? I don't see where any of the stock listeners are registering request scope or session scope, and why would they? there's nothing to say I want that yet, this might not be a Test for Spring MVC code...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload