Closed lhartmann2 closed 1 year ago
Hello, and thanks for reporting this issue!
Can you change the test-task in the product-composite-service's build.gradle
file so that it writes output from the application to the terminal.
The test-task should look like:
tasks.named('test') {
useJUnitPlatform()
testLogging {
outputs.upToDateWhen {false}
showStandardStreams = true
}
}
Rerun the test and add a comment to this issue with the output from running the tests with the command:
./gradlew microservices:product-composite:test
Hi Magnus, thank you for the quick reply!
I've updated the build.gradle
file as instructed. Here is the output from the test:
> Task :microservices:product-composite-service:test
ProductCompositeServiceApplicationTests STANDARD_OUT
11:59:20.399 [Test worker] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [ProductCompositeServiceApplicationTests]: using SpringBootContextLoader
11:59:20.411 [Test worker] DEBUG org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.lhp.microservices.composite.product.ProductCompositeServiceApplicationTests]: no resource found for suffixes {-context.xml, Context.groovy}.
11:59:20.457 [Test worker] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using ContextCustomizers for test class [ProductCompositeServiceApplicationTests]: [DisableObservabilityContextCustomizer, PropertyMappingContextCustomizer, Customizer, ExcludeFilterContextCustomizer, DuplicateJsonObjectContextCustomizer, MockitoContextCustomizer, TestRestTemplateContextCustomizer, WebTestClientContextCustomizer]
11:59:20.621 [Test worker] DEBUG org.springframework.test.context.util.TestContextSpringFactoriesUtils - Skipping candidate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener] due to a missing dependency. Specify custom TestExecutionListener classes or make the default TestExecutionListener classes and their required dependencies available. Offending class: [org/springframework/transaction/TransactionDefinition]
11:59:20.625 [Test worker] DEBUG org.springframework.test.context.util.TestContextSpringFactoriesUtils - Skipping candidate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] due to a missing dependency. Specify custom TestExecutionListener classes or make the default TestExecutionListener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]
11:59:20.629 [Test worker] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners for test class [ProductCompositeServiceApplicationTests]: [ServletTestExecutionListener, DirtiesContextBeforeModesTestExecutionListener, ApplicationEventsTestExecutionListener, MockitoTestExecutionListener, DependencyInjectionTestExecutionListener, DirtiesContextTestExecutionListener, EventPublishingTestExecutionListener, RestDocsTestExecutionListener, MockRestServiceServerResetTestExecutionListener, MockMvcPrintOnlyOnFailureTestExecutionListener, WebDriverTestExecutionListener, MockWebServiceServerTestExecutionListener, ResetMocksTestExecutionListener]
11:59:20.632 [Test worker] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: class [ProductCompositeServiceApplicationTests], class annotated with @DirtiesContext [false] with mode [null]
11:59:21.285 [Test worker] DEBUG org.springframework.test.context.support.DependencyInjectionTestExecutionListener - Performing dependency injection for test class com.lhp.microservices.composite.product.ProductCompositeServiceApplicationTests
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.0.3)
2023-03-01T11:59:21.810-06:00 INFO 254850 --- [ Test worker] .ProductCompositeServiceApplicationTests : Starting ProductCompositeServiceApplicationTests using Java 17.0.6 with PID 254850 (started by logan in /home/logan/Documents/Java Projects/microservices/microservices/product-composite-service)
2023-03-01T11:59:21.814-06:00 INFO 254850 --- [ Test worker] .ProductCompositeServiceApplicationTests : No active profile set, falling back to 1 default profile: "default"
2023-03-01T11:59:23.603-06:00 INFO 254850 --- [ Test worker] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 0 (http)
2023-03-01T11:59:23.617-06:00 INFO 254850 --- [ Test worker] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-03-01T11:59:23.618-06:00 INFO 254850 --- [ Test worker] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.5]
2023-03-01T11:59:23.743-06:00 INFO 254850 --- [ Test worker] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-03-01T11:59:23.746-06:00 INFO 254850 --- [ Test worker] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1896 ms
2023-03-01T11:59:24.681-06:00 INFO 254850 --- [ Test worker] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator'
2023-03-01T11:59:24.792-06:00 INFO 254850 --- [ Test worker] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 43527 (http) with context path ''
2023-03-01T11:59:24.813-06:00 INFO 254850 --- [ Test worker] .ProductCompositeServiceApplicationTests : Started ProductCompositeServiceApplicationTests in 3.499 seconds (process running for 5.61)
ProductCompositeServiceApplicationTests > getProductById() STANDARD_OUT
2023-03-01T11:59:25.635-06:00 INFO 254850 --- [o-auto-1-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-03-01T11:59:25.636-06:00 INFO 254850 --- [o-auto-1-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2023-03-01T11:59:25.637-06:00 INFO 254850 --- [o-auto-1-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
2023-03-01T11:59:25.883-06:00 ERROR 254850 --- [ Test worker] o.s.t.w.reactive.server.ExchangeResult : Request details for assertion failure:
> GET http://localhost:43527/product-composite/1
> accept-encoding: [gzip]
> user-agent: [ReactorNetty/1.1.3]
> host: [localhost:43527]
> WebTestClient-Request-Id: [1]
> Accept: [application/json]
No content
< 404 NOT_FOUND Not Found
< Vary: [Origin, Access-Control-Request-Method, Access-Control-Request-Headers]
< Content-Type: [application/json]
< Transfer-Encoding: [chunked]
< Date: [Wed, 01 Mar 2023 17:59:25 GMT]
{"timestamp":"2023-03-01T17:59:25.702+00:00","status":404,"error":"Not Found","path":"/product-composite/1"}
ProductCompositeServiceApplicationTests > getProductById() FAILED
java.lang.AssertionError at ProductCompositeServiceApplicationTests.java:61
ProductCompositeServiceApplicationTests > getProductInvalidInput() STANDARD_OUT
2023-03-01T11:59:25.924-06:00 ERROR 254850 --- [ Test worker] o.s.t.w.reactive.server.ExchangeResult : Request details for assertion failure:
> GET http://localhost:43527/product-composite/3
> accept-encoding: [gzip]
> user-agent: [ReactorNetty/1.1.3]
> host: [localhost:43527]
> WebTestClient-Request-Id: [2]
> Accept: [application/json]
No content
< 404 NOT_FOUND Not Found
< Vary: [Origin, Access-Control-Request-Method, Access-Control-Request-Headers]
< Content-Type: [application/json]
< Transfer-Encoding: [chunked]
< Date: [Wed, 01 Mar 2023 17:59:25 GMT]
{"timestamp":"2023-03-01T17:59:25.919+00:00","status":404,"error":"Not Found","path":"/product-composite/3"}
ProductCompositeServiceApplicationTests > getProductInvalidInput() FAILED
java.lang.AssertionError at ProductCompositeServiceApplicationTests.java:90
ProductCompositeServiceApplicationTests > getProductNotFound() FAILED
java.lang.AssertionError at ProductCompositeServiceApplicationTests.java:80
Caused by: com.jayway.jsonpath.PathNotFoundException
4 tests completed, 3 failed
> Task :microservices:product-composite-service:test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':microservices:product-composite-service:test'.
> There were failing tests. See the report at: file:///home/logan/Documents/Java%20Projects/microservices/microservices/product-composite-service/build/reports/tests/test/index.html
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 22s
4 actionable tasks: 3 executed, 1 up-to-date
Hello again!
I can see that you are using Spring Boot v3.0.3. Since the book is based on v2.5.2, I'm not sure if everything will work out of the box using v3.0.3. But the tests in Chapter 3 should for sure work :-)
Have you compared your code with the code in the book's GitHub repo?
Can you verify that the tests in the GitHub repo works by running the following commands in a temp-folder:
git clone https://github.com/PacktPublishing/Microservices-with-Spring-Boot-and-Spring-Cloud-2E.git
cd Microservices-with-Spring-Boot-and-Spring-Cloud-2E/Chapter03/2-basic-rest-services
./gradlew microservices:product-composite:test
Hi Magnus,
I ran your code in a temp folder and it ran fine.
I then changed the Spring Boot version for all 6 of my projects from 3.0.3 to 2.5.2 (and later 2.7.9) which caused them to suddenly work, so it seems that that was the likely culprit. Although I'm not sure what changed between 2.5.2 (or 2.7.9) and 3.0.3 that everything works except for the tests involving Mockito.
Either way I'm glad that it's now working and I thank you very much for your help!
Great, happy to help!
I'm closing this issue now.
Hello, In the unit tests for the product-composite-service in Chapter 3, all 3 tests fail with a 'No content - 404 Not Found'. I've even copied the test code verbatim from the source ProductCompositeServiceApplicationTests.java with the same result.
For example on getProductInvalidInput():
I'm at a loss as to what I'm doing wrong here. Any help is much appreciated. Thank you!