I have to create jks files inside of testing with junit - spring

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?

Related

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.

Multiple web applications in one tomcat instance start with a properties file from another applications

We have multiple web applications in one tomcat instance on 1 server, all running a spring-boot application inside.
Whenever we start tomcat and it starts to boot up all the spring-boot applications we mostly see that each application might use property files/settings from another application.
What especially happens is that we see it sometimes use the database information from other applications being used, resulting in a database which holds tables from other applications. This is scary since we might start a database migration or something.
We also see that the logs are written to the wrong project log file.
We define these settings using a application.properties like (or sometimes application-test.properties or application-secret.properties):
spring.datasource.username
spring.datasource.password
logging.file.name
Anyone have an idea why this is happening?
We found 2 possible causes for this behavior:
if Tomcat is started in a directory where application property files are present, or where application property files are placed in a /config subdirectory, like a WEB-INF/classes directory, these application property files are used by every Spring Boot application deployed in the tomcat instance. To fix this, make sure the Tomcat start script changes the working directory to a directory not containing application property files.
if the 'startStopThreads' attribute of the Tomcat Engine element in server.xml is set to values higher than 1, Spring Boot applications seem to occasionally and randomly use application property files of other Spring Boot applications deployed in the Tomcat instance. When 'startStopThreads' is set to 1, we don't see this behavior.

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

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.

How to ignore a java file when maven install

I have a #Service class where i'm caching some table data. I don't want those queries to run while building mvn install. Is there a way to ignore the file while building and it only execute when i start the server ?
It's a spring-boot application.
Here is background of my issue. I have initialized the spring boot app from http://start.spring.io/ site, which actually adds dummy application test file with SpringBootTest annotation and default contextLoads() with Test annotation, with an intention to initialize and execute all test cases, which needs to initialize and execute all code base. In my opinion this is not required, as we can have respective Test classes per controller/manager, which will give more controlled environment to hook up your Test setups and executions.
I have removed the default application Test file and included respective test classes for code coverage and quality. This way my beans are not executed at server startup time rather build time.

Resources