2

I'm trying to pick up some config from application.properties and then override it in the unit test. However, the unit test always sets the schemaPath property to null.

This is what I have:

Service:

@Service
public class XmlValidator {

    @Value("${schema.location}")
    private String schemaPath;

    public void validateXml(String fileName) {
        String schemaPath = null;
        if (fileName.contains("Reference")) {
            schemaPath = schemaPath;
        }
    }
}

application.properties and application-test.properties:

schema.location = /schema/Schema_Reference_0.1.xsd

Unit test:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = XmlValidator.class)
@TestPropertySource(locations="classpath:config/application-test.properties")
public class XmlValidatorTest {

    private XmlValidator validator = new XmlValidator();

    @Test
    public void testSchema() throws Exception {
        validator.validateXml("resources/xmlFile.xml");
    }
}
maloney
  • 1,633
  • 3
  • 26
  • 49
  • 1
    You are creating a new XmlValidator. If you want the properties to be replaced you have to autowire the validator. – alfcope Jan 29 '18 at 15:17
  • you are doing `private XmlValidator validator = new XmlValidator();` it will not use spring to initialise validator. instead use ```@Autowired private XmlValidator validator;``` if you use new XmlValidator(); the value will never be populated even in case of application, leave alone test. – best wishes Jan 29 '18 at 16:10

2 Answers2

1

Edit

Your issue seems very simple to fix, and it may not have anything to do with the profiles. Check the following line

private XmlValidator validator = new XmlValidator();

This is not letting Spring autowire the spring bean as you're creating object by calling constructor. It should be done as below

@Autowired
private XmlValidator validator;

This will let Spring do it's magic and you'll get an injected bean.

Profile related issue

application-test.properties will be picked up only when test profile is active.

Check the active profiles printed in logs when test starts. It should show something like below

The following profiles are active: test,dev,local

Where test, dev, local are profiles that your spring app uses. If these profiles are active then following properties files will be picked up by spring.

application-test.properties
application-dev.properties
application-local.properties
#Similarly *.yml files

#Following are unconditional
bootstrap.properties
application.properties

Check this documentation

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-profile-specific-properties

It would probably be easier to put this test related properties in src/test/resources/application.properties without profile suffix. If you do require profile test then you would have to activate it by using either environment variables or by modifying application.properties file using following properties

spring.profiles.include=test
or
spring.profiles.active=test
Community
  • 1
  • 1
11thdimension
  • 10,333
  • 4
  • 33
  • 71
  • I tried renaming to application.properties but still same result – maloney Jan 29 '18 at 15:34
  • :) well I think I went overboard with a simple issue, you're using `XmlValidator = new XmlValidator()` this does not allow Spring to create bean. It should be wired by Spring check my answer, edited. – 11thdimension Jan 29 '18 at 15:46
0

application.properties file content should be:

schema.location=/schema/Schema_Reference_0.1.xsd

as you can see no spaces near the '=' sign

It could be the application.properties file used in application not visible in text context. You should check this either.

P_M
  • 2,723
  • 4
  • 29
  • 62