Gradle test dependencies not loaded in Spring Boot test - java

I am trying to work with p6spy in Spring Boot tests. I have a test class annotated with
#RunWith(SpringRunner.class)
#SpringBootTest
My gradle.build looks like this
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
runtime('com.h2database:h2')
testCompile 'p6spy:p6spy:3.0.0'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
As for the application itself (which runs fine), I added the new datasource to the test-application-context.
spring:
application:
name: persistence
datasource:
url: jdbc:p6spy:h2:mem:persistence;DB_CLOSE_ON_EXIT=FALSE
username: sa
password:
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
jpa:
database: H2
But when I run my test I get this error
java.lang.IllegalStateException: Cannot load driver class: com.p6spy.engine.spy.P6SpyDriver
To me this looks like my dependencies are not loaded. At first, I was using the #DataJpaTest annotation, but this one ignored even my new test-application-context.
Any help appreciated.
EDIT: I got it working bei adding the p6spy dependency manually to the test using IntelliJ. Now I'm sure that my classpath is wrong, but I don't know how to fix it to make it work in Gradle.

The problem is located in my version of IntelliJ. I will file a bug report.
If anyone should have this problem, I added the missing dependencies manually in the project settings. Then it works also from the IDE.

Related

SpringBoot 2 with Flyway: spring.flyway.locations is ignored

I am using Spring Boot 2.2.2 with Flyway 5.2.4 and I tried to configure flyway to use a differente location for the scripts, but spring.flyway.locations=filesystem:db_other/migration/{vendor} neither flyway.locations=filesystem:db_other/migration/{vendor} configurations on application.properties worked.
When running the program, the following exception appear in the log:
FlywayMigrationScriptMissingException: Cannot find migration scripts in: [classpath:db/migration]
I already tried using Spring Boot 2.2.1, 2.2.0, 2.1.11 and Flyway 6.1.0 and 6.1.3, but the result is the same.
The default value for that property is classpath:db/migration as shown here (search for flyway).
Since you're using a different folder in the resources directory you should only need to change "filesystem" to "classpath" in your application.properties value.
Actually if was my fault: as I used to work with just spring (not spring boot) I configured my test class with the annotations #ExtendWith(SpringExtension.class) AND #ContextConfiguration(classes = { MyConfiguration.class }) instead of just use #SpringBootTest. When making this change the test worked.

Spring boot 2.1.1, use postgres config with jpa

I build some simple spring boot project, with jpa/postgres.
But when i debug this project, error say
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
But in my application.properties that have url.
spring.datasource.url=jdbc:postgresql://localhost:5432/oauth?currentSchema=oauth
spring.datasource.username=SOME_USER_NAME
spring.datasource.password=SOME_PASSWORD
spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.hibernate.ddl-auto=validate
#hibernate
spring.jpa.database = postgresql
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
Does anyone know about this?
if validation is trouble than debug don't say like 'url attribute is not specified'.
And My application.properties name 'application-test.properties', and in debug configuration set 'test', and also log say 'the profiles test are currently active'.
It looks like properties is not matching, but log said profile is matched.
It makes me very confuse..
I found problem. Jonathan Johx and TinyOS is right, Thanks for them.
If project built by spring boot 2.1.1, Intellij with postgres, then Override build.gradle and pom.xml.
In Spring-boot 2.1.1 (I still don't know why default value is it)
default setting is
runtime('org.postgresql:postgresql')
but If you want to run right, than must write 'compile' like this.
compile group: 'org.postgresql', name: 'postgresql', version: '42.2.5'
version is not important. just find in maven repo what you want version.

Spring Boot 2.0.0 , DataSourceBuilder not found in autoconfigure jar

We are upgrading our existing Spring Boot (1.5) application to 2.0.0.
We connect with multiple databases and use the org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder class.
I added the dependency:
compile group: 'org.springframework.boot',
name: 'spring-boot-autoconfigure',
version: '2.0.0.RELEASE'
However, I am not able to compile the project: This class (DataSourceBuilder) does not exist in the 2.0.0 version jar.
In order to rule out gradle issues, I manually downloaded the jar and added it to the classpath. This class does not exist in the version.
Also extracted and searched the jar but this class is missing.
Can anyone help me resolve it?
The class was moved to another package. Its FQN is now org.springframework.boot.jdbc.DataSourceBuilder: https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/jdbc/DataSourceBuilder.html
Old class (Spring Boot 1.x)
org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder
New class (Spring Boot 2.x)
import org.springframework.boot.jdbc.DataSourceBuilder;

Managing dependencies with spring boot profiles and Gradle

I created a java project that will use spring boot and Gradle. I would like to configure profiles, for the different environment (development on my local machine, systemtest for integration test on server farm machine etc). I would use h2 in memory database for development environment and SqlServer for systemtest environment. In build.gradle I defined the following dependencies
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-web-services")
compile('org.springframework.boot:spring-boot-starter-actuator')
runtime('com.h2database:h2:1.4.195')
runtime('com.microsoft.sqlserver:mssql-jdbc')
}
I created a application.yml file, application-development.yml and application-systemtest.yml where I would put common properties and environment specific properties. The file application-systemtest.yml defines the connecction parameters for sql server
spring:
datasource:
url: jdbc:sqlserver://<host>,1433;databaseName=MYDB
username: myuser
password: mypass
driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
jpa:
show-sql: true
hibernate:
dialect: org.hibernate.dialect.SQLServer2012Dialect
I would also create an uber-jar and select the profile as a launch parameter, ie
java -Dspring.profiles.active=systemtest -jar <my uber jar>
The development profiles starts fine and I am running on h2 in memory database. When trying systemtest profile, spring boot fails to load contexts and fails. This is caused by spring boot finding h2 dependency and trying to configure datasource defined in application-systemtest.yml
So I modified the build.gradle dependencies closure
def profile = project.findProperty('spring.profiles.active')
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-web-services")
compile('org.springframework.boot:spring-boot-starter-actuator')
if (profile == 'development') {
runtime('com.h2database:h2:1.4.195')
} else {
runtime('com.microsoft.sqlserver:mssql-jdbc')
}
}
This time spring boot start correctly. Don't like very much this solution as I have to handle the profile configuration partly with Gradle. I would like to know if there is a way to configure spring boot so that profile is completely managed within itself, resolving h2 in development environment and sqlserver in systemtest environment, leaving Gradle unaware of spring profiles.
How to solve this problem ?
It wouldn't be advisable to have a different artifact / binary depending on the DB product. Try to configure the datasources / Spring profiles as suggested by #M. Deinum to prevent the datasource to be configured as H2 when using a different DB.

Gradle exported properties dont work with gradle bootRun in Spring

in my Spring Boot project i have the following fragment in gradle which exports gradle properties to Spring Environment.
processResources {
filesMatching("**/application.properties") {
expand(project.properties)
}
}
My application.properties looks like this (snippet)
app.version=${jar.version}
Works pretty well. I can work with the gradle propeties in Spring classes with #value and even can access them in thymeleaf with
th:text="${#environment.getProperty('app.version')}
But now the issue: When i run the same project with "gradle bootRun". I am getting this:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'jar.version' in string value "${jar.version}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174) ~[spring-core-4.3.3.RELEASE.jar:4.3.3.RELEASE]
I assume its something about the way gradle's bootRun works by not using processResources or something like that?
The question would be: how can i get this to work. I dont want to give up using bootRun.
Specify a default value to use when the real one cannot be found?
#Value("${...:defaultValue}")

Categories

Resources