You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Jochen Mader <pf...@gmail.com> on 2010/01/26 11:48:57 UTC
Wicket, Spring 3 and UnitTesting
Just figured out how to do UnitTesting with Spring 3 and Wicket.
Spring 3 introduced a check to see if a given context was a
WebApplicationContext. That means ApplicationContextMock is not suitable for
testing (giving the infamous " No WebApplicationContext found: no
ContextLoaderListener registered?" message).
I simply extended the class and added WebApplicationContext interface.
The following example shows how to get it going (I hope the code doesn't get
messed up):
public class TestHomePage extends TestCase {
private WicketTester tester;
@Override
public void setUp() {
final WebApplicationContextMock appctx = new WebApplicationContextMock();
final ServiceOfDoom mock = createMock(ServiceOfDoom.class);
expect(mock.getIt()).andReturn("whups").anyTimes();
replay(mock);
tester = new WicketTester(new WicketApplication()) {
@Override
public ServletContext newServletContext(String path) {
MockServletContext servletContext = (MockServletContext) super
.newServletContext(path);
appctx.setServletContext(servletContext);
appctx.putBean("scratchy", mock);
servletContext
.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
appctx);
return servletContext;
}
};
tester.getApplication().addComponentInstantiationListener(
new SpringComponentInjector(tester.getApplication(), appctx,
false));
}
public void testRenderMyPage() {
// 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",
"whups");
}
private class WebApplicationContextMock extends ApplicationContextMock
implements WebApplicationContext {
private ServletContext servletContext;
public <T> T getBean(Class<T> requiredType) throws BeansException {
// TODO Auto-generated method stub
return null;
}
public <A extends Annotation> A findAnnotationOnBean(String beanName,
Class<A> annotationType) {
// TODO Auto-generated method stub
return null;
}
public Map<String, Object> getBeansWithAnnotation(
Class<? extends Annotation> annotationType)
throws BeansException {
// TODO Auto-generated method stub
return null;
}
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
public ServletContext getServletContext() {
return null;
}
}
}
Re: Wicket, Spring 3 and UnitTesting
Posted by Jochen Mader <pf...@gmail.com>.
Oh man, you are right.
My eyes got a little selective on that line :)
Re: Wicket, Spring 3 and UnitTesting
Posted by mbrictson <ma...@55minutes.com>.
The API is bit confusing: registerSingleton() on StaticWebApplicationContext
takes a class, but registerSingleton() on ConfigurableListableBeanFactory
takes a bean. That is why I first call getBeanFactory() in my example.
ctx.getBeanFactory().registerSingleton(...)
I use StaticWebApplicationContext for my unit testing, so I am pretty sure
it works. :)
Here is the registerSingleton signature:
http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/beans/factory/config/SingletonBeanRegistry.html#registerSingleton%28java.lang.String,%20java.lang.Object%29
Jochen Mader-2 wrote:
>
> Sorry for my late answer.
> StaticWebApplicationContext doesn't cut it for me.
> Your example contains a small mistake:
> Inserting the mock-object won't work as registerSingleton expects to get a
> Class.
> As I want to create mock objects with EasyMock there are two approaches
> (as
> far as I know).
> The one I have shown before, involving the creation of the custom
> ApplicationContextMock or using easy mock inside a testing-spring.xml.
>
> CU
>
> Jochen
>
>
--
View this message in context: http://old.nabble.com/Wicket%2C-Spring-3-and-UnitTesting-tp27320784p27358526.html
Sent from the Wicket - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Wicket, Spring 3 and UnitTesting
Posted by Jochen Mader <pf...@gmail.com>.
Sorry for my late answer.
StaticWebApplicationContext doesn't cut it for me.
Your example contains a small mistake:
Inserting the mock-object won't work as registerSingleton expects to get a
Class.
As I want to create mock objects with EasyMock there are two approaches (as
far as I know).
The one I have shown before, involving the creation of the custom
ApplicationContextMock or using easy mock inside a testing-spring.xml.
CU
Jochen
Re: Wicket, Spring 3 and UnitTesting
Posted by mbrictson <ma...@55minutes.com>.
Why not use Spring's StaticWebApplicationContext?
StaticWebApplicationContext ctx = new StaticWebApplicationContext();
ctx.getBeanFactory().registerSingleton("serviceOfDoom", mock);
http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/web/context/support/StaticWebApplicationContext.html
Jochen Mader-2 wrote:
>
> Just figured out how to do UnitTesting with Spring 3 and Wicket.
> Spring 3 introduced a check to see if a given context was a
> WebApplicationContext. That means ApplicationContextMock is not suitable
> for
> testing (giving the infamous " No WebApplicationContext found: no
> ContextLoaderListener registered?" message).
> I simply extended the class and added WebApplicationContext interface.
>
> The following example shows how to get it going (I hope the code doesn't
> get
> messed up):
>
>
> public class TestHomePage extends TestCase {
>
> private WicketTester tester;
>
>
> @Override
>
> public void setUp() {
>
> final WebApplicationContextMock appctx = new WebApplicationContextMock();
>
>
> final ServiceOfDoom mock = createMock(ServiceOfDoom.class);
>
> expect(mock.getIt()).andReturn("whups").anyTimes();
>
> replay(mock);
>
> tester = new WicketTester(new WicketApplication()) {
>
> @Override
>
> public ServletContext newServletContext(String path) {
>
> MockServletContext servletContext = (MockServletContext) super
>
> .newServletContext(path);
>
> appctx.setServletContext(servletContext);
>
> appctx.putBean("scratchy", mock);
>
> servletContext
>
> .setAttribute(
>
> WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
>
> appctx);
>
> return servletContext;
>
> }
>
> };
>
> tester.getApplication().addComponentInstantiationListener(
>
> new SpringComponentInjector(tester.getApplication(), appctx,
>
> false));
>
> }
>
>
> public void testRenderMyPage() {
>
> // 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",
>
> "whups");
>
> }
>
>
> private class WebApplicationContextMock extends ApplicationContextMock
>
> implements WebApplicationContext {
>
> private ServletContext servletContext;
>
>
> public <T> T getBean(Class<T> requiredType) throws BeansException {
>
> // TODO Auto-generated method stub
>
> return null;
>
> }
>
>
> public A findAnnotationOnBean(String beanName,
>
> Class annotationType) {
>
> // TODO Auto-generated method stub
>
> return null;
>
> }
>
>
> public Map<String, Object> getBeansWithAnnotation(
>
> Class<? extends Annotation> annotationType)
>
> throws BeansException {
>
> // TODO Auto-generated method stub
>
> return null;
>
> }
>
>
> public void setServletContext(ServletContext servletContext) {
>
> this.servletContext = servletContext;
>
> }
>
>
> public ServletContext getServletContext() {
>
> return null;
>
> }
>
>
> }
>
> }
>
>
--
View this message in context: http://old.nabble.com/Wicket%2C-Spring-3-and-UnitTesting-tp27320784p27332886.html
Sent from the Wicket - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Wicket, Spring 3 and UnitTesting
Posted by Martin Grigorov <mc...@e-card.bg>.
Please add this to http://cwiki.apache.org/WICKET/spring.html
On Tue, 2010-01-26 at 11:48 +0100, Jochen Mader wrote:
> Just figured out how to do UnitTesting with Spring 3 and Wicket.
> Spring 3 introduced a check to see if a given context was a
> WebApplicationContext. That means ApplicationContextMock is not suitable for
> testing (giving the infamous " No WebApplicationContext found: no
> ContextLoaderListener registered?" message).
> I simply extended the class and added WebApplicationContext interface.
>
> The following example shows how to get it going (I hope the code doesn't get
> messed up):
>
>
> public class TestHomePage extends TestCase {
>
> private WicketTester tester;
>
>
> @Override
>
> public void setUp() {
>
> final WebApplicationContextMock appctx = new WebApplicationContextMock();
>
>
> final ServiceOfDoom mock = createMock(ServiceOfDoom.class);
>
> expect(mock.getIt()).andReturn("whups").anyTimes();
>
> replay(mock);
>
> tester = new WicketTester(new WicketApplication()) {
>
> @Override
>
> public ServletContext newServletContext(String path) {
>
> MockServletContext servletContext = (MockServletContext) super
>
> .newServletContext(path);
>
> appctx.setServletContext(servletContext);
>
> appctx.putBean("scratchy", mock);
>
> servletContext
>
> .setAttribute(
>
> WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
>
> appctx);
>
> return servletContext;
>
> }
>
> };
>
> tester.getApplication().addComponentInstantiationListener(
>
> new SpringComponentInjector(tester.getApplication(), appctx,
>
> false));
>
> }
>
>
> public void testRenderMyPage() {
>
> // 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",
>
> "whups");
>
> }
>
>
> private class WebApplicationContextMock extends ApplicationContextMock
>
> implements WebApplicationContext {
>
> private ServletContext servletContext;
>
>
> public <T> T getBean(Class<T> requiredType) throws BeansException {
>
> // TODO Auto-generated method stub
>
> return null;
>
> }
>
>
> public <A extends Annotation> A findAnnotationOnBean(String beanName,
>
> Class<A> annotationType) {
>
> // TODO Auto-generated method stub
>
> return null;
>
> }
>
>
> public Map<String, Object> getBeansWithAnnotation(
>
> Class<? extends Annotation> annotationType)
>
> throws BeansException {
>
> // TODO Auto-generated method stub
>
> return null;
>
> }
>
>
> public void setServletContext(ServletContext servletContext) {
>
> this.servletContext = servletContext;
>
> }
>
>
> public ServletContext getServletContext() {
>
> return null;
>
> }
>
>
> }
>
> }
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Wicket, Spring 3 and UnitTesting
Posted by Kent Tong <ke...@cpttm.org.mo>.
Jochen Mader-2 wrote:
>
> Just figured out how to do UnitTesting with Spring 3 and Wicket.
>
An alternative is to use http://wicketpagetest.sourceforge.net. It works
fine with Spring 3.0 without changing any of your code.
-----
--
Kent Tong
Better way to unit test Wicket pages (http://wicketpagetest.sourceforge.net)
Books on CXF, Axis2, Wicket, JSF (http://agileskills2.org)
--
View this message in context: http://old.nabble.com/Wicket%2C-Spring-3-and-UnitTesting-tp27320784p27380973.html
Sent from the Wicket - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org