Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.demo;
- import net.ttddyy.dsproxy.asserts.ProxyTestDataSource;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.BeansException;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.config.BeanPostProcessor;
- import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
- import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
- import org.springframework.boot.test.context.TestConfiguration;
- import org.springframework.context.annotation.Bean;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.test.context.junit4.SpringRunner;
- import org.springframework.transaction.annotation.Propagation;
- import org.springframework.transaction.annotation.Transactional;
- import javax.sql.DataSource;
- import java.util.Arrays;
- import java.util.List;
- import static org.assertj.core.api.Assertions.assertThat;
- @DataJpaTest(
- properties = {
- "spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver",
- "spring.datasource.url=jdbc:tc:postgresql:11.2:///database?reWriteBatchedInserts=true",
- "spring.jpa.generate-ddl=true"
- }
- )
- @RunWith(SpringRunner.class)
- @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
- public class PersonRepositoryTest {
- @Autowired
- private PersonRepository personRepository;
- @Autowired
- private JdbcTemplate jdbcTemplate;
- private static ProxyTestDataSource ds;
- @TestConfiguration
- public static class Configuration {
- @Bean
- BeanPostProcessor postProcess() {
- return new BeanPostProcessor() {
- @Override
- public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
- if (bean instanceof DataSource) {
- ds = new ProxyTestDataSource((DataSource) bean);
- return ds;
- }
- return bean;
- }
- };
- }
- }
- @Test
- public void test() {
- ds.reset();
- personRepository.findAll();
- assertThat(ds.getQueryExecutions()).hasSize(1);
- }
- @Test
- @Transactional(propagation = Propagation.NOT_SUPPORTED)
- public void ttt() {
- ds.reset();
- List<Person> persons = Arrays.asList(create(), create(), create(), create());
- jdbcTemplate.batchUpdate("insert into person (value) values (?)", persons, 10, (ps, person) -> {
- ps.setString(1, person.getValue());
- });
- assertThat(ds.getQueryExecutions()).hasSize(1);
- }
- Person create() {
- Person p = new Person();
- p.setValue("hejsan");
- return p;
- }
- }
- <dependency>
- <groupId>org.testcontainers</groupId>
- <artifactId>testcontainers</artifactId>
- <version>1.11.1</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.testcontainers</groupId>
- <artifactId>postgresql</artifactId>
- <version>1.11.1</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>net.ttddyy</groupId>
- <artifactId>datasource-assert</artifactId>
- <version>1.0</version>
- </dependency>
- <dependency>
- <groupId>net.ttddyy</groupId>
- <artifactId>datasource-proxy</artifactId>
- <version>1.5.1</version>
- </dependency>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement