2

I have a consumer.properties file with the following contents in src/main/resources, and an accompanying Configuration class that loads and stores the file's contents into class member variables:

//consumer.properties file in src/main/resources:

com.training.consumer.hostname=myhost
com.training.consumer.username=myusername
com.training.consumer.password=mypassword

//ConsumerConfig.java

@Configuration
@PropertySource(
        value= {"classpath:consumer.properties"}
        )
@ConfigurationProperties(prefix="com.training.consumer")
public class ConsumerConfig {

    private String hostname;
    private String username;
    private String password;

    public ConsumerConfig() { }

    public String getHostname() {
        return hostname;
    }
    public void setHostname(String hostname) {
        this.hostname = hostname;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "ConsumerConfig [hostname=" + hostname + ", username=" + username + ", password=" + password + "]";
    }
}

I also have a ConfigsService class that autowires the ConsumerConfig class to retrieve the individual properties:

@Component
public class ConfigsService {

    @Autowired
    ConsumerConfig consumerConfig;

    public ConsumerConfig getConsumerConfig() {
        return consumerConfig;
    }

    public void showConfig() {
        consumerConfig.toString();
    }

    public ConsumerConfig getConfig() {
        return consumerConfig;
    }
}

The properties are loaded up just fine when running the ConfigsService's methods. The problem is in the unit tests, where invoking configService.getConfig().getHostname() returns a null value -- even after having created a src/test/resources directory, and adding my consumer.properties file in it:

@TestPropertySource("classpath:consumer.properties")
public class ConfigsServiceTest {

    @Mock
    ConsumerConfig consumerConfig;

    @InjectMocks
    ConfigsService configService;


    @Before
    public void beforeEach() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void someTest() {
        System.out.println(configService.getConfig().getHostname()); //outputs null here -- wth!
        Assert.assertTrue(true);
    }
}
1
  • Hi, are you running this with spring runner, otherwise you will not be spinning up a spring instance during the test? Commented Nov 23, 2018 at 20:50

2 Answers 2

4

you are getting null values because of the mock object. I will suggest using spring runner with context configuration which will load properties in the config class and create the bean.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ConsumerConfig.class, ConfigsService.class})
@TestPropertySource(locations = "classpath:consumer.properties")
public class ConfigsServiceTest {

  @Autowired
  private ConfigsService configsService;

  @Test
  public void someTest() {
    Assert.assertNotNull(configService.getConfig().getHostname());
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

The error may be because in the ConfigsServiceTest class you are establishing that your ConsumerConfig is a Mock and that is why it is not loading your configuration if you remove that code it should work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.