Advertisement
Guest User

Untitled

a guest
Apr 12th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. package com.example.demo;
  2.  
  3. import net.ttddyy.dsproxy.asserts.ProxyTestDataSource;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.BeansException;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.beans.factory.config.BeanPostProcessor;
  9. import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
  10. import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
  11. import org.springframework.boot.test.context.TestConfiguration;
  12. import org.springframework.context.annotation.Bean;
  13. import org.springframework.jdbc.core.JdbcTemplate;
  14. import org.springframework.test.context.junit4.SpringRunner;
  15. import org.springframework.transaction.annotation.Propagation;
  16. import org.springframework.transaction.annotation.Transactional;
  17.  
  18. import javax.sql.DataSource;
  19. import java.util.Arrays;
  20. import java.util.List;
  21.  
  22. import static org.assertj.core.api.Assertions.assertThat;
  23.  
  24.  
  25. @DataJpaTest(
  26. properties = {
  27. "spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver",
  28. "spring.datasource.url=jdbc:tc:postgresql:11.2:///database?reWriteBatchedInserts=true",
  29. "spring.jpa.generate-ddl=true"
  30. }
  31. )
  32. @RunWith(SpringRunner.class)
  33. @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
  34. public class PersonRepositoryTest {
  35.  
  36. @Autowired
  37. private PersonRepository personRepository;
  38.  
  39. @Autowired
  40. private JdbcTemplate jdbcTemplate;
  41.  
  42. private static ProxyTestDataSource ds;
  43.  
  44. @TestConfiguration
  45. public static class Configuration {
  46. @Bean
  47. BeanPostProcessor postProcess() {
  48.  
  49. return new BeanPostProcessor() {
  50. @Override
  51. public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  52. if (bean instanceof DataSource) {
  53. ds = new ProxyTestDataSource((DataSource) bean);
  54. return ds;
  55. }
  56. return bean;
  57. }
  58. };
  59. }
  60. }
  61.  
  62. @Test
  63. public void test() {
  64. ds.reset();
  65. personRepository.findAll();
  66. assertThat(ds.getQueryExecutions()).hasSize(1);
  67. }
  68.  
  69. @Test
  70. @Transactional(propagation = Propagation.NOT_SUPPORTED)
  71. public void ttt() {
  72. ds.reset();
  73.  
  74. List<Person> persons = Arrays.asList(create(), create(), create(), create());
  75.  
  76. jdbcTemplate.batchUpdate("insert into person (value) values (?)", persons, 10, (ps, person) -> {
  77. ps.setString(1, person.getValue());
  78. });
  79.  
  80. assertThat(ds.getQueryExecutions()).hasSize(1);
  81. }
  82.  
  83.  
  84. Person create() {
  85. Person p = new Person();
  86. p.setValue("hejsan");
  87. return p;
  88. }
  89.  
  90. }
  91.  
  92. <dependency>
  93. <groupId>org.testcontainers</groupId>
  94. <artifactId>testcontainers</artifactId>
  95. <version>1.11.1</version>
  96. <scope>test</scope>
  97. </dependency>
  98. <dependency>
  99. <groupId>org.testcontainers</groupId>
  100. <artifactId>postgresql</artifactId>
  101. <version>1.11.1</version>
  102. <scope>test</scope>
  103. </dependency>
  104. <dependency>
  105. <groupId>net.ttddyy</groupId>
  106. <artifactId>datasource-assert</artifactId>
  107. <version>1.0</version>
  108. </dependency>
  109. <dependency>
  110. <groupId>net.ttddyy</groupId>
  111. <artifactId>datasource-proxy</artifactId>
  112. <version>1.5.1</version>
  113. </dependency>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement