How to configure Spring Boot Application to run on a specific database when launching - spring

I have a Spring MVC Application developed with Spring Boot. This is application is just for learning purposes, by the way.
By default, the app launches and uses a MySQL database. For unit and integration testing, I use an in-memory H2 database and it works perfectly.
So for that, I have two application.properties. One is under /src/main/resources/application.properties.
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost/myDatabase
spring.datasource.username = root
spring.datasource.password = mysql
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.cache=false
The other application.properties in under /src/test/resources/application.properties
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"
spring.datasource.username=sa
spring.datasource.password=sa
Now, I have to use Selenium for automated website testing and I don't want my MySQL database to be populated with test data.
I haven't done this previously in Spring, but I would like my application to work like this:
Launch from terminal my application with certain commands specifying what database it should use. It should launch on localhost:8080
And then, run all Selenium test in localhost:8080. All the data generated with Selenium tests is only kept in memory as long as the application is running
How to do this in a Spring Boot Application using an application.properties or other configuration?

Create a separate properties file named application-test.properties and place it under /src/test/resources. The test database properties (or any other test specific properties) should go here.
On top of your test class, use this annotation #ActiveProfiles("test").
#ActiveProfiles("test")
public class MyTest {
...
}

Spring should do this for you automatically. To run application.properties from src/test/resources when you are running tests because spring runs with "test" profile. If not, add #ActiveProfiles("test") annotation on your test class (and by that I mean the class where you have your tests, not the class under test). If even that doesn't work, you can rename your src/test/resources/application.properties to src/test/resources/application-test.properties and select your profile in your run configuration (there is a field called 'profile'). Reference and more info.

Related

I have to create jks files inside of testing with junit

I'm working with a spring boot project, currently I need to create 2 files before spring boot starts, in the junit test part (when I run the project into the server, it runs the testing part before, and spring boot use these files), so my idea is about create these files from the testing part (junit test), do you have another idea?
this is not working for this case, I need to create the files before it happens
public class xclass(){
//code before spring starts
main method(Springstarts.class);
}
then, one of the problem is about the db2 connection, because this kind of connection in application.properties needs necessary (and from the beginning) the jks files to connect, example:
spring.datasource.url=jdbc:db2:host:port/bdd:sslConnection=true;sslTrustStoreLocation=folder/file1.jks;
spring.datasource.username=userasdf1234
spring.datasource.password=passwordforconnect1234example
so, we need to create jks files before all of this runs, how can I do that?

Run Spring Boot application with test application.properties

I've just installed Cypress to run e2e tests and that requires my Spring Boot app running so it can plug into the browser to run tests.
I already have an application.properties file under test/resources which creates an H2 memory database populated from a test/resources/data.sql to run my unit and integration tests.
I want to start my app using those test resources so I can run Cypress tests with my testing environment, is that poosible?
I tried changing SPRING_PROFILES_ACTIVE env variable to test but it doesn't take my config files under the test folder, what can I do?
test/resources/application.properties is, as far as I understand it, purely for use in (unit/integration/service) tests that live in test/.
Try creating an additional application-test.properties file in main/resources. This should be used when you have the test Spring profile activated (although it might be better to choose another name for the profile to avoid confusion).

Unable to run SQL script to initialize test data for Spring Boot test

I have a Spring Boot 2.3 reactive application with webflux and r2dbc. Normally it runs on MS Sql database. I want unit tests to run on H2. I got to the point when correct database driver is loaded based on which application.properties file is in use (main or test). But I can't figure out how to run SQL scripts to create schema and load data.
Tried the following without success:
schema-XXX.sql, data-XXX.sql, they work fine with non-reactive JPA.
#Sql annotation referring to the *.sql files in resources directory.
ConnectionFactoryInitializer bean located in the same package as application configuration files, but under the test source tree. #Configuration and #Bean are used properly, but the bean is not instantiated. Maybe this is the problem?
Nothing in the log suggests that there was even a failed attempt to execute *.sql files.
Thank you.

How can use an in-memory H2 database when testing my Quarkus application?

I plan to use PostgreSQL as the database for my Quarkus application but I would like the convenience of using H2 in my tests.
Is there a way I can accomplish such a feat?
Quarkus provides the H2DatabaseTestResource which starts an in memory H2 database as part of the test process.
You will need to add io.quarkus:quarkus-test-h2 as a test scoped dependency and annotate your test with #QuarkusTestResource(H2DatabaseTestResource.class).
You will also need to have something like:
quarkus.datasource.url=jdbc:h2:tcp://localhost/mem:test
quarkus.datasource.driver=org.h2.Driver
in src/test/resources/application.properties
In order for the application use PostgreSQL as part of its regular run, quarkus-jdbc-postgresql should be a dependency and
quarkus.datasource.url=jdbc:postgresql://mypostgres:5432
quarkus.datasource.driver=org.postgresql.Driver
should be set in src/main/resources/application.properties
Update
As of version 1.13, Quarkus can launch H2 automatically in dev and test mode when quarkus-jdbc-h2 is on the classpath and no URL configuration is provided.
See this for more information.
You can use the below configurations in the application.properties file to use h2 database
quarkus.datasource.jdbc.url=jdbc:h2:mem:default
quarkus.datasource.driver=org.h2.Driver
quarkus.datasource.username=admin
quarkus.hibernate-orm.database.generation=drop-and-create

Import database provisioning script with JPA

I'm suing Spring Boot with JPA. I tried to create src/main/resources/import.sql script for provisioning users into application properties:
spring.datasource.jndi-name=java:/global/production
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.datasource.data=classpath:import.sql
But unfortunately when I deploy the application the script is not run. Do you know with JPA what is the proper way to create SQL provisioning script?
If you need to initialized your database (if I understand you correctly), all you need to do in your Spring Boot project is just provide file data.sql with your DML scripts in your root classpath location (i.e. in resources dir):
Spring Boot can automatically create the schema (DDL scripts) of your DataSource and initialize it (DML scripts). It loads SQL from the standard root classpath locations: schema.sql and data.sql, respectively.
Source.
Another approach you can find here.
With the #Sql annotation you can specify the path of the file you want to run before the tests. For example:
#TestPropertySource("classpath:application-test.properties")
#Sql("/data.sql")
class MyClassTest
You can read more in here.

Resources