Skip to content

Commit c402680

Browse files
committed
Polish
1 parent ccca943 commit c402680

File tree

1 file changed

+28
-35
lines changed

1 file changed

+28
-35
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/CustomHibernateJpaAutoConfigurationTests.java

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,13 @@
2323

2424
import javax.sql.DataSource;
2525

26-
import org.junit.After;
2726
import org.junit.Test;
2827

28+
import org.springframework.boot.autoconfigure.AutoConfigurations;
2929
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
30-
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
3130
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
32-
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
3331
import org.springframework.boot.autoconfigure.orm.jpa.test.City;
34-
import org.springframework.boot.test.util.TestPropertyValues;
35-
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
32+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3633
import org.springframework.context.annotation.Bean;
3734
import org.springframework.context.annotation.Configuration;
3835
import org.springframework.orm.jpa.vendor.Database;
@@ -44,49 +41,45 @@
4441
import static org.mockito.Mockito.mock;
4542

4643
/**
47-
* Tests for {@link HibernateJpaAutoConfiguration}.
44+
* Additional tests for {@link HibernateJpaAutoConfiguration}.
4845
*
4946
* @author Dave Syer
5047
* @author Phillip Webb
5148
* @author Eddú Meléndez
49+
* @author Stephane Nicoll
5250
*/
5351
public class CustomHibernateJpaAutoConfigurationTests {
5452

55-
protected AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
53+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
54+
.withUserConfiguration(TestConfiguration.class)
55+
.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class,
56+
HibernateJpaAutoConfiguration.class));
5657

57-
@After
58-
public void close() {
59-
this.context.close();
60-
}
6158

6259
@Test
63-
public void testNamingStrategyDelegatorTakesPrecedence() {
64-
TestPropertyValues
65-
.of("spring.jpa.properties.hibernate.ejb.naming_strategy_delegator:"
66-
+ "org.hibernate.cfg.naming.ImprovedNamingStrategyDelegator");
67-
this.context.register(TestConfiguration.class,
68-
EmbeddedDataSourceConfiguration.class,
69-
PropertyPlaceholderAutoConfiguration.class,
70-
HibernateJpaAutoConfiguration.class);
71-
this.context.refresh();
72-
JpaProperties bean = this.context.getBean(JpaProperties.class);
73-
Map<String, String> hibernateProperties = bean
74-
.getHibernateProperties("create-drop");
75-
assertThat(hibernateProperties.get("hibernate.ejb.naming_strategy")).isNull();
60+
public void namingStrategyDelegatorTakesPrecedence() {
61+
this.contextRunner
62+
.withPropertyValues(
63+
"spring.jpa.properties.hibernate.ejb.naming_strategy_delegator:"
64+
+ "org.hibernate.cfg.naming.ImprovedNamingStrategyDelegator"
65+
).run((context) -> {
66+
JpaProperties bean = context.getBean(JpaProperties.class);
67+
Map<String, String> hibernateProperties = bean
68+
.getHibernateProperties("create-drop");
69+
assertThat(hibernateProperties.get("hibernate.ejb.naming_strategy")).isNull();
70+
});
7671
}
7772

7873
@Test
79-
public void testDefaultDatabaseForH2() throws Exception {
80-
TestPropertyValues.of("spring.datasource.url:jdbc:h2:mem:testdb",
81-
"spring.datasource.initialize:false").applyTo(this.context);
82-
this.context.register(TestConfiguration.class, DataSourceAutoConfiguration.class,
83-
PropertyPlaceholderAutoConfiguration.class,
84-
HibernateJpaAutoConfiguration.class);
85-
this.context.refresh();
86-
HibernateJpaVendorAdapter bean = this.context
87-
.getBean(HibernateJpaVendorAdapter.class);
88-
Database database = (Database) ReflectionTestUtils.getField(bean, "database");
89-
assertThat(database).isEqualTo(Database.H2);
74+
public void defaultDatabaseForH2() {
75+
this.contextRunner.withPropertyValues(
76+
"spring.datasource.url:jdbc:h2:mem:testdb",
77+
"spring.datasource.initialize:false").run((context) -> {
78+
HibernateJpaVendorAdapter bean = context
79+
.getBean(HibernateJpaVendorAdapter.class);
80+
Database database = (Database) ReflectionTestUtils.getField(bean, "database");
81+
assertThat(database).isEqualTo(Database.H2);
82+
});
9083
}
9184

9285
@Configuration

0 commit comments

Comments
 (0)