SpringFrameworkでこう書いて、
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
テストこう書いて、
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
実行すると、こうなる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
(略) |
ねーぞ、と。
なので、テスト用にSessionスコープをつくってあげます(Requestとか他のスコープも一緒)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
実行

とりあえずとおった。
複数のテストの場合、個別のテストに書くの微妙なのでリスナーを作りましょう。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
で、ほげテスト君をもとどおりにしてアノテーションでリスナー指定
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
チクショーDIできてねー(>_<)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
java.lang.NullPointerException | |
at hoge.hoge.hoge.HogeTest.hogeTest(HogeTest.java:24) | |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
(略) |
どうやらこうらしい。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
さいごとのトコちょっと意味不明。
なので、(気が向いたら)つづく。
かねこ m)Д`)
0 件のコメント:
コメントを投稿