SpringBoot not resolving devtools properties in tests - spring-boot

I'm using spring-boot-devtools with my SpringBoot application (2.2.8) to store secrets outside of my repository. This works for the running application, but integration tests fail with Unexpected exception during bean creation; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'secret.key' in value "${secret.key}".
SecretController.kt
#RestController
class SecretController(
#Value("\${secret.key}") private val secret: String
) {
#GetMapping("/secret")
fun secret(): String {
return secret
}
}
.spring-boot-devtools.properties
secret.key: secret-asdf
In an older application (SpringBoot 2.1) this works fine. I know they changed the path with SpringBoot 2.2, but kept the old path as backwards compatibility - I tried both without success. I also upgraded to 2.3.1, but this does not help either.
Any ideas on how to read properties in integration tests with SpringBoot >= 2.2?

This isn't a bug but a conscious change, see https://github.com/spring-projects/spring-boot/issues/5307
You shouldn't rely on devtools to configure your tests this way.

Related

Upgrading to Springboot 3: 'org.springframework.hateoas.server.LinkRelationProvider' that could not be found

I am upgrading my springboot app from 2.7.x to 3.0.0, I have made required changes for jakartaEE. When starting my app I am getting the below error
{"#timestamp":"2023-01-03T15:28:01.149Z","#version":"1","message":"\r\n\r\n***************************\r\nAPPLICATION
FAILED TO
START\r\n***************************\r\n\r\nDescription:\r\n\r\nParameter
1 of method collectionModelContentConverter in
org.springdoc.core.configuration.SpringDocHateoasConfiguration
required a bean of type
'org.springframework.hateoas.server.LinkRelationProvider' that could
not be found.\r\n\r\n\r\nAction:\r\n\r\nConsider defining a bean of
type 'org.springframework.hateoas.server.LinkRelationProvider' in your
configuration.\r\n","logger_name":"org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter","thread_name":"main","level":"ERROR","level_value":40000}
I am using hateos lib for representation in my app, Also I am using springdoc-openapi-starter-webmvc-ui for swagger UI.
When I disable configuration for api-docs springdoc.api-docs.enabled=false the above error go away but my swagger page is also gone, which definitely means this is not the solution.
Could someone please guide me why this bean is not getting found with springboot 3 while it was all working before with springboot2.7.x and java-11.
here is my pom.xml https://gist.github.com/bruce2019/c93a16dc20f7edb7d922b1e284f39d15
Thanks in advance
You can use springdoc-openapi-starter-webmvc-ui with 2.0.2 version
Add "/v3/api-docs/**" to mvcMatchers in WebSecurityCustomizer

Spring expression fails after upgrade to SpringBoot 2.2

I had an application which was on SpringBoot 1.5.9. I recently upgraded to SpringBoot 2.2. I had a bunch of issues which I fixed, and now the app starts up without errors.
However, I have a #PreAuthorize on my controller methods which seems to be failing:
#PreAuthorize("not #appConfig.getSecEnabled() or hasRole('ROLE')") public void someMethod() {}
java.lang.IllegalArgumentException: Failed to evaluate expression 'not #appConfig.getSecEnabled() or hasRole('ROLE')'
What has changed in 2.2 that could cause this?

Why does my Oracle DataSource have a replay error only when unit testing?

My Spring Boot 1.5.17 (Spring 4.3.20) server works fine with bootRun or when deployed.
However, I have an Oracle DataSource that fails only when unit-testing:
java.lang.AssertionError: Server is sending a non-null replay context
but our replayModes=[]
A Google Search for this error doesn't have exact results.
I am able to unit-test with a different Oracle database.
I get the error with a full Application testing context
#RunWith(SpringRunner.class)
#SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = { TestingUserConfiguration.class, Application.class }
)
As well as just the single DataSource config and Service
#RunWith(SpringRunner.class)
#SpringBootTest(classes = {
MyDbConfig.class,
MyService.class
})
Both oracle.jdbc.pool.OracleDataSource and oracle.jdbc.replay.OracleConnectionPoolDataSourceImpl have the error.
UCP makes no difference.
OracleConnectionPoolDataSourceImpl has a different error:
java.sql.SQLException: Unsupported feature
Tried upgrading from OJDBC 12.2 to 2018.3, no difference.
Has anyone seen this error before?
Any ideas on why it only appears with Spring unit tests?
After much head scratching, I found that doing this in the test suite:
static {
ClassLoader.getSystemClassLoader().setPackageAssertionStatus("oracle.jdbc.driver", false);
}
Solved My issue, I am sure this is a bug in the oracle.jdbc.driver.T4CTTIfun class
You can add "-da:oracle..." to your JAVA_TOOL_OPTIONS to turn off oracle asserts. To get around this.
Trent

JerseyTest and Spring and creating a ServletContext

I'm working on migrating from Jersey 1.16 to Jersey 2.7. I have the application running and working, but I'm having trouble with the tests.
Some things to note:
The application uses Spring and is configured by a class, ApplicationConfiguration.class.
The application does not have a web.xml - the Jersey servlet and filters are configured programmatically.
I am using the jersey-spring3 library.
Related to using the jersey-spring3 library, I have to add a workaround to my onStartup method
// The following line is required to avoid having jersey-spring3 registering it's own Spring root context.
// https://java.net/jira/browse/JERSEY-2038
servletContext.setInitParameter("contextConfigLocation", "");
Here's the issue:
When the test is starting up, SpringComponentProvider, tries to initialize Spring with a dangerous assumption that I can't figure out how to correct - xml based configuration. Looking at the code, the trouble is this block
ServletContext sc = locator.getService(ServletContext.class);
if(sc != null) {
// servlet container
ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
} else {
// non-servlet container
ctx = createSpringContext();
}
Running a JUnit test, ServletContext is null, and createSpringContext is called.
Here's the question:
Is there a way to run a test and specify a ServletContext/ServletContainer?
I believe this issue is covered by https://java.net/jira/browse/JERSEY-2259.
In short: they removed this functionality from Jersey 2.x and are treating it as a Feature Request (instead of regression) so it's not considered a high-priority item.

Grails - using Spring el expressions in Spring 3.1's #Cacheable

We're planning to use the Spring 3.1 cache abstraction instead of the Grails Spring cache plugin. I've experimented with it locally, but an issue occurred when using Spring el expressions like
#Cacheable(value = 'dashboardCache', key = 'sessionStorageService.getUser()', condition = 'sessionStorageService.getUser() != null')
public List<BusinessDashboard> getUserDashboards(String serverName, SessionStorageService sessionStorageService) { ... }
the following error occurs when executing the integration test case
EL1008E:(pos 0): Field or property 'sessionStorageService' cannot be found on object of type 'org.springframework.cache.interceptor.CacheExpressionRootObject'
org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'sessionStorageService' cannot be found on object of type 'org.springframework.cache.interceptor.CacheExpressionRootObject'
I assume this behavior is caused by missing debug information - thus my question:
Can Spring el expressions be enabled in Grails apps, or is there any parameter to the compilation process to tell Grails to keep debug symbols in the class files?
(we're running on Grails 2.0.1)
you should try '#sessionStorageService.getUser()'

Resources