Giter VIP home page Giter VIP logo

Comments (9)

ewolff avatar ewolff commented on March 28, 2024

@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.

from spring-boot.

dsyer avatar dsyer commented on March 28, 2024

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.

from spring-boot.

ewolff avatar ewolff commented on March 28, 2024

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

from spring-boot.

odrotbohm avatar odrotbohm commented on March 28, 2024

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.

from spring-boot.

dsyer avatar dsyer commented on March 28, 2024

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.

from spring-boot.

ewolff avatar ewolff commented on March 28, 2024

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.

from spring-boot.

dsyer avatar dsyer commented on March 28, 2024

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).

from spring-boot.

sbrannen avatar sbrannen commented on March 28, 2024

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.

from spring-boot.

dsyer avatar dsyer commented on March 28, 2024

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

from spring-boot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.