mercredi 28 août 2019

How to use default property file to set fields in a test object instead of using ReflectionTestUtils

I am writing a test for a service in springboot and used @InjectMocks for creating the test object. In the test object, there are a lot of fields which are being set from properties file using @Value. I want to set these fields on the test object directly from the properties file instead of ReflectionTestUtils as there are many fields.

Inside Service Class :

@Value("#{${field1}}")
private Integer field1;

@Value("#{${field2}}")
private Double field2;

Inside test class

    @RunWith(SpringRunner.class)
    @ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class)
    @TestPropertySource(properties = {
            "feild1=100",
            "feild2=1.9"
    })
    public class ServiceTest{

    @InjectMocks
    Service serviceObj;

    @Before
    public void setup(){
        ReflectionTestUtils.setField(serviceObj , "field1" , 100);
        ReflectionTestUtils.setField(serviceObj , "field2" , 1.9);
    }

I want to set all the fields directly from properties file in the unit test as spring is doing in the main app.





Aucun commentaire:

Enregistrer un commentaire