Reputation: 37
I have an Spring Boot Interceptor with a preHandle method that does some logic. In the preHandle method, I call my Authentication Service to get the client ID and then I set that client ID on the HttpServletRequest. My controller then calls my service using the client ID as one of the parameters.
This is causing my Spring Cloud Contract test to fail. I know this because when stepping through the debugger, the client ID is null, and when I hardcode a client ID in, the test passes. I tried to stub the HttpServletRequest.getAttribute method, but that does not work. I can also pass the SCC test by stubbing the service method, but I believe that is bad practice.
Also can someone explain to me what is exactly happening during the SCC test? For example, without stubbing repository methods and such, it looks like it is stepping through all of the logic, even though it is not actually querying the db, but there will be a return value after the repository method is called. And when I don't have my db running, it will cause the test to fail. Does it just create some mock values to method calls as long as the correct parameter types are passed in?
Upvotes: 0
Views: 46
Reputation: 37
The SCC tests pass after adding the following :
@TestPropertySource(properties = {
"spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE",
"spring.datasource.driverClassName=org.h2.Driver",
"spring.datasource.username=sa",
"spring.datasource.password=sa",
"spring.jpa.database-platform=org.hibernate.dialect.H2Dialect",
"spring.jpa.hibernate.ddl-auto=none",
// "spring.sql.init.mode=always",
"spring.datasource.initialization-mode=always",
})
I also added a schema.sql file with all of my tables under /src/test/resources. However I'm not exactly sure why this works. Previously I had my db running, but now switched over to an H2 database but the H2 database is not populated.
Upvotes: 0