Include JdbcClientAutoConfiguration in @JdbcTest and @DataJpaTest slices
See gh-37122
This commit is contained in:
parent
208dcb9305
commit
b8eec2a8a4
@ -3,6 +3,7 @@ org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfigurati
|
|||||||
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration
|
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration
|
||||||
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
|
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
|
||||||
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
|
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
|
||||||
|
org.springframework.boot.autoconfigure.jdbc.JdbcClientAutoConfiguration
|
||||||
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration
|
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration
|
||||||
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration
|
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration
|
||||||
org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration
|
org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration
|
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration
|
||||||
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
|
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
|
||||||
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
|
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
|
||||||
|
org.springframework.boot.autoconfigure.jdbc.JdbcClientAutoConfiguration
|
||||||
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration
|
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration
|
||||||
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration
|
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration
|
||||||
org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration
|
org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration
|
||||||
|
@ -3,6 +3,7 @@ org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration
|
|||||||
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration
|
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration
|
||||||
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
|
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
|
||||||
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
|
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
|
||||||
|
org.springframework.boot.autoconfigure.jdbc.JdbcClientAutoConfiguration
|
||||||
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration
|
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration
|
||||||
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration
|
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration
|
||||||
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
|
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
|
||||||
|
@ -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<ExampleEntity> {
|
||||||
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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<ExampleEntity> findAll() {
|
||||||
|
return this.jdbcClient.sql("select id, name from example").query(ROW_MAPPER).list();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -16,14 +16,11 @@
|
|||||||
|
|
||||||
package org.springframework.boot.test.autoconfigure.jdbc;
|
package org.springframework.boot.test.autoconfigure.jdbc;
|
||||||
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
import jakarta.transaction.Transactional;
|
import jakarta.transaction.Transactional;
|
||||||
|
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
import org.springframework.jdbc.core.RowMapper;
|
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -55,15 +52,4 @@ public class ExampleRepository {
|
|||||||
return this.jdbcTemplate.query("select id, name from example", ROW_MAPPER);
|
return this.jdbcTemplate.query("select id, name from example", ROW_MAPPER);
|
||||||
}
|
}
|
||||||
|
|
||||||
static class ExampleEntityRowMapper implements RowMapper<ExampleEntity> {
|
|
||||||
|
|
||||||
@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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,7 @@ import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfigurati
|
|||||||
import org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration;
|
import org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.jdbc.core.simple.JdbcClient;
|
||||||
import org.springframework.test.context.TestPropertySource;
|
import org.springframework.test.context.TestPropertySource;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
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}.
|
* Integration tests for {@link JdbcTest @JdbcTest}.
|
||||||
*
|
*
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
|
* @author Yanming Zhou
|
||||||
*/
|
*/
|
||||||
@JdbcTest
|
@JdbcTest
|
||||||
@TestPropertySource(
|
@TestPropertySource(
|
||||||
properties = "spring.sql.init.schemaLocations=classpath:org/springframework/boot/test/autoconfigure/jdbc/schema.sql")
|
properties = "spring.sql.init.schemaLocations=classpath:org/springframework/boot/test/autoconfigure/jdbc/schema.sql")
|
||||||
class JdbcTestIntegrationTests {
|
class JdbcTestIntegrationTests {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private JdbcClient jdbcClient;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private JdbcTemplate jdbcTemplate;
|
private JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
@ -54,13 +59,30 @@ class JdbcTestIntegrationTests {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ApplicationContext applicationContext;
|
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<ExampleEntity> entities = repository.findAll();
|
||||||
|
assertThat(entities).hasSize(1);
|
||||||
|
entity = entities.iterator().next();
|
||||||
|
assertThat(entity.getId()).isOne();
|
||||||
|
assertThat(entity.getName()).isEqualTo("John");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testJdbcTemplate() {
|
void testJdbcTemplate() {
|
||||||
ExampleRepository repository = new ExampleRepository(this.jdbcTemplate);
|
ExampleRepository repository = new ExampleRepository(this.jdbcTemplate);
|
||||||
repository.save(new ExampleEntity(1, "John"));
|
repository.save(new ExampleEntity(1, "John"));
|
||||||
|
ExampleEntity entity = repository.findById(1);
|
||||||
|
assertThat(entity.getId()).isOne();
|
||||||
|
assertThat(entity.getName()).isEqualTo("John");
|
||||||
Collection<ExampleEntity> entities = repository.findAll();
|
Collection<ExampleEntity> entities = repository.findAll();
|
||||||
assertThat(entities).hasSize(1);
|
assertThat(entities).hasSize(1);
|
||||||
ExampleEntity entity = entities.iterator().next();
|
entity = entities.iterator().next();
|
||||||
assertThat(entity.getId()).isOne();
|
assertThat(entity.getId()).isOne();
|
||||||
assertThat(entity.getName()).isEqualTo("John");
|
assertThat(entity.getName()).isEqualTo("John");
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,7 @@ import org.springframework.boot.testcontainers.service.connection.ServiceConnect
|
|||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.data.repository.config.BootstrapMode;
|
import org.springframework.data.repository.config.BootstrapMode;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
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.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||||
@ -39,6 +40,7 @@ import static org.springframework.boot.test.autoconfigure.AutoConfigurationImpor
|
|||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
* @author Scott Frederick
|
* @author Scott Frederick
|
||||||
|
* @author Yanming Zhou
|
||||||
*/
|
*/
|
||||||
@DataJpaTest
|
@DataJpaTest
|
||||||
class DataJpaTestIntegrationTests {
|
class DataJpaTestIntegrationTests {
|
||||||
@ -46,6 +48,9 @@ class DataJpaTestIntegrationTests {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private TestEntityManager entities;
|
private TestEntityManager entities;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private JdbcClient jdbcClient;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private JdbcTemplate jdbcTemplate;
|
private JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
@ -72,8 +77,10 @@ class DataJpaTestIntegrationTests {
|
|||||||
Long id = this.entities.persistAndGetId(new ExampleEntity("spring", "123"), Long.class);
|
Long id = this.entities.persistAndGetId(new ExampleEntity("spring", "123"), Long.class);
|
||||||
this.entities.flush();
|
this.entities.flush();
|
||||||
assertThat(id).isNotNull();
|
assertThat(id).isNotNull();
|
||||||
String reference = this.jdbcTemplate.queryForObject("SELECT REFERENCE FROM EXAMPLE_ENTITY WHERE ID = ?",
|
String sql = "SELECT REFERENCE FROM EXAMPLE_ENTITY WHERE ID = ?";
|
||||||
String.class, 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");
|
assertThat(reference).isEqualTo("123");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user