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

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

Related

Cucumber configuration error in IntelliJ: Please annotate a glue class with some context configuration

So I recently started working on Cucumber and have been facing this issue.
This is the hierarchy of my module
As you can see this is submodule in my Spring Boot application (AcceptanceTests), so there are no main methods in it.
This is my CucumberSpringContextConfiguration class
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
#CucumberContextConfiguration
public class CucumberSpringContextConfiguration {
}
This is my CucumberIntegrationTest class
#RunWith(Cucumber.class)
#CucumberOptions(
features = "src\test\resources\feature",
plugin = {"pretty", "html:target\\cucumber"},
glue = "com.#####.########.cucumberspringboot.cucumberglue"
)
public class CucumberIntegrationTest {}
I tried running this code with a main class (src/main/java),the code compiled successfully. But since that is not my requirement I removed it and now I am getting below error -
SEVERE: Exception while executing pickle
java.util.concurrent.ExecutionException:
io.cucumber.core.backend.CucumberBackendException: Please annotate a
glue class with some context configuration.
For example:
#CucumberContextConfiguration
#SpringBootTest(classes = TestConfig.class)
public class CucumberSpringConfiguration { }
Or:
#CucumberContextConfiguration
#ContextConfiguration( ... )
public class CucumberSpringConfiguration { }
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at io.cucumber.core.runtime.Runtime.run(Runtime.java:93)
at io.cucumber.core.cli.Main.run(Main.java:92)
at io.cucumber.core.cli.Main.main(Main.java:34)
Caused by: io.cucumber.core.backend.CucumberBackendException:
Please annotate a glue class with some context configuration.
Please suggest how to achieve this without using main class.
io.cucumber.core.cli.Main.run(Main.java:92) at io.cucumber.core.cli.Main.main(Main.java:34)
This part of the stacktrace shows you are using the Cucumber CLI rather than CucumberIntegrationTest. So you are probably running your feature file through IDEA. Presumably you clicked the green play icon in the gutter of a feature file.
Unfortunately Intelij IDEA makes some assumptions and bad guesses. If you look at the run configuration that was created you'll see that there is a command line with --glue argument that probably points at features. You'll have to change this manually.
It is currently unnecessary to guess the --glue argument and I have already asked the team behind Intelij IDEA to fix this at IDEA-243074 but the issue has gotten zero attention so far. You can upvote it, maybe something happens.
You can also avoid this problem by putting your feature files in a different location e.g. src/test/resources/com/#####/########/cucumberspringboot. Becaus this is the parent package of cucumberglue Ingelij IDEA is less likely to mess things up.
Also note: you are currently using JUnit 4 to run Cucumber. Spring Boot prefers using JUnit 5. You should use the Cucumber JUnit Platform Engine. You can find a minimal example here.

How can I write a #DataJpaTest in a Spring Boot application that uses rsa keys loaded through configuration?

I followed the Spring Boot guides to set up JWT's using spring-boot-starter-oauth2-resource-server, so I've got references to the rsa keys used for signing the JWT's in my application.yml:
rsa:
privateKey: classpath:certs/private.pem
publicKey: classpath:certs/public.pem
This worked great until I tried to write a #DataJpaTest for testing the service layer of the application.
#DataJpaTest
public class FooTest {
#Test
public void test() {
System.out.println();
}
}
That test fails with the error:
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [java.security.interfaces.RSAPublicKey]
at app//org.springframework.boot.context.properties.bind.BindConverter.convert(BindConverter.java:118)
at app//org.springframework.boot.context.properties.bind.BindConverter.convert(BindConverter.java:100)
at app//org.springframework.boot.context.properties.bind.BindConverter.convert(BindConverter.java:92)
at app//org.springframework.boot.context.properties.bind.Binder.bindProperty(Binder.java:459)
at app//org.springframework.boot.context.properties.bind.Binder.bindObject(Binder.java:403)
at app//org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:343)
I know the converters are available somewhere, because the same test runs fine with #SpringBootTest. I think I found them in org.springframework.security.converter.RsaKeyConverters. But I don't know how to register them so they're picked up during the #DataJpaTest.
I don't think those converters should be necessary for the test - FooTest has no dependencies right now.
How can I either set up the #DataJpaTest to work with this recommended spring-boot project setup, or how can I change the project setup so that I can easily write and run #DataJpaTests?
RsaKeyConverters is somehow configured by SecurityAutoConfiguration. #SpringBootTest will consider all the auto configuration and hence it can setup RsaKeyConverters properly. But #DataJpaTest will only consider the auto configuraiotn that are related to testing the JPA stuff and hence it will ignore SecurityAutoConfiguration.
You can use #ImportAutoConfiguration to tell it to also consider SecurityAutoConfiguration. But after that, you will find that although it is considered , it will only be enabled if the spring-boot is started as the servlet mode but #DataJpaTest starts it as the 'none' mode. So you need to configure another property spring.main.web-application-type=servlet to force it to start as servlet mode.
So making these two configuration changes should solve your problem :
#ImportAutoConfiguration(classes = SecurityAutoConfiguration.class)
#DataJpaTest(properties = "spring.main.web-application-type=servlet")
public class FooTest {
}
Have you tried to #MockBean Converter<String, RSAPublicKey> rsaPublicKeyConver; in your test class?

SpringBoot not resolving devtools properties in tests

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.

SpringBoot 1.5 : #SpringBootTest and memory Database

I have a SpringBoot application with spring data/jpa to connect for database.
And a propertie file yml where defined the database connection.
Everything works very well.
I create a test like that :
#ActiveProfiles("dev")
#RunWith(SpringRunner.class)
#SpringBootTest(classes = MyMicroServiceApp.class, webEnvironment=SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MyMicroServiceAppTest {
#Test
public <T> void postConnex() {
//Create Object connexCreate
...
// Create POST
ResponseEntity<Udsaconnex> result1 = this.restTemplate().postForEntity("http://localhost:" + port + "/v1/connex",
connexCreate, Udsaconnex.class);
id = result1.getBody().getIdconnex();
assertEquals(result1.getBody().toString().isEmpty(), false);
}
}
For my test, i have not configured properties for database connection but the test work and i view in console that :
Hibernate: drop table connex if exists
I don't understand why, #SpringBootTest mock database like #DataJpaTest automatically ??
It's possible but i don't find anything about that in spring boot documentation.
Thanks for your help.
If you have application.yml file specifying DB location, then SpringBootTest will obviously use the same configuration and will use your configured DB.
From the title of your question I guess you have an in-memory database in your build dependencies. Spring-boot has some autoconfiguration for certain database (H2, HSQL, Derby) if they are found on the classpath. See this link for a list of the supported databases:
Spring Boot Embedded Database Support

Issue with SpringJUnit4ClassRunner, H2 db inetgration test

I am trying to write an integration test using h2 db, I am getting an error as "error: cannot access BlockJUnit4ClassRunner" at the line #RunWith()
Can any one suggest something please? I have no clue, even If I comment out everything in the test it would not build and give the same error, so I don't think what's in the test that causing the issue
#RunWith(SpringJUnit4ClassRunner.class)
#TransactionConfiguration(defaultRollback = true)
#ContextConfiguration(locations = { "classpath:test-applicationContext.xml" })
public class WfsDBTest extends AbstractTransactionalJUnit4SpringContextTests {}
Please help
thanks
It looks like you use wrong version of JUnit.
Make sure that you use JUnit 4.x and there are no other versions of JUnit in your classpath. Try to use the latest version.

Resources