2012年11月5日月曜日

SpringFrameworkのテストでSessionスコープを使うの巻

初歩的なメモ

SpringFrameworkでこう書いて、
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;
}
}
view raw Hoge.java hosted with ❤ by GitHub

テストこう書いて、
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());
}
}
view raw HogeTest.java hosted with ❤ by GitHub

実行すると、こうなる
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)
(略)
view raw ErrorConsole1 hosted with ❤ by GitHub

ねーぞ、と。
なので、テスト用にSessionスコープをつくってあげます(Requestとか他のスコープも一緒)
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());
}
}
view raw HogeTest2.java hosted with ❤ by GitHub

実行


とりあえずとおった。
複数のテストの場合、個別のテストに書くの微妙なのでリスナーを作りましょう。
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);
}
}

で、ほげテスト君をもとどおりにしてアノテーションでリスナー指定
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());
}
}
view raw HogeTest3.java hosted with ❤ by GitHub

チクショーDIできてねー(>_<)
java.lang.NullPointerException
at hoge.hoge.hoge.HogeTest.hogeTest(HogeTest.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
(略)
view raw ErrorConsole2 hosted with ❤ by GitHub

どうやらこうらしい。
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());
}
}
view raw HogeTest4.java hosted with ❤ by GitHub

さいごとのトコちょっと意味不明。
なので、(気が向いたら)つづく。

かねこ m)Д`)

0 件のコメント:

コメントを投稿