arquillian-extension-spring icon indicating copy to clipboard operation
arquillian-extension-spring copied to clipboard

Adding Spring StaticApplicationContext

Open omidp opened this issue 12 years ago • 12 comments

It is a common way that developers programmatically register beans into context specially in testing rather than reading bean definitions from external configuration sources, in this case, you need to use StaticApplicationContext.

I added it to arquillian. please see StaticSpringContext

omidp avatar Feb 11 '13 08:02 omidp

The only problem with this approach is sharing the created instance in test class which we can use an annotation to do so e.g

@SpringContext StaticApplicationContext ctx; //created by Arquillian and inject in test class

WDYT ?

omidp avatar Feb 11 '13 08:02 omidp

Hi,

Thanks for the pull request! :) I went quickly through the code and could you please tell what would be the difference between using SpringAnnotationConfiguration and providing to it the packages to scan (their work not only for @Configuration, but for all Spring stereotype annotated classes) ?

Also, I think that I don't understand entirely the idea of injecting the @SpringContext StaticApplicationContext ctx;

into the test class. How could it be used afterwards?

BTW. would you mind creating a JIRA issue for this functionality. https://issues.jboss.org/browse/ARQ/component/12313437

jmnarloch avatar Feb 17 '13 12:02 jmnarloch

Also, please take a look at https://github.com/jmnarloch/arquillian-extension-guice/blob/master/int-tests/src/test/java/org/jboss/arquillian/extension/guice/testsuite/CustomInjectorTestCase.java

That's the Guice enricher. I added there custom @GuiceInjector which allows to "grab" the application context directly from the test. Maybe that's the direction we could also aproach with spring extension?

jmnarloch avatar Feb 17 '13 13:02 jmnarloch

As goes for other ideas: http://blog.springsource.com/2011/06/21/spring-3-1-m2-testing-with-configuration-classes-and-profiles/

Spring Test has nice capabilities, that were not yet implemented in Arquillian extension. I would especially favor having inner static class annoated with @Configuration, that could be used by the extension for configuring the application context. That would be really neat to have.

jmnarloch avatar Feb 17 '13 13:02 jmnarloch

I've created a JIRA issue and explained it there.

https://issues.jboss.org/browse/ARQ-1303

omidp avatar Feb 18 '13 06:02 omidp

What is SpringAnnotationConfiguration and where is it? I wasn't aware there is such a thing I haven't used Guice so I have no idea about your second comment

omidp avatar Feb 18 '13 08:02 omidp

The @SpringAnnotationConfiguration from arquillian-service-integration-spring-javaconfig is used for configuring the AnnotationConfigApplicationContext by providing the classes to scan or optional packages. The only drawback is that it requires Spring 3.x in order to be used.

jmnarloch avatar Feb 18 '13 18:02 jmnarloch

Hi again,

Let's continue the hight level discusion in JIRA and here let's discuss only the details.

I'm wondering how can be injected @SpringContext StaticApplicationContext used afterwads?

jmnarloch avatar Feb 20 '13 17:02 jmnarloch

Hi,

I found it useful to have spring context in your test for some occasions. it's like your test is ApplicationContextAware. for example :

  • For publishing events
 ctx.publishEvent(event)
  • for checking what beans has been autowired
  • for working with different environment ctx.getEnvironment() and maybe other situation that I don't know besides I've checked SpringAnnotationConfiguration, I wasn't aware of it. It's pretty useful and currently meets all my need for testing. maybe there is no need to have staticapplicationcontext.

omidp avatar Feb 21 '13 07:02 omidp

But you know that this works, out of the box in Spring? You just @Autowire the ApplicationContext. There is no need for special annotation that would inject it.

https://jira.springsource.org/browse/SPR-5101

jmnarloch avatar Feb 21 '13 08:02 jmnarloch

There is a test example in integrations tests: https://github.com/arquillian/arquillian-extension-spring/blob/master/arquillian-service-integration-spring-3-int-tests/src/test/java/org/jboss/arquillian/spring/testsuite/test/ApplicationContextTestCase.java

I have nothing against having the StaticApplicationContext as one of the implementations, altughout would mind having the annotations made consistent with the existing ones? I were thinking about one single annotation something like:

@interface SpringStaticConfiguration {

Class<?>[] classes(); String[] packages(); }

You can choice different name if you like.

jmnarloch avatar Feb 21 '13 18:02 jmnarloch

that's fine with me. I totally agree. thanks for your help

omidp avatar Feb 23 '13 05:02 omidp