Re: [Stripes-users] Spring + Mockito
This is all very interesting.. In Rayures 2.0 I have a TestWithMockContainer base class that you can extend and just tell it the path to your web.xml file (could be different than your regular file if you wish, e.g. web-test.xml). It parses the file and wires up the Stripes mocks for you, including your Spring web application context. This not only solves the problem in this discussion but also saves you from manually setting up your Stripes mocks. Caveat is that I have it set up to work with TestNG, which has some differences in test class lifecycle. Not sure if it would work with JUnit. Other caveat is that I'm using (unreleased) Stripes 1.6 to use the ObjectFactory for Spring beans with standard Spring annotations, and to be able to inject beans in type converters, formatters, the action bean context, etc. Cheers, Freddy On Tue, 22 Feb 2011 23:12 +0900, "Iwao AVE!" wrote: > Hi Marcus, > > I was testing my action beans as Remi suggested, so it should work. > > Currently, I use a custom interceptor and a modified version of > SpringHelper to create/inject mock spring beans into an action bean > automatically. > As it's a little bit tricky to explain, I would create a simple > example project and upload it to somewhere if you are interested. > > Regards, > Iwao > > 2011/2/22 Marcus Kraßmann : > > Hi Remi, > > > > ctx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEX_ATTRIBUTE, > > springContext) > > > > I also had this idea some weeks ago. I cannot surely remember the reason > > why I dropped it, but I think that I was not able to "convert" the > > ApplicationContext used by JUnit to a WebApplicationContext that is needed > > for the whole servlet thing. So your approach fails IIRC. > > > > Maybe someday I will review the whole thing and find a way to make your > > approach work. That would be the best way: Using the "Spring test context" > > in Stripes. > > > > Best wishes, > > Marcus > > > > ----- Ursprüngliche Mail ----- > > Von: "VANKEISBELCK Remi" > > An: "Stripes Users List" > > CC: "Marcus Kraßmann" > > Gesendet: Dienstag, 22. Februar 2011 14:44:04 > > Betreff: Re: [Stripes-users] Spring + Mockito > > > > Btw, what about : > > > > // create the Spring context for our test > > ApplicationContext appCtx = createSpringAppCtx() > > // play with the mockito bean > > LoginService ls = (LoginService)appCtx.getBean(...) > > ... > > // create a mock servlet context, without the Spring context loader > > MockServletContext ctx = createMockServletContextWithoutSpring() > > // add Spring to the servlet context > > ctx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEX_ATTRIBUTE) > > // and now do the roundtrip > > MockRoundtrip trip = new MockRoundtrip(ctx, MyAction.class) > > trip.xyz() > > > > ? > > > > Cheers > > > > Remi > > > > > > 2011/2/22 VANKEISBELCK Remi < r...@rvkb.com > > > > > > > Yeah your "first attempt" makes much more sense than this static field :) > > > > One thing though, what do you mean by "injecting into JUnit" ? > > > > From what I understood, you want to configure the mock object *prior* to > > performing a mock round trip, don't you ? > > > > Cheers > > > > > > > > > > Remi > > > > > > 2011/2/22 Marcus Kraßmann < m...@syn-online.de > > > > > > > Hi Remi, > > > > Thanks for your reply. Actually I have to wrap it because MockRoundtrip > > uses another Spring ApplicationContext than JUnit itself. Fact is that if I > > inject LoginService into JUnit (which is configured to be a singleton > > instance of MockitoLoginService), then it is another instance than the one > > that is injected into my action bean. > > > > Surely I could write a simple mock object that behaves the way I want > > without Mockito. But by using Mockito, I can configure my test stubs very > > easily without writing much code. At least in theory. > > > > If that still does not help to clearify my problem: > > My first try was to declare a Mockito stub as Spring bean by declaring this > > in my applicationContext.xml: > > > > > scope="singleton"> > > > > > > > > This _should_ work fine, but is doesn't. The service is created twice, once > > when starting up my JUnit tests, and the second time when the StripesFilter > > is initialized in my test fixture. So if I c
Re: [Stripes-users] Spring + Mockito
Hi Iwao, Without a modified SpringHelper the suggested way fails with an IllegalStateException ("Context attribute is not of type WebApplicationContext"). I guess I know how you solved this issue, but IMHO that requires more Stripes related changes than my current way (a simple getBean method instead of the "magic" @Autowired annotation). But I will surely give it a try in the near future - two concurrent Spring contexts really smell bad :-) Thanks for your feedback. Best wishes, Marcus - Ursprüngliche Mail - Von: "Iwao AVE!" An: "Stripes Users List" Gesendet: Dienstag, 22. Februar 2011 15:12:16 Betreff: Re: [Stripes-users] Spring + Mockito Hi Marcus, I was testing my action beans as Remi suggested, so it should work. Currently, I use a custom interceptor and a modified version of SpringHelper to create/inject mock spring beans into an action bean automatically. As it's a little bit tricky to explain, I would create a simple example project and upload it to somewhere if you are interested. Regards, Iwao 2011/2/22 Marcus Kraßmann : > Hi Remi, > > ctx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEX_ATTRIBUTE, > springContext) > > I also had this idea some weeks ago. I cannot surely remember the reason why > I dropped it, but I think that I was not able to "convert" the > ApplicationContext used by JUnit to a WebApplicationContext that is needed > for the whole servlet thing. So your approach fails IIRC. > > Maybe someday I will review the whole thing and find a way to make your > approach work. That would be the best way: Using the "Spring test context" in > Stripes. > > Best wishes, > Marcus > > - Ursprüngliche Mail - > Von: "VANKEISBELCK Remi" > An: "Stripes Users List" > CC: "Marcus Kraßmann" > Gesendet: Dienstag, 22. Februar 2011 14:44:04 > Betreff: Re: [Stripes-users] Spring + Mockito > > Btw, what about : > > // create the Spring context for our test > ApplicationContext appCtx = createSpringAppCtx() > // play with the mockito bean > LoginService ls = (LoginService)appCtx.getBean(...) > ... > // create a mock servlet context, without the Spring context loader > MockServletContext ctx = createMockServletContextWithoutSpring() > // add Spring to the servlet context > ctx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEX_ATTRIBUTE) > // and now do the roundtrip > MockRoundtrip trip = new MockRoundtrip(ctx, MyAction.class) > trip.xyz() > > ? > > Cheers > > Remi > > > 2011/2/22 VANKEISBELCK Remi < r...@rvkb.com > > > > Yeah your "first attempt" makes much more sense than this static field :) > > One thing though, what do you mean by "injecting into JUnit" ? > > From what I understood, you want to configure the mock object *prior* to > performing a mock round trip, don't you ? > > Cheers > > > > > Remi > > > 2011/2/22 Marcus Kraßmann < m...@syn-online.de > > > > Hi Remi, > > Thanks for your reply. Actually I have to wrap it because MockRoundtrip uses > another Spring ApplicationContext than JUnit itself. Fact is that if I inject > LoginService into JUnit (which is configured to be a singleton instance of > MockitoLoginService), then it is another instance than the one that is > injected into my action bean. > > Surely I could write a simple mock object that behaves the way I want without > Mockito. But by using Mockito, I can configure my test stubs very easily > without writing much code. At least in theory. > > If that still does not help to clearify my problem: > My first try was to declare a Mockito stub as Spring bean by declaring this > in my applicationContext.xml: > > scope="singleton"> > > > > This _should_ work fine, but is doesn't. The service is created twice, once > when starting up my JUnit tests, and the second time when the StripesFilter > is initialized in my test fixture. So if I configure a mocked LoginService in > JUnit, I configure another object than the one used by MockRoundtrip. > > Hope that this helps to understand the issue. I know that this is quite > "advanced" stuff. I just hope that someone else hat at least a similar issue > and knows a fine solution for the "two Spring contexts" problem :-) > > Best regards, > Marcus > > - Ursprüngliche Mail - > Von: "VANKEISBELCK Remi" < r...@rvkb.com > > An: "Stripes Users List" < stripes-users@lists.sourceforge.net > > CC: "Marcus Kraßmann" < m...@syn-online.de > > Gesendet: Dienstag, 22. Februar 2011 13:38:36 > Betreff:
Re: [Stripes-users] Spring + Mockito
Hi Marcus, I was testing my action beans as Remi suggested, so it should work. Currently, I use a custom interceptor and a modified version of SpringHelper to create/inject mock spring beans into an action bean automatically. As it's a little bit tricky to explain, I would create a simple example project and upload it to somewhere if you are interested. Regards, Iwao 2011/2/22 Marcus Kraßmann : > Hi Remi, > > ctx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEX_ATTRIBUTE, > springContext) > > I also had this idea some weeks ago. I cannot surely remember the reason why > I dropped it, but I think that I was not able to "convert" the > ApplicationContext used by JUnit to a WebApplicationContext that is needed > for the whole servlet thing. So your approach fails IIRC. > > Maybe someday I will review the whole thing and find a way to make your > approach work. That would be the best way: Using the "Spring test context" in > Stripes. > > Best wishes, > Marcus > > - Ursprüngliche Mail - > Von: "VANKEISBELCK Remi" > An: "Stripes Users List" > CC: "Marcus Kraßmann" > Gesendet: Dienstag, 22. Februar 2011 14:44:04 > Betreff: Re: [Stripes-users] Spring + Mockito > > Btw, what about : > > // create the Spring context for our test > ApplicationContext appCtx = createSpringAppCtx() > // play with the mockito bean > LoginService ls = (LoginService)appCtx.getBean(...) > ... > // create a mock servlet context, without the Spring context loader > MockServletContext ctx = createMockServletContextWithoutSpring() > // add Spring to the servlet context > ctx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEX_ATTRIBUTE) > // and now do the roundtrip > MockRoundtrip trip = new MockRoundtrip(ctx, MyAction.class) > trip.xyz() > > ? > > Cheers > > Remi > > > 2011/2/22 VANKEISBELCK Remi < r...@rvkb.com > > > > Yeah your "first attempt" makes much more sense than this static field :) > > One thing though, what do you mean by "injecting into JUnit" ? > > From what I understood, you want to configure the mock object *prior* to > performing a mock round trip, don't you ? > > Cheers > > > > > Remi > > > 2011/2/22 Marcus Kraßmann < m...@syn-online.de > > > > Hi Remi, > > Thanks for your reply. Actually I have to wrap it because MockRoundtrip uses > another Spring ApplicationContext than JUnit itself. Fact is that if I inject > LoginService into JUnit (which is configured to be a singleton instance of > MockitoLoginService), then it is another instance than the one that is > injected into my action bean. > > Surely I could write a simple mock object that behaves the way I want without > Mockito. But by using Mockito, I can configure my test stubs very easily > without writing much code. At least in theory. > > If that still does not help to clearify my problem: > My first try was to declare a Mockito stub as Spring bean by declaring this > in my applicationContext.xml: > > scope="singleton"> > > > > This _should_ work fine, but is doesn't. The service is created twice, once > when starting up my JUnit tests, and the second time when the StripesFilter > is initialized in my test fixture. So if I configure a mocked LoginService in > JUnit, I configure another object than the one used by MockRoundtrip. > > Hope that this helps to understand the issue. I know that this is quite > "advanced" stuff. I just hope that someone else hat at least a similar issue > and knows a fine solution for the "two Spring contexts" problem :-) > > Best regards, > Marcus > > - Ursprüngliche Mail - > Von: "VANKEISBELCK Remi" < r...@rvkb.com > > An: "Stripes Users List" < stripes-users@lists.sourceforge.net > > CC: "Marcus Kraßmann" < m...@syn-online.de > > Gesendet: Dienstag, 22. Februar 2011 13:38:36 > Betreff: Re: [Stripes-users] Spring + Mockito > > > > > Hi Marcus, > > Not sure I understand what you're trying to do... > > Why do you have to wrap the mock ? > > Cheers > > Remi > > > 2011/2/22 Marcus Kraßmann < m...@syn-online.de > > > > 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
Re: [Stripes-users] Spring + Mockito
H, then do it the other way maybe ? Let Stripes create a real WebApplicationContext (in MockServletContext, using Spring's loader) and use it before you roundtrip : // create stripes mock context (loads spring via context listener) MockServletContext ctx = createMockServletContextWithSpring() // get hold of the mocked service WebApplicationContext springCtx = WebApplicationContext.getWebApplicationContext(ctx) LoginService mockitoLoginService = (LoginService)springCtx.getBean(...) mockitoLoginService.xyz() ... // And later, perform the roundtrip (will inject the mock service)... MockRoundtrip trip = new MockRoundtrip(ctx, MyAction.class) ... Ok, it doesn't solve your problem, rather it's a workaround. Still, I find it more concise and simple than all those annotations : you see what's done reading the code, there's no magic :P Anyway, good luck :) Cheers Remi 2011/2/22 Marcus Kraßmann > Hi Remi, > > ctx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEX_ATTRIBUTE, > springContext) > > I also had this idea some weeks ago. I cannot surely remember the reason > why I dropped it, but I think that I was not able to "convert" the > ApplicationContext used by JUnit to a WebApplicationContext that is needed > for the whole servlet thing. So your approach fails IIRC. > > Maybe someday I will review the whole thing and find a way to make your > approach work. That would be the best way: Using the "Spring test context" > in Stripes. > > Best wishes, > Marcus > > - Ursprüngliche Mail - > Von: "VANKEISBELCK Remi" > An: "Stripes Users List" > CC: "Marcus Kraßmann" > Gesendet: Dienstag, 22. Februar 2011 14:44:04 > Betreff: Re: [Stripes-users] Spring + Mockito > > Btw, what about : > > // create the Spring context for our test > ApplicationContext appCtx = createSpringAppCtx() > // play with the mockito bean > LoginService ls = (LoginService)appCtx.getBean(...) > ... > // create a mock servlet context, without the Spring context loader > MockServletContext ctx = createMockServletContextWithoutSpring() > // add Spring to the servlet context > > ctx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEX_ATTRIBUTE) > // and now do the roundtrip > MockRoundtrip trip = new MockRoundtrip(ctx, MyAction.class) > trip.xyz() > > ? > > Cheers > > Remi > > > 2011/2/22 VANKEISBELCK Remi < r...@rvkb.com > > > > Yeah your "first attempt" makes much more sense than this static field :) > > One thing though, what do you mean by "injecting into JUnit" ? > > From what I understood, you want to configure the mock object *prior* to > performing a mock round trip, don't you ? > > Cheers > > > > > Remi > > > 2011/2/22 Marcus Kraßmann < m...@syn-online.de > > > > Hi Remi, > > Thanks for your reply. Actually I have to wrap it because MockRoundtrip > uses another Spring ApplicationContext than JUnit itself. Fact is that if I > inject LoginService into JUnit (which is configured to be a singleton > instance of MockitoLoginService), then it is another instance than the one > that is injected into my action bean. > > Surely I could write a simple mock object that behaves the way I want > without Mockito. But by using Mockito, I can configure my test stubs very > easily without writing much code. At least in theory. > > If that still does not help to clearify my problem: > My first try was to declare a Mockito stub as Spring bean by declaring this > in my applicationContext.xml: > > scope="singleton"> > > > > This _should_ work fine, but is doesn't. The service is created twice, once > when starting up my JUnit tests, and the second time when the StripesFilter > is initialized in my test fixture. So if I configure a mocked LoginService > in JUnit, I configure another object than the one used by MockRoundtrip. > > Hope that this helps to understand the issue. I know that this is quite > "advanced" stuff. I just hope that someone else hat at least a similar issue > and knows a fine solution for the "two Spring contexts" problem :-) > > Best regards, > Marcus > > - Ursprüngliche Mail - > Von: "VANKEISBELCK Remi" < r...@rvkb.com > > An: "Stripes Users List" < stripes-users@lists.sourceforge.net > > CC: "Marcus Kraßmann" < m...@syn-online.de > > Gesendet: Dienstag, 22. Februar 2011 13:38:36 > Betreff: Re: [Stripes-users] Spring + Mockito > > > > > Hi Marcus, > > Not sure I understand what you're trying to do... > > Why do you have to wrap the mock ? > > Ch
Re: [Stripes-users] Spring + Mockito
Hi Remi, ctx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEX_ATTRIBUTE, springContext) I also had this idea some weeks ago. I cannot surely remember the reason why I dropped it, but I think that I was not able to "convert" the ApplicationContext used by JUnit to a WebApplicationContext that is needed for the whole servlet thing. So your approach fails IIRC. Maybe someday I will review the whole thing and find a way to make your approach work. That would be the best way: Using the "Spring test context" in Stripes. Best wishes, Marcus - Ursprüngliche Mail - Von: "VANKEISBELCK Remi" An: "Stripes Users List" CC: "Marcus Kraßmann" Gesendet: Dienstag, 22. Februar 2011 14:44:04 Betreff: Re: [Stripes-users] Spring + Mockito Btw, what about : // create the Spring context for our test ApplicationContext appCtx = createSpringAppCtx() // play with the mockito bean LoginService ls = (LoginService)appCtx.getBean(...) ... // create a mock servlet context, without the Spring context loader MockServletContext ctx = createMockServletContextWithoutSpring() // add Spring to the servlet context ctx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEX_ATTRIBUTE) // and now do the roundtrip MockRoundtrip trip = new MockRoundtrip(ctx, MyAction.class) trip.xyz() ? Cheers Remi 2011/2/22 VANKEISBELCK Remi < r...@rvkb.com > Yeah your "first attempt" makes much more sense than this static field :) One thing though, what do you mean by "injecting into JUnit" ? From what I understood, you want to configure the mock object *prior* to performing a mock round trip, don't you ? Cheers Remi 2011/2/22 Marcus Kraßmann < m...@syn-online.de > Hi Remi, Thanks for your reply. Actually I have to wrap it because MockRoundtrip uses another Spring ApplicationContext than JUnit itself. Fact is that if I inject LoginService into JUnit (which is configured to be a singleton instance of MockitoLoginService), then it is another instance than the one that is injected into my action bean. Surely I could write a simple mock object that behaves the way I want without Mockito. But by using Mockito, I can configure my test stubs very easily without writing much code. At least in theory. If that still does not help to clearify my problem: My first try was to declare a Mockito stub as Spring bean by declaring this in my applicationContext.xml: This _should_ work fine, but is doesn't. The service is created twice, once when starting up my JUnit tests, and the second time when the StripesFilter is initialized in my test fixture. So if I configure a mocked LoginService in JUnit, I configure another object than the one used by MockRoundtrip. Hope that this helps to understand the issue. I know that this is quite "advanced" stuff. I just hope that someone else hat at least a similar issue and knows a fine solution for the "two Spring contexts" problem :-) Best regards, Marcus - Ursprüngliche Mail - Von: "VANKEISBELCK Remi" < r...@rvkb.com > An: "Stripes Users List" < stripes-users@lists.sourceforge.net > CC: "Marcus Kraßmann" < m...@syn-online.de > Gesendet: Dienstag, 22. Februar 2011 13:38:36 Betreff: Re: [Stripes-users] Spring + Mockito Hi Marcus, Not sure I understand what you're trying to do... Why do you have to wrap the mock ? Cheers Remi 2011/2/22 Marcus Kraßmann < m...@syn-online.de > 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:
Re: [Stripes-users] Spring + Mockito
Btw, what about : // create the Spring context for our test ApplicationContext appCtx = createSpringAppCtx() // play with the mockito bean LoginService ls = (LoginService)appCtx.getBean(...) ... // create a mock servlet context, without the Spring context loader MockServletContext ctx = createMockServletContextWithoutSpring() // add Spring to the servlet context ctx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEX_ATTRIBUTE) // and now do the roundtrip MockRoundtrip trip = new MockRoundtrip(ctx, MyAction.class) trip.xyz() ? Cheers Remi 2011/2/22 VANKEISBELCK Remi > Yeah your "first attempt" makes much more sense than this static field :) > > One thing though, what do you mean by "injecting into JUnit" ? > > From what I understood, you want to configure the mock object *prior* to > performing a mock round trip, don't you ? > > Cheers > > > Remi > > 2011/2/22 Marcus Kraßmann > >> Hi Remi, >> >> Thanks for your reply. Actually I have to wrap it because MockRoundtrip >> uses another Spring ApplicationContext than JUnit itself. Fact is that if I >> inject LoginService into JUnit (which is configured to be a singleton >> instance of MockitoLoginService), then it is another instance than the one >> that is injected into my action bean. >> >> Surely I could write a simple mock object that behaves the way I want >> without Mockito. But by using Mockito, I can configure my test stubs very >> easily without writing much code. At least in theory. >> >> If that still does not help to clearify my problem: >> My first try was to declare a Mockito stub as Spring bean by declaring >> this in my applicationContext.xml: >> >>> factory-method="mock" scope="singleton"> >> >> >> >> This _should_ work fine, but is doesn't. The service is created twice, >> once when starting up my JUnit tests, and the second time when the >> StripesFilter is initialized in my test fixture. So if I configure a mocked >> LoginService in JUnit, I configure another object than the one used by >> MockRoundtrip. >> >> Hope that this helps to understand the issue. I know that this is quite >> "advanced" stuff. I just hope that someone else hat at least a similar issue >> and knows a fine solution for the "two Spring contexts" problem :-) >> >> Best regards, >> Marcus >> >> - Ursprüngliche Mail - >> Von: "VANKEISBELCK Remi" >> An: "Stripes Users List" >> CC: "Marcus Kraßmann" >> Gesendet: Dienstag, 22. Februar 2011 13:38:36 >> Betreff: Re: [Stripes-users] Spring + Mockito >> >> Hi Marcus, >> >> Not sure I understand what you're trying to do... >> >> Why do you have to wrap the mock ? >> >> Cheers >> >> Remi >> >> >> 2011/2/22 Marcus Kraßmann < m...@syn-online.de > >> >> >> 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 af
Re: [Stripes-users] Spring + Mockito
Yeah your "first attempt" makes much more sense than this static field :) One thing though, what do you mean by "injecting into JUnit" ? >From what I understood, you want to configure the mock object *prior* to performing a mock round trip, don't you ? Cheers Remi 2011/2/22 Marcus Kraßmann > Hi Remi, > > Thanks for your reply. Actually I have to wrap it because MockRoundtrip > uses another Spring ApplicationContext than JUnit itself. Fact is that if I > inject LoginService into JUnit (which is configured to be a singleton > instance of MockitoLoginService), then it is another instance than the one > that is injected into my action bean. > > Surely I could write a simple mock object that behaves the way I want > without Mockito. But by using Mockito, I can configure my test stubs very > easily without writing much code. At least in theory. > > If that still does not help to clearify my problem: > My first try was to declare a Mockito stub as Spring bean by declaring this > in my applicationContext.xml: > > factory-method="mock" scope="singleton"> > > > > This _should_ work fine, but is doesn't. The service is created twice, once > when starting up my JUnit tests, and the second time when the StripesFilter > is initialized in my test fixture. So if I configure a mocked LoginService > in JUnit, I configure another object than the one used by MockRoundtrip. > > Hope that this helps to understand the issue. I know that this is quite > "advanced" stuff. I just hope that someone else hat at least a similar issue > and knows a fine solution for the "two Spring contexts" problem :-) > > Best regards, > Marcus > > - Ursprüngliche Mail - > Von: "VANKEISBELCK Remi" > An: "Stripes Users List" > CC: "Marcus Kraßmann" > Gesendet: Dienstag, 22. Februar 2011 13:38:36 > Betreff: Re: [Stripes-users] Spring + Mockito > > Hi Marcus, > > Not sure I understand what you're trying to do... > > Why do you have to wrap the mock ? > > Cheers > > Remi > > > 2011/2/22 Marcus Kraßmann < m...@syn-online.de > > > > 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 complian
Re: [Stripes-users] Spring + Mockito
Hi Remi, Thanks for your reply. Actually I have to wrap it because MockRoundtrip uses another Spring ApplicationContext than JUnit itself. Fact is that if I inject LoginService into JUnit (which is configured to be a singleton instance of MockitoLoginService), then it is another instance than the one that is injected into my action bean. Surely I could write a simple mock object that behaves the way I want without Mockito. But by using Mockito, I can configure my test stubs very easily without writing much code. At least in theory. If that still does not help to clearify my problem: My first try was to declare a Mockito stub as Spring bean by declaring this in my applicationContext.xml: This _should_ work fine, but is doesn't. The service is created twice, once when starting up my JUnit tests, and the second time when the StripesFilter is initialized in my test fixture. So if I configure a mocked LoginService in JUnit, I configure another object than the one used by MockRoundtrip. Hope that this helps to understand the issue. I know that this is quite "advanced" stuff. I just hope that someone else hat at least a similar issue and knows a fine solution for the "two Spring contexts" problem :-) Best regards, Marcus - Ursprüngliche Mail - Von: "VANKEISBELCK Remi" An: "Stripes Users List" CC: "Marcus Kraßmann" Gesendet: Dienstag, 22. Februar 2011 13:38:36 Betreff: Re: [Stripes-users] Spring + Mockito Hi Marcus, Not sure I understand what you're trying to do... Why do you have to wrap the mock ? Cheers Remi 2011/2/22 Marcus Kraßmann < m...@syn-online.de > 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 -- 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
Re: [Stripes-users] Spring + Mockito
Hi Marcus, Not sure I understand what you're trying to do... Why do you have to wrap the mock ? Cheers Remi 2011/2/22 Marcus Kraßmann > 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 > -- 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
[Stripes-users] Spring + Mockito
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