Guest User

Untitled

a guest
Apr 4th, 2019
149
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. *
  26. */
  27. @Testcontainers
  28. @SpringJUnitConfig
  29. @ContextConfiguration(classes = {
  30. StormTrackerApplication.class }, initializers = ITStormRepoAlternate.Initializer.class)
  31. @TestPropertySource("classpath:application.properties")
  32. @TestMethodOrder(OrderAnnotation.class)
  33. public class ITStormRepoAlternate {
  34.  
  35. public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
  36. @Override
  37. public void initialize(ConfigurableApplicationContext applicationContext) {
  38. TestPropertyValues.of("spring.datasource.url=" + container.getJdbcUrl(), //Pull from the container the JDBC connection URL
  39. "spring.datasource.username=" + container.getUsername(), //Pull from the container the username to connect to the containerized database (default is "test")
  40. "spring.datasource.password=" + container.getPassword(), //Pull from the container the password to connect to the containerized database (default is "test")
  41. "spring.jpa.properties.hibernate.hbm2ddl.import_files=data.sql", //
  42. "spring.jpa.hibernate.ddl-auto=create-drop")
  43. .applyTo(applicationContext);
  44. }
  45. }
  46.  
  47. @Container
  48. private static PostgreSQLContainer container = new PostgreSQLContainer("postgres:11.2");//Can be an arbitrary image name and tag
  49.  
  50. @Autowired
  51. private StormRepo repo;
  52.  
  53. @Test
  54. @Order(1)
  55. public void testReadFromStormsTable() {
  56. assertThat(repo.count()).isEqualTo(2);
  57. }
  58.  
  59. @Test
  60. public void testWriteToStormsTable() {
  61. Storm savedStorm = repo.save(new Storm("03-17-2019", "03-20-2019", "South Atlantic", "Knoxville, Tennesee",
  62. "Tropical Depression", 3));
  63. assertThat(savedStorm.getId()).isEqualTo(12);
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment