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]

Reply via email to