Skip to content

Instantly share code, notes, and snippets.

@e2kaneko
Created November 5, 2012 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e2kaneko/4016473 to your computer and use it in GitHub Desktop.
Save e2kaneko/4016473 to your computer and use it in GitHub Desktop.
SpringFrameworkTest - JUnitでScopeの利用
java.lang.IllegalStateException: No Scope registered for scope 'session'
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
(略)
java.lang.NullPointerException
at hoge.hoge.hoge.HogeTest.hogeTest(HogeTest.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
(略)
package hoge.hoge.hoge;
import java.io.Serializable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@SuppressWarnings("serial")
@Component
@Scope("session")
public class Hoge implements Serializable {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package hoge.hoge.hoge;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "file:WebContent/WEB-INF/applicationContext-unittest.xml", "file:WebContent/WEB-INF/web-servlet.xml" })
public class HogeTest {
@Autowired
private Hoge hoge;
@Test
public void hogeTest() {
hoge.setName("hoge");
assertEquals("hoge", hoge.getName());
}
}
package hoge.hoge.hoge;
import static org.junit.Assert.assertEquals;
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.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.Scope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.support.SimpleThreadScope;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "file:WebContent/WEB-INF/applicationContext-unittest.xml", "file:WebContent/WEB-INF/web-servlet.xml" })
public class HogeTest {
@Autowired
private Hoge hoge;
@Autowired
private GenericApplicationContext applicationContext;
@Before
public void init() {
ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
Scope sessionScope = new SimpleThreadScope();
beanFactory.registerScope("session", sessionScope); // ←これ。
// Scope requestScope = new SimpleThreadScope();
// beanFactory.registerScope("request", requestScope);
}
@Test
public void hogeTest() {
hoge.setName("hoge");
assertEquals("hoge", hoge.getName());
}
}
package hoge.hoge.hoge;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "file:WebContent/WEB-INF/applicationContext-unittest.xml", "file:WebContent/WEB-INF/web-servlet.xml" })
@TestExecutionListeners({ WebContextTestExecutionListener.class })
public class HogeTest {
@Autowired
private Hoge hoge;
@Test
public void hogeTest() {
hoge.setName("hoge");
assertEquals("hoge", hoge.getName());
}
}
package hoge.hoge.hoge;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "file:WebContent/WEB-INF/applicationContext-unittest.xml", "file:WebContent/WEB-INF/web-servlet.xml" })
@TestExecutionListeners({ WebContextTestExecutionListener.class, DependencyInjectionTestExecutionListener.class })
public class HogeTest {
@Autowired
private Hoge hoge;
@Test
public void hogeTest() {
hoge.setName("hoge");
assertEquals("hoge", hoge.getName());
}
}
package hoge.hoge.hoge;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.Scope;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.support.SimpleThreadScope;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.support.AbstractTestExecutionListener;
public class WebContextTestExecutionListener extends AbstractTestExecutionListener {
@Override
public void prepareTestInstance(TestContext testContext) throws Exception {
GenericApplicationContext context = (GenericApplicationContext)testContext.getApplicationContext();
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
Scope requestScope = new SimpleThreadScope();
beanFactory.registerScope("request", requestScope);
Scope sessionScope = new SimpleThreadScope();
beanFactory.registerScope("session", sessionScope);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment