I am learning Junit 5 and test cases. I am using spring boot version '2.2.6.RELEASE and JUnit 5, in my application, I have a method that processes based on the boolean flag from property file.
\src\main\resources\application.properties
#data base connection properties spring.app.datasource.url=jdbc:mysql://localhost:3306/student_db spring.app.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.app.datasource.username=root spring.datasource.password=root spring.app.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect #additional properties spring.property.name=shrikant spring.property.enable=false database connection properties are used to create the database connection
Datasource.java
@Value("${spring.app.datasource.url}") private String url; @Value("${spring.app.datasource.driver-class-name}") private String className; @Value("${spring.app.datasource.username}") private String userName; @Value("${spring.datasource.password}") private String password; @Value("${spring.app.jpa.properties.hibernate.dialect}") private String dialect; controller class
@RestController public class Controller { @Value("${spring.property.name}") private String name; @Value("${spring.property.enable}") private boolean status; public void validateObject(String surName) { if (status) { # if this flag is true then only process System.out.println("name= " + name); System.out.println("surName= " + surName); } } ControllerTest.java
@SpringBootTest class ControllerTest { @Autowired private Controller controller; @Test void show() { controller.validateObject("sharma"); } by default the flag is false, so every time test case runs it never processes the object. so I tried to create aplication.properties in the test folder
\src\test\resources\application.properties
spring.property.name=vishal spring.property.enable=true but now it's giving me an error that
Could not resolve placeholder 'spring.app.datasource.url' but I don't want to provide DB connection URL, I am not connecting to the database while testing.
Q1 - how to change the value of properties file for test case only.
Q2 - is it mandatory to provide all the keys of \src\main\resources\application.properties is \src\test\resources\application.properties?
I am new in test case, so little explained answers would be welcomed.
Update:- I found that
@SpringBootTest @TestPropertySource(properties = {"spring.property.name=vishal", " spring.property.status=true"}) class ControllerTest { will solve the issue temporarily, by providing keys along with values, but I have a lot of keys, which cannot be provided in such a way.