Hi Marcus,

Here's how I did in my recent project.
In a @BeforeMethod of my base test class, spring's application
context is created with mocks and added to MockServletContext.

@BeforeMethod
public void setupSpringContext() {
  GenericWebApplicationContext springContext =
    new GenericWebApplicationContext();
  context.setAttribute(
    WebApplicationContext
      .ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
    springContext);

  yourService = mock(YourService.class);
  prepareSpringBean("yourService", bureauService);
  personService = mock(AnotherService.class);
  anotherSpringBean("anotherService", personService);
  scripService = mock(ScripService.class);
  ...
}

protected void prepareSpringBean(String name, Object bean) {
  GenericWebApplicationContext springContext =
    (GenericWebApplicationContext)context
      .getAttribute(WebApplicationContext
        .ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
  springContext.getBeanFactory()
    .registerSingleton(name, bean);
}

--
And in each test class, mocks are configured before creating a
MockRoundtrip.

@Test
public void testSomething() throws Exception {
  // define behavior of services used in the test.
  when(yourService.doServiceMethod())
    .thenReturn(x);

  trip = new MockRoundtrip(context,
    OneOfYourActionBean.class);
  // add parameters
  trip.execute();

  // assertions
}

--
It's a little bit annoying that I have to prepare all the spring
beans manually.
If you find a easier way to do this, please let me know.

Hope this helps,
Iwao


on 09/11/21 6:19 M.C.S. said the following:
> Hi all,
> 
> until now I used MockRoundtrip for testing my ActionBeans. This went
> fine although my beans usually had some Spring-injected services. But as
> long as they were valid Spring beans, every worked fine.
> 
> Now while developing, I don't always have a concrete implementation of
> the services that I am planning to be injected in the ActionBeans, so I
> want the bean use a stub (created by Mockito) instead of a real class.
> But that seems to be a problem: I see no way to get the stub in the
> ActionBean because it is created on trip.execute().
> 
> Any idea how I can use my stub in the ActionBean when using MockRoundtrip?
> 
> Kind regards,
> Marcus

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Stripes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-users

Reply via email to