Comments (9)
@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.
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.
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.
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.
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.
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.
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.
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.
Cool. I'll watch that JIRA and try it out when it's ready.
from spring-boot.
Related Issues (20)
- Custom Jackson annotation on Spring boot 3.3 fail HOT 4
- Upgrade to Flyway 10.15.0
- jpms: don't try to initialize mockito... HOT 2
- Upgrade to GraphQL Java 22.1 HOT 3
- Missing TraceId in Web + WebFlux application since v3.3.1 HOT 1
- DirtiesContext used with Webflux, a random port and multiple contexts causes multiple contexts to misbehave
- DirtiesContext used with Webflux, a random port and multiple contexts causes multiple contexts to misbehave
- Is the Spring Maven plugin still using CF Memory Calculator? HOT 3
- DirtiesContext used with Webflux, a random port and multiple contexts causes multiple contexts to misbehave HOT 1
- When using Jetty, filters, listeners, and servlets are not initialized with the same thread context classloader
- When using Jetty, filters, listeners, and servlets are not initialized with the same thread context classloader
- Explain difference between OTel agent and Micrometer instrumentations HOT 1
- Run Docker-based tests serially
- Multi DataSource setup stopped working after upgrading Spring Boot to 3.3.x HOT 3
- How spring.messages.basename config path file jar outsite, to easy edit message no rebuild source HOT 3
- RedisAutoConfiguration via url doesn't allow to pass username + password outside the url HOT 4
- WebClient with HttpComponentsClientHttpConnector does not send TLS fatal alerts HOT 2
- mvn spring-boot:build-image command failing to create docker image of Spring Boot application when Docker Desktop version 4.31.x is installed HOT 1
- build-image failures after docker desktop update with 'Illegal char <:> at index 5: npipe:////'
- build-image failures after docker desktop update with 'Illegal char <:> at index 5: npipe:////'
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spring-boot.