I\'m using a spring boot app which runs my src/main/resources/config/application.yml.
When I run my test case by :
@RunWith(SpringJUnit4ClassRunner.c
Lu55 Option 1 how to...
Add test only application.yml inside a seperated resources folder.
├── main │ ├── java │ └── resources │ ├── application.yml └── test ├── java └── resources └── application.yml
In this project structure the application.yml under main is loaded if the code under main is running, the application.yml under test is used in a test.
To setup this structure add a new Package folder test/recources if not present.
Eclipse right click on your project -> Properties -> Java Build Path -> Source Tab -> (Dialog ont the rigth side) "Add Folder ..."
Inside Source Folder Selection -> mark test -> click on "Create New Folder ..." button -> type "resources" inside the Textfeld -> Click the "Finish" button.
After pushing the "Finisch" button you can see the sourcefolder {projectname}/src/test/recources (new)
Optional: Arrange folder sequence for the Project Explorer view.
Klick on Order and Export Tab mark and move {projectname}/src/test/recources to bottom.
Apply and Close
!!! Clean up Project !!!
Eclipse -> Project -> Clean ...
Now there is a separated yaml for test and the main application.
You can use @TestPropertySource
to load different properties/yaml file
@TestPropertySource(locations="classpath:test.properties")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class MyIntTest{
}
OR if you want to override only specific properties/yaml you can use
@TestPropertySource(
properties = {
"spring.jpa.hibernate.ddl-auto=validate",
"liquibase.enabled=false"
}
)
Starting with Spring 4.1, We can directly set the property in application.yml using the @TestPropertySource annotation.
@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource(properties = {"yoursection.yourparameter=your_value"})
public MyIntTest
{
//your test methods
}
Just convert your yaml parameters into complete property structure. For example: If content of application.yml is like below
yoursection:
yourparameter:your_value
Then value to go inside the @TestPropertySource will be,
yoursection.yourparameter=your_value
A simple working configuration using
@TestPropertySource and properties
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(properties = {"spring.config.location=classpath:another.yml"})
public class TestClass {
@Test
public void someTest() {
}
}
We can use @SpringBootTest annotation which loads the yml file from src\main\java\com...hence when we execute the unit test, all of the properties are already there in the config properties class.
@RunWith(SpringRunner.class)
@SpringBootTest
public class AddressFieldsTest {
@InjectMocks
AddressFieldsValidator addressFieldsValidator;
@Autowired
AddressFieldsConfig addressFieldsConfig;
...........
@Before
public void setUp() throws Exception{
MockitoAnnotations.initMocks(this);
ReflectionTestUtils.setField(addressFieldsValidator,"addressFieldsConfig", addressFieldsConfig);
}
}
We can use @Value annotation if you have few configs or other wise we can use a config properties class. For e.g
@Data
@Component
@RefreshScope
@ConfigurationProperties(prefix = "address.fields.regex")
public class AddressFieldsConfig {
private int firstName;
private int lastName;
.........