-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathAutoConfiguredDatasourceDependsOnTest.java
More file actions
63 lines (53 loc) · 2.1 KB
/
AutoConfiguredDatasourceDependsOnTest.java
File metadata and controls
63 lines (53 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.playtika.testcontainer.mysql;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
import static com.playtika.testcontainer.mysql.MySQLProperties.BEAN_NAME_EMBEDDED_MYSQL;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
@Slf4j
@SpringBootTest(
classes = {
AutoConfiguredDatasourceDependsOnTest.TestConfiguration.class
},
properties = {
"spring.profiles.active=enabled",
"embedded.toxiproxy.proxies.mysql.enabled=true"
}
)
public class AutoConfiguredDatasourceDependsOnTest {
@Autowired
ConfigurableListableBeanFactory beanFactory;
@Autowired
JdbcTemplate jdbcTemplate;
@Test
public void shouldConnectToMySQL() {
assertThat(jdbcTemplate.queryForObject("select version()", String.class)).startsWith("9.5.");
}
@Test
public void shouldSetupDependsOnForAllDataSources() {
String[] beanNamesForType = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, DataSource.class);
assertThat(beanNamesForType)
.as("Auto-configured datasource should be present")
.hasSize(1)
.contains("dataSource");
asList(beanNamesForType).forEach(this::hasDependsOn);
}
private void hasDependsOn(String beanName) {
assertThat(beanFactory.getBeanDefinition(beanName).getDependsOn())
.isNotNull()
.isNotEmpty()
.contains(BEAN_NAME_EMBEDDED_MYSQL);
}
@EnableAutoConfiguration
@Configuration
static class TestConfiguration {
}
}