sbrannen / spring-events Goto Github PK
View Code? Open in Web Editor NEWSpring Events Sample Application
License: Apache License 2.0
Spring Events Sample Application
License: Apache License 2.0
When having @Nested
tests in EventRepositoryTests
, it seems that there is an issue with autowired dependencies and transactional context.
For example, if you put delete()
, update()
and save()
tests into a nested WriteTests
class, and findAll()
with findById()
tests into a nested ReadTests
class (like this), update()
and findById()
tests will fail:
└─ JUnit Jupiter ✔
├─ ApplicationTests ↷ class com.sambrannen.spring.events.ApplicationTests is @Disabled
├─ EventRepositoryTests ✔
│ ├─ WriteTests ✔
│ │ ├─ delete() ✔
│ │ ├─ update() ✘
│ │ │ Expecting actual not to be null
│ │ └─ save() ✔
│ └─ ReadTests ✔
│ ├─ findAll() ✔
│ └─ findById() ✘
│ Expecting Optional to contain a value but was empty.
├─ EventTests ✔
│ ├─ lombokShouldGetCorrectly() ✔
│ ├─ eventsShouldHaveSameHashCodeWithDifferentIdsButTheSameName() ✔
│ ├─ eventsShouldBeEqualWithDifferentIdsButTheSameName() ✔
│ ├─ lombokShouldSetCorrectly() ✔
│ ├─ eventsShouldNotBeEqualWithTheSameIdButDifferentNames() ✔
│ └─ eventsShouldNotHaveSameHashCodeWithTheSameIdButDifferentNames() ✔
├─ RestEventsControllerTests ✔
│ ├─ retrieveNonexistentEvent() ✔
│ ├─ deleteEvent() ✔
│ ├─ updateEvent() ✔
│ ├─ createEvent() ✔
│ ├─ retrieveAllEvents() ✔
│ └─ retrieveEvent() ✔
├─ EventsControllerWithMockedServiceTests ✔
│ └─ listEvents(MockMvc) ✔
└─ EventsControllerTests ✔
└─ Home page should display more than 10 events ✔
Failures (2):
JUnit Jupiter:EventRepositoryTests:WriteTests:update()
MethodSource [className = 'com.sambrannen.spring.events.repository.EventRepositoryTests$WriteTests', methodName = 'update', methodParameterTypes = '']
=> java.lang.AssertionError:
Expecting actual not to be null
JUnit Jupiter:EventRepositoryTests:ReadTests:findById()
MethodSource [className = 'com.sambrannen.spring.events.repository.EventRepositoryTests$ReadTests', methodName = 'findById', methodParameterTypes = '']
=> java.lang.AssertionError:
Expecting Optional to contain a value but was empty.
./gradlew check
I believe EventsControllerTests
(which only differs to EventsControllerIT
by the mocking of eventService
) is outside the web
package because the TestConfiguration
's @EnableWebMvc
annotation causes the Spring Boot Jackson auto-configuration not be used. If this was in the web
package as is, it would impact RestEventsControllerIT.retreiveEvent
because the eventDate
won't be serialized properly.
However, if @EnableWebMvc
was changed to @EnableAutoConfiguration
, I think things should work fine in the web
package as the Jackson JSR-310-based ObjectMapper
will continue to be used.
See: Default Jackson HttpMessageConverter does not honor autoconfigured ObjectMapper
Just a suggestion. Would appreciate your feedback.
Application should extend WebMvcAutoConfiguration
; otherwise, static content can not be served when started via 'Run As - Java Application'. (see patch below)
diff --git a/src/main/java/com/sambrannen/spring/events/Application.java b/src/main/java/com/sambrannen/spring/events/Application.java
index 2bc47b0..3cb757f 100644
--- a/src/main/java/com/sambrannen/spring/events/Application.java
+++ b/src/main/java/com/sambrannen/spring/events/Application.java
@@ -19,6 +19,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
@@ -34,7 +35,7 @@
*/
@SpringBootApplication(scanBasePackageClasses = { EventService.class, EventsController.class })
@EnableGlobalMethodSecurity(prePostEnabled = true)
-public class Application {
+public class Application extends WebMvcAutoConfiguration {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.