From b8eec2a8a42a9ba7bee8fa92c10f99bd216b97b5 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Tue, 29 Aug 2023 09:23:58 +0800 Subject: [PATCH] Include JdbcClientAutoConfiguration in @JdbcTest and @DataJpaTest slices See gh-37122 --- ...re.data.jdbc.AutoConfigureDataJdbc.imports | 1 + ...toconfigure.jdbc.AutoConfigureJdbc.imports | 1 + ...igure.orm.jpa.AutoConfigureDataJpa.imports | 1 + .../jdbc/ExampleEntityRowMapper.java | 36 +++++++++++ .../jdbc/ExampleJdbcClientRepository.java | 62 +++++++++++++++++++ .../autoconfigure/jdbc/ExampleRepository.java | 14 ----- .../jdbc/JdbcTestIntegrationTests.java | 24 ++++++- .../orm/jpa/DataJpaTestIntegrationTests.java | 11 +++- 8 files changed, 133 insertions(+), 17 deletions(-) create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleEntityRowMapper.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleJdbcClientRepository.java diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.test.autoconfigure.data.jdbc.AutoConfigureDataJdbc.imports b/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.test.autoconfigure.data.jdbc.AutoConfigureDataJdbc.imports index 2fc2a1e54a..eb4b3faada 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.test.autoconfigure.data.jdbc.AutoConfigureDataJdbc.imports +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.test.autoconfigure.data.jdbc.AutoConfigureDataJdbc.imports @@ -3,6 +3,7 @@ org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfigurati org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration +org.springframework.boot.autoconfigure.jdbc.JdbcClientAutoConfiguration org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureJdbc.imports b/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureJdbc.imports index 5aa3ad940c..480dcff0e7 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureJdbc.imports +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureJdbc.imports @@ -2,6 +2,7 @@ org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration +org.springframework.boot.autoconfigure.jdbc.JdbcClientAutoConfiguration org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureDataJpa.imports b/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureDataJpa.imports index ba99875857..83465fdeba 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureDataJpa.imports +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureDataJpa.imports @@ -3,6 +3,7 @@ org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration +org.springframework.boot.autoconfigure.jdbc.JdbcClientAutoConfiguration org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleEntityRowMapper.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleEntityRowMapper.java new file mode 100644 index 0000000000..5897f26716 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleEntityRowMapper.java @@ -0,0 +1,36 @@ +/* + * Copyright 2012-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.jdbc; + +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.springframework.jdbc.core.RowMapper; + +/** + * @author Stephane Nicoll + */ +class ExampleEntityRowMapper implements RowMapper { + + @Override + public ExampleEntity mapRow(ResultSet rs, int rowNum) throws SQLException { + int id = rs.getInt("id"); + String name = rs.getString("name"); + return new ExampleEntity(id, name); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleJdbcClientRepository.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleJdbcClientRepository.java new file mode 100644 index 0000000000..1140090d4b --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleJdbcClientRepository.java @@ -0,0 +1,62 @@ +/* + * Copyright 2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.jdbc; + +import java.util.Collection; + +import jakarta.transaction.Transactional; + +import org.springframework.jdbc.core.simple.JdbcClient; +import org.springframework.stereotype.Repository; + +/** + * Example repository used with {@link JdbcClient JdbcClient} and + * {@link JdbcTest @JdbcTest} tests. + * + * @author Yanming Zhou + */ +@Repository +public class ExampleJdbcClientRepository { + + private static final ExampleEntityRowMapper ROW_MAPPER = new ExampleEntityRowMapper(); + + private final JdbcClient jdbcClient; + + public ExampleJdbcClientRepository(JdbcClient jdbcClient) { + this.jdbcClient = jdbcClient; + } + + @Transactional + public void save(ExampleEntity entity) { + this.jdbcClient.sql("insert into example (id, name) values (:id, :name)") + .param("id", entity.getId()) + .param("name", entity.getName()) + .update(); + } + + public ExampleEntity findById(int id) { + return this.jdbcClient.sql("select id, name from example where id =:id") + .param("id", id) + .query(ROW_MAPPER) + .single(); + } + + public Collection findAll() { + return this.jdbcClient.sql("select id, name from example").query(ROW_MAPPER).list(); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleRepository.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleRepository.java index 9ac32aeba4..0f8c44e3bf 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleRepository.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleRepository.java @@ -16,14 +16,11 @@ package org.springframework.boot.test.autoconfigure.jdbc; -import java.sql.ResultSet; -import java.sql.SQLException; import java.util.Collection; import jakarta.transaction.Transactional; import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; /** @@ -55,15 +52,4 @@ public class ExampleRepository { return this.jdbcTemplate.query("select id, name from example", ROW_MAPPER); } - static class ExampleEntityRowMapper implements RowMapper { - - @Override - public ExampleEntity mapRow(ResultSet rs, int rowNum) throws SQLException { - int id = rs.getInt("id"); - String name = rs.getString("name"); - return new ExampleEntity(id, name); - } - - } - } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestIntegrationTests.java index 9cf7781d3b..44d6fc2ed2 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestIntegrationTests.java @@ -29,6 +29,7 @@ import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfigurati import org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration; import org.springframework.context.ApplicationContext; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.simple.JdbcClient; import org.springframework.test.context.TestPropertySource; import static org.assertj.core.api.Assertions.assertThat; @@ -39,12 +40,16 @@ import static org.springframework.boot.test.autoconfigure.AutoConfigurationImpor * Integration tests for {@link JdbcTest @JdbcTest}. * * @author Stephane Nicoll + * @author Yanming Zhou */ @JdbcTest @TestPropertySource( properties = "spring.sql.init.schemaLocations=classpath:org/springframework/boot/test/autoconfigure/jdbc/schema.sql") class JdbcTestIntegrationTests { + @Autowired + private JdbcClient jdbcClient; + @Autowired private JdbcTemplate jdbcTemplate; @@ -54,13 +59,30 @@ class JdbcTestIntegrationTests { @Autowired private ApplicationContext applicationContext; + @Test + void testJdbcClient() { + ExampleJdbcClientRepository repository = new ExampleJdbcClientRepository(this.jdbcClient); + repository.save(new ExampleEntity(1, "John")); + ExampleEntity entity = repository.findById(1); + assertThat(entity.getId()).isOne(); + assertThat(entity.getName()).isEqualTo("John"); + Collection entities = repository.findAll(); + assertThat(entities).hasSize(1); + entity = entities.iterator().next(); + assertThat(entity.getId()).isOne(); + assertThat(entity.getName()).isEqualTo("John"); + } + @Test void testJdbcTemplate() { ExampleRepository repository = new ExampleRepository(this.jdbcTemplate); repository.save(new ExampleEntity(1, "John")); + ExampleEntity entity = repository.findById(1); + assertThat(entity.getId()).isOne(); + assertThat(entity.getName()).isEqualTo("John"); Collection entities = repository.findAll(); assertThat(entities).hasSize(1); - ExampleEntity entity = entities.iterator().next(); + entity = entities.iterator().next(); assertThat(entity.getId()).isOne(); assertThat(entity.getName()).isEqualTo("John"); } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestIntegrationTests.java index 277677ab0f..f1f84cf1fe 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestIntegrationTests.java @@ -28,6 +28,7 @@ import org.springframework.boot.testcontainers.service.connection.ServiceConnect import org.springframework.context.ApplicationContext; import org.springframework.data.repository.config.BootstrapMode; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.simple.JdbcClient; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -39,6 +40,7 @@ import static org.springframework.boot.test.autoconfigure.AutoConfigurationImpor * @author Phillip Webb * @author Andy Wilkinson * @author Scott Frederick + * @author Yanming Zhou */ @DataJpaTest class DataJpaTestIntegrationTests { @@ -46,6 +48,9 @@ class DataJpaTestIntegrationTests { @Autowired private TestEntityManager entities; + @Autowired + private JdbcClient jdbcClient; + @Autowired private JdbcTemplate jdbcTemplate; @@ -72,8 +77,10 @@ class DataJpaTestIntegrationTests { Long id = this.entities.persistAndGetId(new ExampleEntity("spring", "123"), Long.class); this.entities.flush(); assertThat(id).isNotNull(); - String reference = this.jdbcTemplate.queryForObject("SELECT REFERENCE FROM EXAMPLE_ENTITY WHERE ID = ?", - String.class, id); + String sql = "SELECT REFERENCE FROM EXAMPLE_ENTITY WHERE ID = ?"; + String reference = this.jdbcTemplate.queryForObject(sql, String.class, id); + assertThat(reference).isEqualTo("123"); + reference = this.jdbcClient.sql(sql).param(id).query(String.class).single(); assertThat(reference).isEqualTo("123"); }