Using Docker For Testing Legacy Code
Transcript of Using Docker For Testing Legacy Code
![Page 1: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/1.jpg)
USING DOCKER FOR TESTING LEGACY
@alexsotob
![Page 2: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/2.jpg)
@alexsotob2
Alex Soto
Red Hat Engineer www.lordofthejars.com @alexsotob
Who Am I?
![Page 3: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/3.jpg)
@alexsotob3
https://www.manning.com/books/testing-java-microservices
![Page 4: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/4.jpg)
@alexsotob4
Questions
![Page 5: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/5.jpg)
@alexsotob5
ENIAC
![Page 6: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/6.jpg)
@alexsotob
Quality for your Customers
6
![Page 7: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/7.jpg)
@alexsotob
Enable changeRefactor code
Validate functionalityThe right thing, and the thing right
Trust deployments
Testing
7
![Page 8: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/8.jpg)
@alexsoto8
What is Legacy Code?
![Page 9: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/9.jpg)
@alexsotob
Old Code Needs To Be Maintained
9
![Page 10: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/10.jpg)
@alexsotob
Apply Test First Strategies
10
![Page 11: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/11.jpg)
@alexsotob
How To Start?
11
![Page 12: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/12.jpg)
@alexsotob
Characterization Tests
12
![Page 13: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/13.jpg)
@alexsotob
Tests By Division
13
![Page 14: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/14.jpg)
@alexsotob14
![Page 15: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/15.jpg)
@alexsotob
![Page 16: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/16.jpg)
@alexsotob
Testing Containers
docker build -t myorg/myservice:1.0.0 . docker run --rm -ti -p 8080:8080 myorg/myservice:1.0.0
docker-compose up
mvn clean test
docker-compose stop
16
Docker Run
Docker Compose Run
Run tests
Stop Docker Containers
![Page 17: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/17.jpg)
@alexsotob17
![Page 18: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/18.jpg)
@alexsotob
Arquillian Cube DSL
@RunWith(SpringRunner.class) @SpringBootTest(classes = PingPongController.class, webEnvironment = RANDOM_PORT) @ContextConfiguration(initializers = PingPongSpringBootTest.Initializer.class) public class PingPongSpringBootTest {
@ClassRule public static ContainerDslRule redis = new ContainerDslRule("redis:3.2.6") .withPortBinding(6379); @Autowired TestRestTemplate restTemplate;
@Test public void should_get_data_from_redis() { }
18
Spring Boot Test
Custom Initializer
Container Definitionpublic static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
EnvironmentTestUtils.addEnvironment("testcontainers", configurableApplicationContext.getEnvironment(), "spring.redis.host=" + redis.getIpAddress(), "spring.redis.port=" + redis.getBindPort(6379) );
Sets Container Environment
![Page 19: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/19.jpg)
@alexsotob
Arquillian Cube Example@RunWith(Arquillian.class) public class HelloWorldTest {
@ArquillianResource @DockerUrl(containerName = "helloworld", exposedPort = 8080) RequestSpecBuilder requestSpecBuilder;
@Test public void should_receive_ok_message() { RestAssured .given() .spec(requestSpecBuilder.build()) .when() .get() .then() .assertThat().body("status", equalTo("OK")); }
}
19
Arquillian Runner
REST-Assured Integration Environment Resolver
Normal REST-Assured Call
helloworld: image: jonmorehouse/ping-pong ports: - "8080:8080"
src/test/docker/docker-compose.yml
![Page 20: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/20.jpg)
@alexsotob
DEMO
20
![Page 21: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/21.jpg)
@alexsotob21
Persistence Tests
![Page 22: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/22.jpg)
@alexsotob22
APE
![Page 23: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/23.jpg)
@alexsotob
APE SQL example
@Rule public ArquillianPersistenceRule arquillianPersistenceRule = new ArquillianPersistenceRule(); @DbUnit @ArquillianResource RdbmsPopulator rdbmsPopulator;
@Before public void populateData() { // Given: rdbmsPopulator.forUri(jdbcUri).withDriver(Driver.class).withUsername("sa") .withPassword("").usingDataSet("composers.yml") .execute();
}
23
APE JUnit Rule (not necessary with Arquillian Runner)
Set DBUnit usage
Configure Connection and Dataset
Populate
composers:
- id: 1 name: Wolfgang Amadeus Mozart birthdate: 27/1/1756 died: 5/12/1791
![Page 24: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/24.jpg)
@alexsotob
APE SQL example
@After public void clean_database() { // Given: rdbmsPopulator.forUri(jdbcUri).withDriver(Driver.class).withUsername("sa") .withPassword("").usingDataSet("composers.yml") .clean(); }
24
Clean After Test
Clean Database
![Page 25: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/25.jpg)
@alexsotob
Boilerplate Code
Programmatic/Declarative@UsingDataSet/@ShouldMatchDataSet
SQL SupportDBUnit and Flyway
REST API SupportPostman Collections
NoSQL SupportMongoDB, Couchbase, CouchDB, Vault, Redis, Infinispan
Benefits of Arquillian APE
25
![Page 26: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/26.jpg)
@alexsotob26
DEMO
![Page 27: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/27.jpg)
@alexsotob27
Visual Testing
![Page 28: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/28.jpg)
@alexsotob
Selenium
28
![Page 29: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/29.jpg)
@alexsotob
Arquillian Drone
29
![Page 30: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/30.jpg)
@alexsotob
Arquillian Drone example
@RunWith(Arquillian.class) public class LoginScreenTest { @Drone private WebDriver browser; @Test public void should_login_user() { driver.get("www.mypage.com/login"); driver.findElement(By.id("loginForm:login")).click(); } }
30
Arquillian Runner
Injection of WebDriver instance
![Page 31: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/31.jpg)
@alexsotob
Arquillian Graphene
31
![Page 32: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/32.jpg)
@alexsotob
Arquillian Graphene Page Object example
@Location("login.xhtml") public class HomePage {
@Page private UserPage userPage;
@FindBy(id = "submit") private WebElement submitButton;
@FindBy(id = "form") private LoginPopup loginPopup;
// methods
}
32
Location of the page
Another Page Object
Find web element by id
Page fragment
![Page 33: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/33.jpg)
@alexsotob
Arquillian Graphene example
@RunWith(Arquillian.class) public class LoginTest {
@Drone WebDriver webDriver;
@Test public void should_create_a_new_speaker(@InitialPage HomePage homePage) { homePage.login("username", "pasword"); } }
33
Open given page
Use Page Object method
![Page 34: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/34.jpg)
@alexsotob34
Docker and Selenium
![Page 35: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/35.jpg)
@alexsotob
DEMO
35
![Page 36: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/36.jpg)
@alexsotob36
Let’s Wind Down
![Page 37: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/37.jpg)
@alexsotob37
![Page 38: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/38.jpg)
@alexsotob38
Automate Everything
![Page 39: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/39.jpg)
@alexsotob39
![Page 40: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/40.jpg)
@alexsotob40
![Page 41: Using Docker For Testing Legacy Code](https://reader034.fdocuments.in/reader034/viewer/2022051007/5a6562e17f8b9af13a8b47a9/html5/thumbnails/41.jpg)
Thank You
https://github.com/lordofthejars/conference-testbed
https://github.com/lordofthejars/jpa-onetomany-springboot-maven-mysql