BAEL 3234 - Add missing code snippets (#9285)
This commit is contained in:
parent
e264ffd4bc
commit
c72b2846d5
|
@ -7,7 +7,7 @@ import org.springframework.context.annotation.ComponentScan;
|
|||
import com.baeldung.configurationproperties.ConfigProperties;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class, Database.class })
|
||||
@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class})
|
||||
public class ConfigPropertiesDemoApplication {
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer())
|
||||
|
|
|
@ -1,33 +0,0 @@
|
|||
package com.baeldung.properties;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "database")
|
||||
public class Database {
|
||||
|
||||
private String url;
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -14,8 +14,4 @@
|
|||
<constructor-arg value="${key.something}"/>
|
||||
</bean>
|
||||
|
||||
<bean id="dataSource" class="com.baeldung.configurationproperties.Database">
|
||||
<property name="url" value="${jdbc.url}" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
jdbc.url=jdbc:postgresql:/localhost:5432
|
||||
database.username=foo
|
||||
database.password=bar
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
database:
|
||||
url: jdbc:postresql:/localhost:5432/instance
|
||||
jdbc:
|
||||
url: jdbc:postresql:/localhost:5432
|
||||
username: foo
|
||||
password: bar
|
||||
secret: foo
|
|
@ -14,17 +14,13 @@ import com.baeldung.properties.AdditionalProperties;
|
|||
import com.baeldung.properties.ConfigPropertiesDemoApplication;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = {ConfigPropertiesDemoApplication.class, DatabaseConfigPropertiesApp.class})
|
||||
@TestPropertySource(locations = {"classpath:configprops-test.properties", "classpath:database-test.properties"})
|
||||
@SpringBootTest(classes = {ConfigPropertiesDemoApplication.class})
|
||||
@TestPropertySource(locations = {"classpath:configprops-test.properties"})
|
||||
public class ConfigPropertiesIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ConfigProperties properties;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("dataSource")
|
||||
private Database databaseProperties;
|
||||
|
||||
@Autowired
|
||||
private AdditionalProperties additionalProperties;
|
||||
|
||||
|
@ -59,10 +55,4 @@ public class ConfigPropertiesIntegrationTest {
|
|||
Assert.assertTrue(additionalProperties.getMax() == 100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDatabasePropertyQueriedthenReturnsProperty() {
|
||||
Assert.assertTrue(databaseProperties.getUrl().equals("jdbc:postgresql:/localhost:5432"));
|
||||
Assert.assertTrue(databaseProperties.getUsername().equals("foo"));
|
||||
Assert.assertTrue(databaseProperties.getPassword().equals("bar"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,26 +4,28 @@ import org.junit.Assert;
|
|||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.properties.ConfigPropertiesDemoApplication;
|
||||
import com.baeldung.properties.Database;
|
||||
import com.baeldung.configurationproperties.Database;
|
||||
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = ConfigPropertiesDemoApplication.class)
|
||||
@TestPropertySource("classpath:application.properties")
|
||||
@SpringBootTest(classes = DatabaseConfigPropertiesApp.class)
|
||||
@TestPropertySource("classpath:database-test.properties")
|
||||
public class DatabasePropertiesIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("dataSource")
|
||||
private Database database;
|
||||
|
||||
@Test
|
||||
public void testDatabaseProperties() {
|
||||
Assert.assertNotNull(database);
|
||||
Assert.assertEquals("jdbc:postgresql:/localhost:5432/instance", database.getUrl());
|
||||
Assert.assertEquals("jdbc:postgresql:/localhost:5432", database.getUrl());
|
||||
Assert.assertEquals("foo", database.getUsername());
|
||||
Assert.assertEquals("bar", database.getPassword());
|
||||
}
|
||||
|
|
|
@ -3,6 +3,3 @@ spring.properties.refreshDelay=1000
|
|||
spring.config.location=file:extra.properties
|
||||
spring.main.allow-bean-definition-overriding=true
|
||||
|
||||
database.url=jdbc:postgresql:/localhost:5432/instance
|
||||
database.username=foo
|
||||
database.password=bar
|
Loading…
Reference in New Issue