Advertisement
Guest User

Untitled

a guest
Mar 27th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 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.  
  17. /*
  18. * A quick and easy way to do database testing if you only depend a base database container.
  19. * @author William.Korando@ibm.com
  20. *
  21. */
  22. @SpringJUnitConfig
  23. @ContextConfiguration(classes = { StormTrackerApplication.class }, initializers = ITStormRepo.Initializer.class)
  24. @TestPropertySource("classpath:application.properties")
  25. //Inherit the application.properties the application would really be using, override/add properties like JDBC URL below
  26. //More demonstration purposes, ideally properties should be managed by environment and not packaged in application artifact
  27. @TestMethodOrder(OrderAnnotation.class)
  28. public class ITStormRepo {
  29.  
  30. public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
  31. @Override
  32. public void initialize(ConfigurableApplicationContext applicationContext) {
  33. TestPropertyValues.of("spring.datasource.url=jdbc:tc:postgresql:11.2://arbitrary/arbitrary", //
  34. // JDBC url must start with "jdbc:tc" followed by type of database you are
  35. // connecting to
  36. "spring.datasource.username=arbitrary", //
  37. "spring.datasource.password=arbitrary", //
  38. "spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver")//
  39. // Must use the ContainerDatabaseDriver which starts up the Docker container, is
  40. // eventually replaced with database appropriate driver
  41. .applyTo(applicationContext);
  42. }
  43. }
  44.  
  45. @Autowired
  46. private StormRepo repo;
  47.  
  48. @Test
  49. @Order(1)
  50. public void testReadFromStormsTable() {
  51. assertThat(repo.count()).isEqualTo(2);
  52. }
  53.  
  54. @Test
  55. public void testWriteToStormsTable() {
  56. Storm savedStorm = repo.save(new Storm("03-17-2019", "03-20-2019", "South Atlantic", "Knoxville, Tennesee",
  57. "Tropical Depression", 3));
  58. assertThat(savedStorm.getId()).isEqualTo(12);
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement