using docker for testing legacy code

41
USING DOCKER FOR TESTING LEGACY @alexsotob

Upload: alex-soto

Post on 22-Jan-2018

121 views

Category:

Software


8 download

TRANSCRIPT

Page 1: Using Docker For Testing Legacy Code

USING DOCKER FOR TESTING LEGACY

@alexsotob

Page 2: Using Docker For Testing Legacy Code

@alexsotob2

Alex Soto

Red Hat Engineer www.lordofthejars.com @alexsotob

Who Am I?

Page 3: Using Docker For Testing Legacy Code

@alexsotob3

https://www.manning.com/books/testing-java-microservices

Page 4: Using Docker For Testing Legacy Code

@alexsotob4

Questions

Page 5: Using Docker For Testing Legacy Code

@alexsotob5

ENIAC

Page 6: Using Docker For Testing Legacy Code

@alexsotob

Quality for your Customers

6

Page 7: Using Docker For Testing Legacy Code

@alexsotob

Enable changeRefactor code

Validate functionalityThe right thing, and the thing right

Trust deployments

Testing

7

Page 8: Using Docker For Testing Legacy Code

@alexsoto8

What is Legacy Code?

Page 9: Using Docker For Testing Legacy Code

@alexsotob

Old Code Needs To Be Maintained

9

Page 10: Using Docker For Testing Legacy Code

@alexsotob

Apply Test First Strategies

10

Page 11: Using Docker For Testing Legacy Code

@alexsotob

How To Start?

11

Page 12: Using Docker For Testing Legacy Code

@alexsotob

Characterization Tests

12

Page 13: Using Docker For Testing Legacy Code

@alexsotob

Tests By Division

13

Page 14: Using Docker For Testing Legacy Code

@alexsotob14

Page 15: Using Docker For Testing Legacy Code

@alexsotob

Page 16: Using Docker For Testing Legacy Code

@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

@alexsotob17

Page 18: Using Docker For Testing Legacy Code

@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

@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

@alexsotob

DEMO

20

Page 21: Using Docker For Testing Legacy Code

@alexsotob21

Persistence Tests

Page 22: Using Docker For Testing Legacy Code

@alexsotob22

APE

Page 23: Using Docker For Testing Legacy Code

@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

@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

@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

@alexsotob26

DEMO

Page 27: Using Docker For Testing Legacy Code

@alexsotob27

Visual Testing

Page 28: Using Docker For Testing Legacy Code

@alexsotob

Selenium

28

Page 29: Using Docker For Testing Legacy Code

@alexsotob

Arquillian Drone

29

Page 30: Using Docker For Testing Legacy Code

@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

@alexsotob

Arquillian Graphene

31

Page 32: Using Docker For Testing Legacy Code

@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

@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

@alexsotob34

Docker and Selenium

Page 35: Using Docker For Testing Legacy Code

@alexsotob

DEMO

35

Page 36: Using Docker For Testing Legacy Code

@alexsotob36

Let’s Wind Down

Page 37: Using Docker For Testing Legacy Code

@alexsotob37

Page 38: Using Docker For Testing Legacy Code

@alexsotob38

Automate Everything

Page 39: Using Docker For Testing Legacy Code

@alexsotob39

Page 40: Using Docker For Testing Legacy Code

@alexsotob40

Page 41: Using Docker For Testing Legacy Code

Thank You

https://github.com/lordofthejars/conference-testbed

https://github.com/lordofthejars/jpa-onetomany-springboot-maven-mysql