Re: Wicket Unit Test: No WebApplicationContext found: no ContextLoaderListener registered?
On Sat, Apr 26, 2008 at 1:40 AM, Nino Saturnino Martinez Vazquez Wael [EMAIL PROTECTED] wrote: James Carman wrote: Why are you using the Spring injector to inject your dependencies? Can you not manually inject your dependencies? Adding stuff manually to a Spring context and then having the Spring injector inject it doesn't really test what's going to be going on in production anyway. If it were me, I'd provide setters for my dependencies and just set them that way with my mock objects. It can be pretty use fully for several purposes, one could be using mock object's instead (like easymock). I understand the importance of having the stuff injected. My question is whether or not you should be using Spring to inject them during unit tests. You don't use Spring to inject stuff during other types of unit tests, so why should you use it for Wicket Pages/Components? Why can't you just do this: public class MyWebPage extends WebPage { @SpringBean private MyService myService; public void setMyService(MyService myService) { this.myService = myService; } } public class TestMyWebPage { @Test public void testPageRendersProperly() { WicketTester tester = new WicketTester(); MyWebPage page = new MyWebPage(); MyService myService = ...; // Create mock object here using jmock/easymock! page.setMyService(myService); tester.startPage(myPage); ... } } Is that not an acceptable way to unit test the pages? To me, this is much simpler. This is how you test normal classes that are used inside a Spring context. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Wicket Unit Test: No WebApplicationContext found: no ContextLoaderListener registered?
James Carman wrote: On Sat, Apr 26, 2008 at 1:40 AM, Nino Saturnino Martinez Vazquez Wael [EMAIL PROTECTED] wrote: James Carman wrote: Why are you using the Spring injector to inject your dependencies? Can you not manually inject your dependencies? Adding stuff manually to a Spring context and then having the Spring injector inject it doesn't really test what's going to be going on in production anyway. If it were me, I'd provide setters for my dependencies and just set them that way with my mock objects. It can be pretty use fully for several purposes, one could be using mock object's instead (like easymock). I understand the importance of having the stuff injected. My question is whether or not you should be using Spring to inject them during unit tests. You don't use Spring to inject stuff during other types of unit tests, so why should you use it for Wicket Pages/Components? Why can't you just do this: I agree you could do it that way, however Im not sure how wicket spring reacts to not having it's mock context. Im using the wicket tester in a mixed mode, a little for unit test and a little for integration test, so for me it's very nice to be working against either easy mock or the test spring beans. public class MyWebPage extends WebPage { @SpringBean private MyService myService; public void setMyService(MyService myService) { this.myService = myService; } } public class TestMyWebPage { @Test public void testPageRendersProperly() { WicketTester tester = new WicketTester(); MyWebPage page = new MyWebPage(); MyService myService = ...; // Create mock object here using jmock/easymock! page.setMyService(myService); tester.startPage(myPage); ... } } Is that not an acceptable way to unit test the pages? To me, this is much simpler. This is how you test normal classes that are used inside a Spring context. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- -Wicket for love Nino Martinez Wael Java Specialist @ Jayway DK http://www.jayway.dk +45 2936 7684 - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Wicket Unit Test: No WebApplicationContext found: no ContextLoaderListener registered?
I solved it! I modified the mock application class and it worked as I expected public class MockWicketApplication extends WicketApplication { private AnnotApplicationContextMock mockContext; @Override protected void internalInit() { mockContext = new AnnotApplicationContextMock(); setApplicationContext(mockContext); super.internalInit(); } @Override protected void configureSpringInjection() { addComponentInstantiationListener(new SpringComponentInjector(this, mockContext)); } public AnnotApplicationContextMock getMockContext() { return mockContext; } } Thanks for all the answers! I had to set the mock application context to the attribute applicationContext inherited from the SpringWebApplication class, because at org.apache.wicket.spring.SpringWebApplication.internalInit(SpringWebApplication.java:77) if the attribute is null searchs for the ContextLoaderListener (configured in the web.xml) and of course is not there because we are running a test. Answering to James and Nino, I prefer to have the beans injected by spring because is cleaner and more understandable. Setting manually the beans is too verbose and the way the tests are coded can be completely different if many programmers are working on the same project. The intention of the mechanism I'm trying to implement is just to test the web layer. I will mock all the services in the same way and reuse the mocks along all the tests. For different layers I will follow different strategies depending of the requirements i have. But definitely i would never inject manually a bean, although it works perfectly. Even in the tests i will use the spring injection engine! Cheers! JG On 26/04/2008, Nino Saturnino Martinez Vazquez Wael [EMAIL PROTECTED] wrote: James Carman wrote: On Sat, Apr 26, 2008 at 1:40 AM, Nino Saturnino Martinez Vazquez Wael [EMAIL PROTECTED] wrote: James Carman wrote: Why are you using the Spring injector to inject your dependencies? Can you not manually inject your dependencies? Adding stuff manually to a Spring context and then having the Spring injector inject it doesn't really test what's going to be going on in production anyway. If it were me, I'd provide setters for my dependencies and just set them that way with my mock objects. It can be pretty use fully for several purposes, one could be using mock object's instead (like easymock). I understand the importance of having the stuff injected. My question is whether or not you should be using Spring to inject them during unit tests. You don't use Spring to inject stuff during other types of unit tests, so why should you use it for Wicket Pages/Components? Why can't you just do this: I agree you could do it that way, however Im not sure how wicket spring reacts to not having it's mock context. Im using the wicket tester in a mixed mode, a little for unit test and a little for integration test, so for me it's very nice to be working against either easy mock or the test spring beans. public class MyWebPage extends WebPage { @SpringBean private MyService myService; public void setMyService(MyService myService) { this.myService = myService; } } public class TestMyWebPage { @Test public void testPageRendersProperly() { WicketTester tester = new WicketTester(); MyWebPage page = new MyWebPage(); MyService myService = ...; // Create mock object here using jmock/easymock! page.setMyService(myService); tester.startPage(myPage); ... } } Is that not an acceptable way to unit test the pages? To me, this is much simpler. This is how you test normal classes that are used inside a Spring context. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- -Wicket for love Nino Martinez Wael Java Specialist @ Jayway DK http://www.jayway.dk +45 2936 7684 - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- Jorge Gallardo [EMAIL PROTECTED] [EMAIL PROTECTED]
Re: Wicket Unit Test: No WebApplicationContext found: no ContextLoaderListener registered?
Why are you using the Spring injector to inject your dependencies? Can you not manually inject your dependencies? Adding stuff manually to a Spring context and then having the Spring injector inject it doesn't really test what's going to be going on in production anyway. If it were me, I'd provide setters for my dependencies and just set them that way with my mock objects. On Fri, Apr 25, 2008 at 8:42 PM, Jorge Gallardo [EMAIL PROTECTED] wrote: Hi everyone, I'm setting up an app with wicket 1.3.3 I have my Application class defined this way, to use Spring injection with annotations public class WicketApplication extends SpringWebApplication { /** * Constructor */ public WicketApplication() { } @Override protected void init() { super.init(); configureSpringInjection(); // configure bookmarkable pages mountBookmarkablePage(/login, LoginPage.class); mountBookmarkablePage(/home, HomePage.class); } protected void configureSpringInjection() { addComponentInstantiationListener(new SpringComponentInjector(this)); } @Override public Class? extends Page getHomePage() { return HomePage.class; } @Override public Session newSession(Request request, Response response) { return new WicketSession(request); } } Then I subclassed it for testing: public class MockWicketApplication extends WicketApplication { private AnnotApplicationContextMock mockContext; @Override protected void configureSpringInjection() { mockContext = new AnnotApplicationContextMock(); addComponentInstantiationListener(new SpringComponentInjector(this, mockContext)); } public AnnotApplicationContextMock getMockContext() { return mockContext; } } And defined an abstract Test class: public abstract class BaseWicketTest extends TestCase { protected WicketTester tester; protected AnnotApplicationContextMock mockContext; protected void setUp() throws Exception { MockWicketApplication webapp = new MockWicketApplication(); tester = new WicketTester(webapp); mockContext = ((MockWicketApplication) tester.getApplication()) .getMockContext(); } } and finally the Test itself: public class TestHomePage extends BaseWicketTest { public void testRenderMyPage() { mockContext.putBean(userAuthenticationSrv, new MockUserAuthenticationSrv()); //start and render the test page tester.startPage(HomePage.class); //assert rendered page class tester.assertRenderedPage(HomePage.class); //assert rendered label component tester.assertLabel(message, Some text); } } but then when i try to run the test case the result is the following stacktrace: java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered? at org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:95) at org.apache.wicket.spring.SpringWebApplication.internalInit(SpringWebApplication.java:77) at org.apache.wicket.protocol.http.WicketFilter.init(WicketFilter.java:523) at org.apache.wicket.protocol.http.MockWebApplication.init(MockWebApplication.java:151) at org.apache.wicket.util.tester.BaseWicketTester.init(BaseWicketTester.java:205) at org.apache.wicket.util.tester.WicketTester.init(WicketTester.java:308) at org.apache.wicket.util.tester.WicketTester.init(WicketTester.java:291) at com.globant.web.page.BaseWicketTest.setUp(BaseWicketTest.java:19) at junit.framework.TestCase.runBare(TestCase.java:128) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:120) at junit.framework.TestSuite.runTest(TestSuite.java:230) at junit.framework.TestSuite.run(TestSuite.java:225) at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196) I must be making a mistake somewhere, but i cant get it. I checked the examples, the wiki, everywhere. Any idea? Any thoughts? Will be
Re: Wicket Unit Test: No WebApplicationContext found: no ContextLoaderListener registered?
James Carman wrote: Why are you using the Spring injector to inject your dependencies? Can you not manually inject your dependencies? Adding stuff manually to a Spring context and then having the Spring injector inject it doesn't really test what's going to be going on in production anyway. If it were me, I'd provide setters for my dependencies and just set them that way with my mock objects. It can be pretty use fully for several purposes, one could be using mock object's instead (like easymock). On Fri, Apr 25, 2008 at 8:42 PM, Jorge Gallardo [EMAIL PROTECTED] wrote: Hi everyone, I'm setting up an app with wicket 1.3.3 I have my Application class defined this way, to use Spring injection with annotations public class WicketApplication extends SpringWebApplication { /** * Constructor */ public WicketApplication() { } @Override protected void init() { super.init(); configureSpringInjection(); // configure bookmarkable pages mountBookmarkablePage(/login, LoginPage.class); mountBookmarkablePage(/home, HomePage.class); } protected void configureSpringInjection() { addComponentInstantiationListener(new SpringComponentInjector(this)); } @Override public Class? extends Page getHomePage() { return HomePage.class; } @Override public Session newSession(Request request, Response response) { return new WicketSession(request); } } Then I subclassed it for testing: public class MockWicketApplication extends WicketApplication { private AnnotApplicationContextMock mockContext; @Override protected void configureSpringInjection() { mockContext = new AnnotApplicationContextMock(); addComponentInstantiationListener(new SpringComponentInjector(this, mockContext)); } public AnnotApplicationContextMock getMockContext() { return mockContext; } } And defined an abstract Test class: public abstract class BaseWicketTest extends TestCase { protected WicketTester tester; protected AnnotApplicationContextMock mockContext; protected void setUp() throws Exception { MockWicketApplication webapp = new MockWicketApplication(); tester = new WicketTester(webapp); mockContext = ((MockWicketApplication) tester.getApplication()) .getMockContext(); } } and finally the Test itself: public class TestHomePage extends BaseWicketTest { public void testRenderMyPage() { mockContext.putBean(userAuthenticationSrv, new MockUserAuthenticationSrv()); //start and render the test page tester.startPage(HomePage.class); //assert rendered page class tester.assertRenderedPage(HomePage.class); //assert rendered label component tester.assertLabel(message, Some text); } } but then when i try to run the test case the result is the following stacktrace: java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered? at org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:95) at org.apache.wicket.spring.SpringWebApplication.internalInit(SpringWebApplication.java:77) at org.apache.wicket.protocol.http.WicketFilter.init(WicketFilter.java:523) at org.apache.wicket.protocol.http.MockWebApplication.init(MockWebApplication.java:151) at org.apache.wicket.util.tester.BaseWicketTester.init(BaseWicketTester.java:205) at org.apache.wicket.util.tester.WicketTester.init(WicketTester.java:308) at org.apache.wicket.util.tester.WicketTester.init(WicketTester.java:291) at com.globant.web.page.BaseWicketTest.setUp(BaseWicketTest.java:19) at junit.framework.TestCase.runBare(TestCase.java:128) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:120) at junit.framework.TestSuite.runTest(TestSuite.java:230) at junit.framework.TestSuite.run(TestSuite.java:225) at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196) I must be making a mistake somewhere, but i cant get it. I checked the examples, the wiki, everywhere. Any idea? Any
Re: Wicket Unit Test: No WebApplicationContext found: no ContextLoaderListener registered?
I think you also need to put your beans in there aswell: ApplicationContext appcxt = new ClassPathXmlApplicationContext( applicationContext.xml); generalDao = (GeneralDao) appcxt.getBean(GeneralDao); // 2. setup mock injection environment AnnotApplicationContextMock appctx = new AnnotApplicationContextMock(); appctx.putBean(GeneralDao, generalDao); WicketApplication wicketPersistanceApplication = new WicketApplication(); wicketPersistanceApplication .setSpringComponentInjector(new SpringComponentInjector( wicketPersistanceApplication, appctx)); wicketTester = new WicketTester(wicketPersistanceApplication); // or this way: protected IAllInOneDao getDao() { allInOneDaoCtrl = EasyMock.createControl(); return allInOneDaoCtrl.createMock(IAllInOneDao.class); } @Override protected void setUp() throws Exception { super.setUp(); allInOneDao = getDao(); // 2. setup mock injection environment AnnotApplicationContextMock appctx = new AnnotApplicationContextMock(); appctx.putBean(allInOneDao, allInOneDao); wicketTester = new WicketTester(WicketApplication.class); WebApplication app = wicketTester.getApplication(); app.addComponentInstantiationListener(new SpringComponentInjector(app, appctx)); allInOneDaoCtrl.reset(); } Check out the blog tutorial(uses easymock to test in someplaces) or wicket persistence template(just uses injected beans): http://cwiki.apache.org/WICKET/blog-tutorial.html https://wicket-stuff.svn.sourceforge.net/svnroot/wicket-stuff/trunk/wicket-persistence-template Jorge Gallardo wrote: Hi everyone, I'm setting up an app with wicket 1.3.3 I have my Application class defined this way, to use Spring injection with annotations public class WicketApplication extends SpringWebApplication { /** * Constructor */ public WicketApplication() { } @Override protected void init() { super.init(); configureSpringInjection(); // configure bookmarkable pages mountBookmarkablePage(/login, LoginPage.class); mountBookmarkablePage(/home, HomePage.class); } protected void configureSpringInjection() { addComponentInstantiationListener(new SpringComponentInjector(this)); } @Override public Class? extends Page getHomePage() { return HomePage.class; } @Override public Session newSession(Request request, Response response) { return new WicketSession(request); } } Then I subclassed it for testing: public class MockWicketApplication extends WicketApplication { private AnnotApplicationContextMock mockContext; @Override protected void configureSpringInjection() { mockContext = new AnnotApplicationContextMock(); addComponentInstantiationListener(new SpringComponentInjector(this, mockContext)); } public AnnotApplicationContextMock getMockContext() { return mockContext; } } And defined an abstract Test class: public abstract class BaseWicketTest extends TestCase { protected WicketTester tester; protected AnnotApplicationContextMock mockContext; protected void setUp() throws Exception { MockWicketApplication webapp = new MockWicketApplication(); tester = new WicketTester(webapp); mockContext = ((MockWicketApplication) tester.getApplication()) .getMockContext(); } } and finally the Test itself: public class TestHomePage extends BaseWicketTest { public void testRenderMyPage() { mockContext.putBean(userAuthenticationSrv, new MockUserAuthenticationSrv()); //start and render the test page tester.startPage(HomePage.class); //assert rendered page class tester.assertRenderedPage(HomePage.class); //assert rendered label component tester.assertLabel(message, Some text); } } but then when i try to run the test case the result is the following stacktrace: java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered? at org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:95) at org.apache.wicket.spring.SpringWebApplication.internalInit(SpringWebApplication.java:77) at org.apache.wicket.protocol.http.WicketFilter.init(WicketFilter.java:523) at org.apache.wicket.protocol.http.MockWebApplication.init(MockWebApplication.java:151) at org.apache.wicket.util.tester.BaseWicketTester.init(BaseWicketTester.java:205) at org.apache.wicket.util.tester.WicketTester.init(WicketTester.java:308) at org.apache.wicket.util.tester.WicketTester.init(WicketTester.java:291) at