Load different application.yml in SpringBoot Test

前端 未结 11 1109
天涯浪人
天涯浪人 2020-11-30 22:09

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         


        
相关标签:
11条回答
  • 2020-11-30 22:52

    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.

    0 讨论(0)
  • 2020-11-30 22:54

    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"
            }
    )
    
    0 讨论(0)
  • 2020-11-30 23:01

    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
    
    0 讨论(0)
  • 2020-11-30 23:03

    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() {
        }
    }
    
    0 讨论(0)
  • 2020-11-30 23:06

    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;
        .........
    
    0 讨论(0)
提交回复
热议问题