Advertisement
Guest User

Untitled

a guest
Apr 4th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. package com.ibm.developer.stormtracker;
  2.  
  3. import static org.assertj.core.api.Assertions.assertThat;
  4.  
  5. import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
  6. import org.junit.jupiter.api.Order;
  7. import org.junit.jupiter.api.Test;
  8. import org.junit.jupiter.api.TestMethodOrder;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.boot.test.util.TestPropertyValues;
  11. import org.springframework.context.ApplicationContextInitializer;
  12. import org.springframework.context.ConfigurableApplicationContext;
  13. import org.springframework.test.context.ContextConfiguration;
  14. import org.springframework.test.context.TestPropertySource;
  15. import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
  16. import org.testcontainers.containers.PostgreSQLContainer;
  17. import org.testcontainers.junit.jupiter.Container;
  18. import org.testcontainers.junit.jupiter.Testcontainers;
  19.  
  20. /*
  21. * An alternate example of using Testcontainers for database testing.
  22. * This example directly starts up a Testcontainer and the pulls the
  23. * relevant info on connecting to the container from it.
  24. *
  25. * @author William.Korando@ibm.com
  26. *
  27. */
  28. @Testcontainers
  29. @SpringJUnitConfig
  30. @ContextConfiguration(classes = {
  31. StormTrackerApplication.class }, initializers = ITStormRepoAlternate.Initializer.class)
  32. @TestPropertySource("classpath:application.properties")
  33. @TestMethodOrder(OrderAnnotation.class)
  34. public class ITStormRepoAlternate {
  35.  
  36. public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
  37. @Override
  38. public void initialize(ConfigurableApplicationContext applicationContext) {
  39. TestPropertyValues.of("spring.datasource.url=" + container.getJdbcUrl(), //Pull from the container the JDBC connection URL
  40. "spring.datasource.username=" + container.getUsername(), //Pull from the container the username to connect to the containerized database (default is "test")
  41. "spring.datasource.password=" + container.getPassword(), //Pull from the container the password to connect to the containerized database (default is "test")
  42. "spring.jpa.properties.hibernate.hbm2ddl.import_files=data.sql", //
  43. "spring.jpa.hibernate.ddl-auto=create-drop")
  44. .applyTo(applicationContext);
  45. }
  46. }
  47.  
  48. @Container
  49. private static PostgreSQLContainer container = new PostgreSQLContainer("postgres:11.2");//Can be an arbitrary image name and tag
  50.  
  51. @Autowired
  52. private StormRepo repo;
  53.  
  54. @Test
  55. @Order(1)
  56. public void testReadFromStormsTable() {
  57. assertThat(repo.count()).isEqualTo(2);
  58. }
  59.  
  60. @Test
  61. public void testWriteToStormsTable() {
  62. Storm savedStorm = repo.save(new Storm("03-17-2019", "03-20-2019", "South Atlantic", "Knoxville, Tennesee",
  63. "Tropical Depression", 3));
  64. assertThat(savedStorm.getId()).isEqualTo(12);
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement