Reputation: 773
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);
}
}
Upvotes: 2
Views: 10456
Reputation: 1
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
Upvotes: 0
Reputation: 2900
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());
}
}
Upvotes: 4