Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for repository testing and maybe convenience annotations for integration testing #66

Closed
ewolff opened this issue Oct 2, 2013 · 9 comments
Labels
type: enhancement A general enhancement

Comments

@ewolff
Copy link

ewolff commented Oct 2, 2013

It would be great to have better tests support. So far it seems the only way to do tests is to run an embedded server. I would like

  • Support for an annotation like ContextConfiguration but using a Spring Boot application class instead. It would fire up the ApplicationContext - but not the web part. That way services and repositories could be tested without a container.
  • An probably also support for the Spring Web MVC testing support instead of in-container testing.
@ewolff
Copy link
Author

ewolff commented Oct 2, 2013

@RunWith(SpringJUnit4ClassRunner.class)
This seems to be close to what is needed:

@WebAppConfiguration
@ContextConfiguration(classes = SampleDataJpaApplication.class, initializers = {
ConfigFileApplicationContextInitializer.class,
LoggingApplicationContextInitializer.class })
public abstract class AbstractIntegrationTests {

}

Taken from the sample data jpa project. Just inherit from the class and use @Autowired as usual.

@dsyer
Copy link
Member

dsyer commented Oct 2, 2013

So you agree that the existing MVC support is adequate (maybe some docs)? Or you think we should add an annotation or something to pull together your example into one line?

@olivergierke has also asked about repository testing without the web layer. I was under the impress that he might contribute some code but I haven't seen anything yet so I guess he hasn't had time. If you have any ideas you want to contribute feel free.

@ewolff
Copy link
Author

ewolff commented Oct 2, 2013

The code above is from the Spring Boot Data Jpa Sample @olivergierke wrote. I think it would make sense to provide it as a class to inherit from for tests or better yet a class to be used with @RunWith

@odrotbohm
Copy link
Member

Back in the days I tried to migrate Spring RESTBucks to Boot but ran into the situation @ewolff describes. In a web app, there's no way to reuse a configuration class annotated with @EnableAutoConfiguration in test scenarios as this will always bootstrap the entire web container. This is way more than needed if all you want to test is your entity mapping and a bunch of query methods.

The sample code Eberhard found, apparently is from the repositories-deepdive project. I found out the necessary Initializer classes needed to successfully inspect the Boot config files during application context startup. So while this enables you to run the Spring test context framework and run Boot alongside it, this is not a solution for the aforementioned problem.

I think we should make sure that developers can use the test context framework as before. I don't even think I'd want to have a custom runner or enumerate the necessary listeners myself. We might have to get @sbrannen into the loop. In the best case the test context frameworks detects Boot in the classpath and configures itself automatically. If that is a no-go, we probably should go with something custom to Boot.

Not sure it's the most elegant solution but I can imagine an enum indicating which parts to bootstrap:
PERSISTENCE, WEB, CONTAINER. The latter being inclusive of the former. On the other hand, detecting @WebConfiguration should be sufficient to indicate you want to use Spring Web MVC testing support.

@dsyer
Copy link
Member

dsyer commented Oct 2, 2013

Yeah, I'm not sure I like the enumeration either - I think the choice comes down to web or not-web. You can flip that switch pretty easily by setting SpringApplication.webEnvironment=false, so maybe if we had a ContextLoader that used SpringApplication it could do that.

@ewolff
Copy link
Author

ewolff commented Oct 2, 2013

Yes, that sound like a good idea. Basically I just want to test the
business logic without the web tier. So that switch should be sufficient.

@dsyer
Copy link
Member

dsyer commented Oct 2, 2013

I just pushed a SpringApplicationContextLoader (and used it in the JPA sample). Check it out and see if it works for you or has any obvious issues that might show up for others.

Spring Test doesn't support @ContextConfiguration as a meta-annotation, so unless we can fix that we can't make it any easier. I think Spring 4 has extended the programming model for meta-annotations so we probably could have a @SpringContextConfiguration (have to ask @sbrannen).

@sbrannen
Copy link
Member

sbrannen commented Oct 2, 2013

Yes, Spring Framework 4.0 will include meta-annotation support in the TestContext framework. I'm actively working on that right now. Details here: SPR-7827.

@dsyer
Copy link
Member

dsyer commented Oct 2, 2013

Cool. I'll watch that JIRA and try it out when it's ready.

@dsyer dsyer closed this as completed in 3e6c1b4 Nov 28, 2013
gigfork pushed a commit to boostrack/spring-boot that referenced this issue Apr 21, 2014
Example:

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringApplicationConfiguration(classes = SampleDataJpaApplication.class)
    public class CityRepositoryIntegrationTests {

    	@Autowired
    	CityRepository repository;

Fixes spring-projectsgh-66.
mocleiri pushed a commit to mocleiri/spring-boot that referenced this issue Apr 20, 2015
Example:

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringApplicationConfiguration(classes = SampleDataJpaApplication.class)
    public class CityRepositoryIntegrationTests {

    	@Autowired
    	CityRepository repository;

Fixes spring-projectsgh-66.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants