Hi Stripes Users, Currently I want to test an action bean. It gets a Spring bean (service class) injected via the @SpringBean annotation. Now I want this service to be mocked with Mockito. My current solution works like this:
I have an interface "LoginService" and a real implementation "LoginServiceImpl" annotated with @Service annotation. In my test classpath, I also have an implementation called MockitoLoginService which uses the decorator pattern. It has the following static (!) field: public static final LoginService mock = mock(LoginService.class); The login method derived from the interface looks like this: public User login(String username, String password) throws LoginException { return mock.login(username, password); } This enables me to configure the mocked service from my unit test by configuring the "mock" constant: when(MockitoLoginService.mock.login(anyString(), anyString())).thenReturn(new User()); Why did I make the field static? Well, when executing LoginActionBean with MockRoundtrip, a new Spring context is created. If "mock" was an instance field, it would also be newly created for the action bean, so I cannot configure the mock object of the MockitoLoginService that was injected into my unit test. By using a static field, this problem is solved. Now the bad thing: By using this static field, I lose all thread safety that the real implementation provides. Test cases must be executed one after another - not really what I want. What I really want is that Stripes uses the same Spring context as the unit tests, or vice versa. I already use some Spring test annotations and implement ApplicationContextAware in my unit tests. This way, I have access to the used Spring context. But when configuring and starting the StripesFilter, a new WebApplicationContext is created and used. This one exists side by side to the context used by the unit tests. Has anyone an idea how to let use Stripes the same Spring context that us ised while executing the unit tests? Best regards, Marcus ------------------------------------------------------------------------------ Index, Search & Analyze Logs and other IT data in Real-Time with Splunk Collect, index and harness all the fast moving IT data generated by your applications, servers and devices whether physical, virtual or in the cloud. Deliver compliance at lower cost and gain new business insights. Free Software Download: http://p.sf.net/sfu/splunk-dev2dev _______________________________________________ Stripes-users mailing list Stripes-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/stripes-users