Slide 1

Slide 1 text

TDD - EP 5 codurance.com Testing anti-patterns - The stranger, The operating system evangelist, Success against all odds and The free ride

Slide 2

Slide 2 text

Matheus Marabesi Hello there, you can call me Marabesi, But my name is Matheus Marabesi, I work at Codurance as a Software Craftsperson. I enjoy talking about anything related to: testing, patterns and gamification. You can find me at @MatheusMarabesi or https://marabesi.com Codurance Crafting Code

Slide 3

Slide 3 text

1. Intro - Recap 2. The stranger 3. The operating system evangelist 4. Success against all odds 5. The free ride 6. Wrapping up Crafting code Agenda

Slide 4

Slide 4 text

1. Recap Episode 1, Episode 2, Episode 3, Episode 4 Getting started

Slide 5

Slide 5 text

The Liar The Giant The Mockery The Inspector Generous Leftovers The Local Hero The Nitpicker The Secret Catcher The Dodger The Loudmouth Anti patterns The Greedy Catcher Excessive Setup The Sequencer Hidden Dependency The Enumerator The Stranger The Operating System Evangelist Success Against All Odds The Free Ride The One The Peeping Tom The Slow Poke James Carr - TDD Anti-Patterns

Slide 6

Slide 6 text

The Liar 4 The Giant 5 The Mockery 1 The Inspector 7 Generous Leftovers 5 The Local Hero 7 The Nitpicker 8 The Secret Catcher 7 The Dodger 8 The Loudmouth 8 Anti patterns The Greedy Catcher 7 Excessive Setup 3 The Sequencer 7 Hidden Dependency 2 The Enumerator 8 The Stranger The Operating System Evangelist Success Against All Odds The Free Ride The One The Peeping Tom The Slow Poke 6

Slide 7

Slide 7 text

Anti patterns - Survey takeaways 1. Survey notes: Javascript, PHP and Java were the most used programming languages 2. Survey notes: Practitioners usually informally learn TDD 3. The anti patterns covered were related to test last 4. Subjects we touched around testability: SOLID, Object calisthenics, Non-determinism and the test pyramid 5. Examples are from open source projects and also extraction from real code bases

Slide 8

Slide 8 text

MOST POPULAR

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

2. Anti-patterns - Episode 5 The stranger, The operating system evangelist, Success against all odds and The free ride Getting started

Slide 14

Slide 14 text

The Liar The Giant The Mockery The Inspector Generous Leftovers The Local Hero The Nitpicker The Secret Catcher The Dodger The Loudmouth Anti patterns The Greedy Catcher Excessive Setup The Sequencer Hidden Dependency The Enumerator The Stranger The Operating System Evangelist Success Against All Odds The Free Ride The One The Peeping Tom The Slow Poke James Carr - TDD Anti-Patterns

Slide 15

Slide 15 text

The Liar The Giant The Mockery The Inspector Generous Leftovers The Local Hero The Nitpicker The Secret Catcher The Dodger The Loudmouth Anti patterns The Greedy Catcher Excessive Setup The Sequencer Hidden Dependency The Enumerator The Stranger 7 The Operating System Evangelist 8 Success Against All Odds 3 The Free Ride 8 The One The Peeping Tom The Slow Poke James Carr - TDD Anti-Patterns

Slide 16

Slide 16 text

2. The stranger - 🏆 7 Crafting code

Slide 17

Slide 17 text

2. The stranger - 🏆 7 A test case that doesn’t even belong in the unit test it is part of. it’s really testing a separate object, most likely an object that is used by the object under test, but the test case has gone and tested that object directly without relying on the output from the object under test making use of that object for its own behavior. Crafting code

Slide 18

Slide 18 text

Usually the strange is not something easy to spot

Slide 19

Slide 19 text

● It depends on context The stranger

Slide 20

Slide 20 text

● It depends on context ● It is related to the xUnit pattern in the section "Test smells" The stranger

Slide 21

Slide 21 text

● It depends on context ● It is related to the xUnit pattern in the section "Test smells" ● It can be related to mocks The stranger

Slide 22

Slide 22 text

3. The Operating System Evangelist - 🏆8 Crafting code

Slide 23

Slide 23 text

3. The Operating System Evangelist - 🏆8 A unit test that relies on a specific operation system environment to be in place in order to work. Crafting code

Slide 24

Slide 24 text

class LutrisWrapperTestCase(unittest.TestCase): def test_excluded_initial_process(self): "Test that an excluded process that starts a monitored process works" env = os.environ.copy() env['PYTHONPATH'] = ':'.join(sys.path) # run the lutris-wrapper with a bash subshell. bash is "excluded" wrapper_proc = subprocess.Popen( [ sys.executable, lutris_wrapper_bin, 'title', '0', '1', 'bash', 'bash', '-c', "echo Hello World; exec 1>&-; while sleep infinity; do true; done" ], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, env=env, ) Python - Lutris

Slide 25

Slide 25 text

class LutrisWrapperTestCase(unittest.TestCase): def test_excluded_initial_process(self): "Test that an excluded process that starts a monitored process works" env = os.environ.copy() env['PYTHONPATH'] = ':'.join(sys.path) # run the lutris-wrapper with a bash subshell. bash is "excluded" wrapper_proc = subprocess.Popen( [ sys.executable, lutris_wrapper_bin, 'title', '0', '1', 'bash', 'bash', '-c', "echo Hello World; exec 1>&-; while sleep infinity; do true; done" ], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, env=env, ) Python - Lutris

Slide 26

Slide 26 text

class LutrisWrapperTestCase(unittest.TestCase): def test_excluded_initial_process(self): "Test that an excluded process that starts a monitored process works" env = os.environ.copy() env['PYTHONPATH'] = ':'.join(sys.path) # run the lutris-wrapper with a bash subshell. bash is "excluded" wrapper_proc = subprocess.Popen( [ sys.executable, lutris_wrapper_bin, 'title', '0', '1', 'bash', 'bash', '-c', "echo Hello World; exec 1>&-; while sleep infinity; do true; done" ], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, env=env, ) Python - Lutris

Slide 27

Slide 27 text

class LutrisWrapperTestCase(unittest.TestCase): def test_excluded_initial_process(self): "Test that an excluded process that starts a monitored process works" env = os.environ.copy() env['PYTHONPATH'] = ':'.join(sys.path) # run the lutris-wrapper with a bash subshell. bash is "excluded" wrapper_proc = subprocess.Popen( [ sys.executable, lutris_wrapper_bin, 'title', '0', '1', 'bash', 'bash', '-c', "echo Hello World; exec 1>&-; while sleep infinity; do true; done" ], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, env=env, ) Python - Lutris

Slide 28

Slide 28 text

class LutrisWrapperTestCase(unittest.TestCase): def test_excluded_initial_process(self): "Test that an excluded process that starts a monitored process works" env = os.environ.copy() env['PYTHONPATH'] = ':'.join(sys.path) # run the lutris-wrapper with a bash subshell. bash is "excluded" wrapper_proc = subprocess.Popen( [ sys.executable, lutris_wrapper_bin, 'title', '0', '1', 'bash', 'bash', '-c', "echo Hello World; exec 1>&-; while sleep infinity; do true; done" ], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, env=env, ) Python - Lutris

Slide 29

Slide 29 text

class LutrisWrapperTestCase(unittest.TestCase): def test_excluded_initial_process(self): "Test that an excluded process that starts a monitored process works" env = os.environ.copy() env['PYTHONPATH'] = ':'.join(sys.path) # run the lutris-wrapper with a bash subshell. bash is "excluded" wrapper_proc = subprocess.Popen( [ sys.executable, lutris_wrapper_bin, 'title', '0', '1', 'bash', 'bash', '-c', "echo Hello World; exec 1>&-; while sleep infinity; do true; done" ], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, env=env, ) Python - Lutris

Slide 30

Slide 30 text

class LutrisWrapperTestCase(unittest.TestCase): def test_excluded_initial_process(self): "Test that an excluded process that starts a monitored process works" env = os.environ.copy() env['PYTHONPATH'] = ':'.join(sys.path) # run the lutris-wrapper with a bash subshell. bash is "excluded" wrapper_proc = subprocess.Popen( [ sys.executable, lutris_wrapper_bin, 'title', '0', '1', 'bash', 'bash', '-c', "echo Hello World; exec 1>&-; while sleep infinity; do true; done" ], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, env=env, ) Python - Lutris

Slide 31

Slide 31 text

class LutrisWrapperTestCase(unittest.TestCase): def test_excluded_initial_process(self): "Test that an excluded process that starts a monitored process works" env = os.environ.copy() env['PYTHONPATH'] = ':'.join(sys.path) # run the lutris-wrapper with a bash subshell. bash is "excluded" wrapper_proc = subprocess.Popen( [ sys.executable, lutris_wrapper_bin, 'title', '0', '1', 'bash', 'bash', '-c', "echo Hello World; exec 1>&-; while sleep infinity; do true; done" ], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, env=env, ) Python - Lutris

Slide 32

Slide 32 text

class LutrisWrapperTestCase(unittest.TestCase): def test_excluded_initial_process(self): "Test that an excluded process that starts a monitored process works" env = os.environ.copy() env['PYTHONPATH'] = ':'.join(sys.path) # run the lutris-wrapper with a bash subshell. bash is "excluded" wrapper_proc = subprocess.Popen( [ sys.executable, lutris_wrapper_bin, 'title', '0', '1', 'bash', 'bash', '-c', "echo Hello World; exec 1>&-; while sleep infinity; do true; done" ], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, env=env, ) Python - Lutris

Slide 33

Slide 33 text

class LutrisWrapperTestCase(unittest.TestCase): def test_excluded_initial_process(self): "Test that an excluded process that starts a monitored process works" env = os.environ.copy() env['PYTHONPATH'] = ':'.join(sys.path) # run the lutris-wrapper with a bash subshell. bash is "excluded" wrapper_proc = subprocess.Popen( [ sys.executable, lutris_wrapper_bin, 'title', '0', '1', 'bash', 'bash', '-c', "echo Hello World; exec 1>&-; while sleep infinity; do true; done" ], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, env=env, ) Python - Lutris

Slide 34

Slide 34 text

class LutrisWrapperTestCase(unittest.TestCase): def test_excluded_initial_process(self): "Test that an excluded process that starts a monitored process works" env = os.environ.copy() env['PYTHONPATH'] = ':'.join(sys.path) # run the lutris-wrapper with a bash subshell. bash is "excluded" wrapper_proc = subprocess.Popen( [ sys.executable, lutris_wrapper_bin, 'title', '0', '1', 'bash', 'bash', '-c', "echo Hello World; exec 1>&-; while sleep infinity; do true; done" ], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, env=env, ) Python - Lutris

Slide 35

Slide 35 text

class LutrisWrapperTestCase(unittest.TestCase): def test_excluded_initial_process(self): "Test that an excluded process that starts a monitored process works" env = os.environ.copy() env['PYTHONPATH'] = ':'.join(sys.path) # run the lutris-wrapper with a bash subshell. bash is "excluded" wrapper_proc = subprocess.Popen( [ sys.executable, lutris_wrapper_bin, 'title', '0', '1', 'bash', 'bash', '-c', "echo Hello World; exec 1>&-; while sleep infinity; do true; done" ], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, env=env, ) Python - Lutris

Slide 36

Slide 36 text

class LutrisWrapperTestCase(unittest.TestCase): def test_excluded_initial_process(self): "Test that an excluded process that starts a monitored process works" env = os.environ.copy() env['PYTHONPATH'] = ':'.join(sys.path) # run the lutris-wrapper with a bash subshell. bash is "excluded" wrapper_proc = subprocess.Popen( [ sys.executable, lutris_wrapper_bin, 'title', '0', '1', 'bash', 'bash', '-c', "echo Hello World; exec 1>&-; while sleep infinity; do true; done" ], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, env=env, ) Python - Lutris

Slide 37

Slide 37 text

Go lang - Windows portability

Slide 38

Slide 38 text

Go lang - Windows portability

Slide 39

Slide 39 text

@Test public void testWithoutSOptionAndWithoutJENKINS_URL() throws Exception { Assume.assumeThat(System.getenv("JENKINS_URL"), is(nullValue())); // TODO instead remove it from the process env? assertNotEquals(0, launch("java", "-Duser.home=" + home, "-jar", jar.getAbsolutePath(), "who-am-i") ); } Java - Jenkins

Slide 40

Slide 40 text

@Test public void testWithoutSOptionAndWithoutJENKINS_URL() throws Exception { Assume.assumeThat(System.getenv("JENKINS_URL"), is(nullValue())); // TODO instead remove it from the process env? assertNotEquals(0, launch("java", "-Duser.home=" + home, "-jar", jar.getAbsolutePath(), "who-am-i") ); } Java - Jenkins

Slide 41

Slide 41 text

FLASH BACK Episode 2 - The local hero Episode 2 - The local hero

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

https://www.codurance.com/publications/tdd-anti-patterns-chapter-2

Slide 44

Slide 44 text

4. Success Against All Odds - 🏆3 Crafting code

Slide 45

Slide 45 text

4. Success Against All Odds - 🏆3 A test that was written pass first rather than fail first. As an unfortunate side effect, the test case happens to always pass even though the test should fail. Crafting code

Slide 46

Slide 46 text

@Repository class ProductsRepositoryWithPostgres( private val entityManager: EntityManager ) : Repository { override fun listFilteredProducts(query: String?, input: PagingQueryInput?) { val pageRequest: PageRequest = input.asPageRequest() val page: Page = if (query.isNullOrBlank()) { entityManager.findAll(pageRequest) } else { entityManager.findAllByName(query, pageRequest) } return page } } mockk - kotlin

Slide 47

Slide 47 text

@Repository class ProductsRepositoryWithPostgres( private val entityManager: EntityManager ) : Repository { override fun listFilteredProducts(query: String?, input: PagingQueryInput?) { val pageRequest: PageRequest = input.asPageRequest() val page: Page = if (query.isNullOrBlank()) { entityManager.findAll(pageRequest) } else { entityManager.findAllByName(query, pageRequest) } return page } } mockk - kotlin

Slide 48

Slide 48 text

@Repository class ProductsRepositoryWithPostgres( private val entityManager: EntityManager ) : Repository { override fun listFilteredProducts(query: String?, input: PagingQueryInput?) { val pageRequest: PageRequest = input.asPageRequest() val page: Page = if (query.isNullOrBlank()) { entityManager.findAll(pageRequest) } else { entityManager.findAllByName(query, pageRequest) } return page } } mockk - kotlin

Slide 49

Slide 49 text

@Repository class ProductsRepositoryWithPostgres( private val entityManager: EntityManager ) : Repository { override fun listFilteredProducts(query: String?, input: PagingQueryInput?) { val pageRequest: PageRequest = input.asPageRequest() val page: Page = if (query.isNullOrBlank()) { entityManager.findAll(pageRequest) } else { entityManager.findAllByName(query, pageRequest) } return page } } mockk - kotlin

Slide 50

Slide 50 text

@Repository class ProductsRepositoryWithPostgres( private val entityManager: EntityManager ) : Repository { override fun listFilteredProducts(query: String?, input: PagingQueryInput?) { val pageRequest: PageRequest = input.asPageRequest() val page: Page = if (query.isNullOrBlank()) { entityManager.findAll(pageRequest) } else { entityManager.findAllByName(query, pageRequest) } return page } } mockk - kotlin

Slide 51

Slide 51 text

@Repository class ProductsRepositoryWithPostgres( private val entityManager: EntityManager ) : Repository { override fun listFilteredProducts(query: String?, input: PagingQueryInput?) { val pageRequest: PageRequest = input.asPageRequest() val page: Page = if (query.isNullOrBlank()) { entityManager.findAll(pageRequest) } else { entityManager.findAllByName(query, pageRequest) } return page } } mockk - kotlin

Slide 52

Slide 52 text

@Repository class ProductsRepositoryWithPostgres( private val entityManager: EntityManager ) : Repository { override fun listFilteredProducts(query: String?, input: PagingQueryInput?) { val pageRequest: PageRequest = input.asPageRequest() val page: Page = if (query.isNullOrBlank()) { entityManager.findAll(pageRequest) } else { entityManager.findAllByName(query, pageRequest) } return page } } mockk - kotlin

Slide 53

Slide 53 text

@Repository class ProductsRepositoryWithPostgres( private val entityManager: EntityManager ) : Repository { override fun listFilteredProducts(query: String?, input: PagingQueryInput?) { val pageRequest: PageRequest = input.asPageRequest() val page: Page = if (query.isNullOrBlank()) { entityManager.findAll(pageRequest) } else { entityManager.findAllByName(query, pageRequest) } return page } } mockk - kotlin

Slide 54

Slide 54 text

@Repository class ProductsRepositoryWithPostgres( private val entityManager: EntityManager ) : Repository { override fun listFilteredProducts(query: String?, input: PagingQueryInput?) { val pageRequest: PageRequest = input.asPageRequest() val page: Page = if (query.isNullOrBlank()) { entityManager.findAll(pageRequest) } else { entityManager.findAllByName(query, pageRequest) } return page } } mockk - kotlin

Slide 55

Slide 55 text

@Repository class ProductsRepositoryWithPostgres( private val entityManager: EntityManager ) : Repository { override fun listFilteredProducts(query: String?, input: PagingQueryInput?) { val pageRequest: PageRequest = input.asPageRequest() val page: Page = if (query.isNullOrBlank()) { entityManager.findAll(pageRequest) } else { entityManager.findAllByName(query, pageRequest) } return page } } mockk - kotlin

Slide 56

Slide 56 text

private fun setupBeforeAll() { productIds = (1..100).map { db().productWithDependencies().apply().get() } productIdsContainingWood.addAll( (1..3).map { insertProductWithName("WoodyWoodOrange " + faker.funnyName().name()) } ) productIdsContainingWood.addAll( (1..3).map { insertProductWithName( faker.funnyName().name() + " WoodyWoodOrange " + faker.funnyName().name() ) } ) mockk - kotlin

Slide 57

Slide 57 text

private fun setupBeforeAll() { productIds = (1..100).map { db().productWithDependencies().apply().get() } productIdsContainingWood.addAll( (1..3).map { insertProductWithName("WoodyWoodOrange " + faker.funnyName().name()) } ) productIdsContainingWood.addAll( (1..3).map { insertProductWithName( faker.funnyName().name() + " WoodyWoodOrange " + faker.funnyName().name() ) } ) mockk - kotlin

Slide 58

Slide 58 text

private fun setupBeforeAll() { productIds = (1..100).map { db().productWithDependencies().apply().get() } productIdsContainingWood.addAll( (1..3).map { insertProductWithName("WoodyWoodOrange " + faker.funnyName().name()) } ) productIdsContainingWood.addAll( (1..3).map { insertProductWithName( faker.funnyName().name() + " WoodyWoodOrange " + faker.funnyName().name() ) } ) mockk - kotlin

Slide 59

Slide 59 text

private fun setupBeforeAll() { productIds = (1..100).map { db().productWithDependencies().apply().get() } productIdsContainingWood.addAll( (1..3).map { insertProductWithName("WoodyWoodOrange " + faker.funnyName().name()) } ) productIdsContainingWood.addAll( (1..3).map { insertProductWithName( faker.funnyName().name() + " WoodyWoodOrange " + faker.funnyName().name() ) } ) mockk - kotlin

Slide 60

Slide 60 text

private fun setupBeforeAll() { productIds = (1..100).map { db().productWithDependencies().apply().get() } productIdsContainingWood.addAll( (1..3).map { insertProductWithName("WoodyWoodOrange " + faker.funnyName().name()) } ) productIdsContainingWood.addAll( (1..3).map { insertProductWithName( faker.funnyName().name() + " WoodyWoodOrange " + faker.funnyName().name() ) } ) mockk - kotlin

Slide 61

Slide 61 text

productIdsContainingWood.addAll( (1..2).map { insertProductWithName(faker.funnyName().name() + " WoodyWoodOrange") } ) cancelledProductId = db().productWithDependencies("status" to Product.CANCELLED).apply().get() idsOfStartedByWithCustomerDeadline.addAll( listOf( db().productWithDependencies("customer_deadline" to "2022-04-03T00:00:00.00Z").apply() .get(), db().productWithDependencies("customer_deadline" to "2022-04-02T00:00:00.00Z").apply() .get(), db().productWithDependencies("customer_deadline" to "2022-04-01T00:00:00.00Z").apply() .get(), ) ) } mockk - kotlin

Slide 62

Slide 62 text

productIdsContainingWood.addAll( (1..2).map { insertProductWithName(faker.funnyName().name() + " WoodyWoodOrange") } ) cancelledProductId = db().productWithDependencies("status" to Product.CANCELLED).apply().get() idsOfStartedByWithCustomerDeadline.addAll( listOf( db().productWithDependencies("customer_deadline" to "2022-04-03T00:00:00.00Z").apply() .get(), db().productWithDependencies("customer_deadline" to "2022-04-02T00:00:00.00Z").apply() .get(), db().productWithDependencies("customer_deadline" to "2022-04-01T00:00:00.00Z").apply() .get(), ) ) } mockk - kotlin

Slide 63

Slide 63 text

productIdsContainingWood.addAll( (1..2).map { insertProductWithName(faker.funnyName().name() + " WoodyWoodOrange") } ) cancelledProductId = db().productWithDependencies("status" to Product.CANCELLED).apply().get() idsOfStartedByWithCustomerDeadline.addAll( listOf( db().productWithDependencies("customer_deadline" to "2022-04-03T00:00:00.00Z").apply() .get(), db().productWithDependencies("customer_deadline" to "2022-04-02T00:00:00.00Z").apply() .get(), db().productWithDependencies("customer_deadline" to "2022-04-01T00:00:00.00Z").apply() .get(), ) ) } mockk - kotlin

Slide 64

Slide 64 text

productIdsContainingWood.addAll( (1..2).map { insertProductWithName(faker.funnyName().name() + " WoodyWoodOrange") } ) cancelledProductId = db().productWithDependencies("status" to Product.CANCELLED).apply().get() idsOfStartedByWithCustomerDeadline.addAll( listOf( db().productWithDependencies("customer_deadline" to "2022-04-03T00:00:00.00Z").apply() .get(), db().productWithDependencies("customer_deadline" to "2022-04-02T00:00:00.00Z").apply() .get(), db().productWithDependencies("customer_deadline" to "2022-04-01T00:00:00.00Z").apply() .get(), ) ) } mockk - kotlin

Slide 65

Slide 65 text

productIdsContainingWood.addAll( (1..2).map { insertProductWithName(faker.funnyName().name() + " WoodyWoodOrange") } ) cancelledProductId = db().productWithDependencies("status" to Product.CANCELLED).apply().get() idsOfStartedByWithCustomerDeadline.addAll( listOf( db().productWithDependencies("customer_deadline" to "2022-04-03T00:00:00.00Z").apply() .get(), db().productWithDependencies("customer_deadline" to "2022-04-02T00:00:00.00Z").apply() .get(), db().productWithDependencies("customer_deadline" to "2022-04-01T00:00:00.00Z").apply() .get(), ) ) } mockk - kotlin

Slide 66

Slide 66 text

productIdsContainingWood.addAll( (1..2).map { insertProductWithName(faker.funnyName().name() + " WoodyWoodOrange") } ) cancelledProductId = db().productWithDependencies("status" to Product.CANCELLED).apply().get() idsOfStartedByWithCustomerDeadline.addAll( listOf( db().productWithDependencies("customer_deadline" to "2022-04-03T00:00:00.00Z").apply() .get(), db().productWithDependencies("customer_deadline" to "2022-04-02T00:00:00.00Z").apply() .get(), db().productWithDependencies("customer_deadline" to "2022-04-01T00:00:00.00Z").apply() .get(), ) ) } mockk - kotlin

Slide 67

Slide 67 text

productIdsContainingWood.addAll( (1..2).map { insertProductWithName(faker.funnyName().name() + " WoodyWoodOrange") } ) cancelledProductId = db().productWithDependencies("status" to Product.CANCELLED).apply().get() idsOfStartedByWithCustomerDeadline.addAll( listOf( db().productWithDependencies("customer_deadline" to "2022-04-03T00:00:00.00Z").apply() .get(), db().productWithDependencies("customer_deadline" to "2022-04-02T00:00:00.00Z").apply() .get(), db().productWithDependencies("customer_deadline" to "2022-04-01T00:00:00.00Z").apply() .get(), ) ) } mockk - kotlin

Slide 68

Slide 68 text

@Test fun `list products sorted by creation at date ascending`() { val pageQueryInput = PagingQueryInput( size = 30, page = 0, sort = listOf(Sort.CREATED_AT_ASC) ) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.currentPage).isEqualTo(0) assertThat(result.totalPages).isEqualTo(4) assertThat(result.totalElements).isEqualTo(112) assertThat(result.content.size).isEqualTo(30) assertThat(result.content).allSatisfy { productIds.subList(0, 29).contains(it.id) } } mockk - kotlin

Slide 69

Slide 69 text

@Test fun `list products sorted by creation at date ascending`() { val pageQueryInput = PagingQueryInput( size = 30, page = 0, sort = listOf(Sort.CREATED_AT_ASC) ) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.currentPage).isEqualTo(0) assertThat(result.totalPages).isEqualTo(4) assertThat(result.totalElements).isEqualTo(112) assertThat(result.content.size).isEqualTo(30) assertThat(result.content).allSatisfy { productIds.subList(0, 29).contains(it.id) } } mockk - kotlin

Slide 70

Slide 70 text

@Test fun `list products sorted by creation at date ascending`() { val pageQueryInput = PagingQueryInput( size = 30, page = 0, sort = listOf(Sort.CREATED_AT_ASC) ) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.currentPage).isEqualTo(0) assertThat(result.totalPages).isEqualTo(4) assertThat(result.totalElements).isEqualTo(112) assertThat(result.content.size).isEqualTo(30) assertThat(result.content).allSatisfy { productIds.subList(0, 29).contains(it.id) } } mockk - kotlin

Slide 71

Slide 71 text

@Test fun `list products sorted by creation at date ascending`() { val pageQueryInput = PagingQueryInput( size = 30, page = 0, sort = listOf(Sort.CREATED_AT_ASC) ) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.currentPage).isEqualTo(0) assertThat(result.totalPages).isEqualTo(4) assertThat(result.totalElements).isEqualTo(112) assertThat(result.content.size).isEqualTo(30) assertThat(result.content).allSatisfy { productIds.subList(0, 29).contains(it.id) } } mockk - kotlin

Slide 72

Slide 72 text

@Test fun `list products sorted by creation at date ascending`() { val pageQueryInput = PagingQueryInput( size = 30, page = 0, sort = listOf(Sort.CREATED_AT_ASC) ) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.currentPage).isEqualTo(0) assertThat(result.totalPages).isEqualTo(4) assertThat(result.totalElements).isEqualTo(112) assertThat(result.content.size).isEqualTo(30) assertThat(result.content).allSatisfy { productIds.subList(0, 29).contains(it.id) } } mockk - kotlin

Slide 73

Slide 73 text

@Test fun `list products sorted by creation at date ascending`() { val pageQueryInput = PagingQueryInput( size = 30, page = 0, sort = listOf(Sort.CREATED_AT_ASC) ) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.currentPage).isEqualTo(0) assertThat(result.totalPages).isEqualTo(4) assertThat(result.totalElements).isEqualTo(112) assertThat(result.content.size).isEqualTo(30) assertThat(result.content).allSatisfy { productIds.subList(0, 29).contains(it.id) } } mockk - kotlin

Slide 74

Slide 74 text

@Test fun `list products sorted by creation at date ascending`() { val pageQueryInput = PagingQueryInput( size = 30, page = 0, sort = listOf(Sort.CREATED_AT_ASC) ) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.currentPage).isEqualTo(0) assertThat(result.totalPages).isEqualTo(4) assertThat(result.totalElements).isEqualTo(112) assertThat(result.content.size).isEqualTo(30) assertThat(result.content).allSatisfy { productIds.subList(0, 29).contains(it.id) } } mockk - kotlin

Slide 75

Slide 75 text

@Test fun `list products sorted by creation at date ascending`() { val pageQueryInput = PagingQueryInput( size = 30, page = 0, sort = listOf(Sort.CREATED_AT_ASC) ) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.currentPage).isEqualTo(0) assertThat(result.totalPages).isEqualTo(4) assertThat(result.totalElements).isEqualTo(112) assertThat(result.content.size).isEqualTo(30) assertThat(result.content).allSatisfy { productIds.subList(0, 29).contains(it.id) } } mockk - kotlin

Slide 76

Slide 76 text

@Test fun `list products sorted by creation at date ascending`() { val pageQueryInput = PagingQueryInput( size = 30, page = 0, sort = listOf(Sort.CREATED_AT_ASC) ) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.currentPage).isEqualTo(0) assertThat(result.totalPages).isEqualTo(4) assertThat(result.totalElements).isEqualTo(112) assertThat(result.content.size).isEqualTo(30) assertThat(result.content).allSatisfy { productIds.subList(0, 29).contains(it.id) } } mockk - kotlin

Slide 77

Slide 77 text

@Test fun `list products sorted by creation at date ascending`() { val pageQueryInput = PagingQueryInput( size = 30, page = 0, sort = listOf(Sort.CREATED_AT_ASC) ) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.currentPage).isEqualTo(0) assertThat(result.totalPages).isEqualTo(4) assertThat(result.totalElements).isEqualTo(112) assertThat(result.content.size).isEqualTo(30) assertThat(result.content).allSatisfy { productIds.subList(0, 29).contains(it.id) } } mockk - kotlin

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

@Test fun `list products sorted by creation at date ascending`() { val pageQueryInput = PagingQueryInput( size = 30, page = 0, sort = listOf(Sort.CREATED_AT_ASC) ) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.currentPage).isEqualTo(0) assertThat(result.totalPages).isEqualTo(4) assertThat(result.totalElements).isEqualTo(112) assertThat(result.content.size).isEqualTo(30) assertThat(result.content).allSatisfy { productIds.subList(0, 29).contains(it.id) } } mockk - kotlin

Slide 80

Slide 80 text

@Test fun `list products sorted by creation at date descending`() { val pageQueryInput = PagingQueryInput( size = 30, page = 0, sort = listOf(Sort.CREATED_AT_DESC) ) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.currentPage).isEqualTo(0) assertThat(result.totalPages).isEqualTo(4) assertThat(result.totalElements).isEqualTo(112) assertThat(result.content.size).isEqualTo(30) assertThat(result.content).allSatisfy { productIds.subList(0, 29).contains(it.id) } } mockk - kotlin

Slide 81

Slide 81 text

● Sorting ● Pagination Points of attention WHAT COULD WE DO?

Slide 82

Slide 82 text

Focus on a single thing SORTING

Slide 83

Slide 83 text

@Test fun `list products sorted by creation at date ascending`() { val pageQueryInput = PagingQueryInput( size = 30, page = 0, sort = listOf(Sort.CREATED_AT_ASC) ) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.currentPage).isEqualTo(0) assertThat(result.totalPages).isEqualTo(4) assertThat(result.totalElements).isEqualTo(112) assertThat(result.content.size).isEqualTo(30) assertThat(result.content).allSatisfy { productIds.subList(0, 29).contains(it.id) } } mockk - kotlin

Slide 84

Slide 84 text

@Test fun `list products sorted by ascending creation date`() { db().productWithDependencies("created_at" to "2022-04-03T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-02T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-01T00:00:00.00Z").apply() val pageQueryInput = PagingQueryInput(sort = listOf(SortOrder.CREATED_AT_ASC)) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.content[0].createdAt).isEqualTo("2022-04-01T00:00:00.00Z") assertThat(result.content[1].createdAt).isEqualTo("2022-04-02T00:00:00.00Z") assertThat(result.content[2].createdAt).isEqualTo("2022-04-03T00:00:00.00Z") } mockk - kotlin

Slide 85

Slide 85 text

@Test fun `list products sorted by ascending creation date`() { db().productWithDependencies("created_at" to "2022-04-03T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-02T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-01T00:00:00.00Z").apply() val pageQueryInput = PagingQueryInput(sort = listOf(SortOrder.CREATED_AT_ASC)) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.content[0].createdAt).isEqualTo("2022-04-01T00:00:00.00Z") assertThat(result.content[1].createdAt).isEqualTo("2022-04-02T00:00:00.00Z") assertThat(result.content[2].createdAt).isEqualTo("2022-04-03T00:00:00.00Z") } mockk - kotlin

Slide 86

Slide 86 text

@Test fun `list products sorted by ascending creation date`() { db().productWithDependencies("created_at" to "2022-04-03T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-02T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-01T00:00:00.00Z").apply() val pageQueryInput = PagingQueryInput(sort = listOf(SortOrder.CREATED_AT_ASC)) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.content[0].createdAt).isEqualTo("2022-04-01T00:00:00.00Z") assertThat(result.content[1].createdAt).isEqualTo("2022-04-02T00:00:00.00Z") assertThat(result.content[2].createdAt).isEqualTo("2022-04-03T00:00:00.00Z") } mockk - kotlin

Slide 87

Slide 87 text

@Test fun `list products sorted by ascending creation date`() { db().productWithDependencies("created_at" to "2022-04-03T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-02T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-01T00:00:00.00Z").apply() val pageQueryInput = PagingQueryInput(sort = listOf(SortOrder.CREATED_AT_ASC)) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.content[0].createdAt).isEqualTo("2022-04-01T00:00:00.00Z") assertThat(result.content[1].createdAt).isEqualTo("2022-04-02T00:00:00.00Z") assertThat(result.content[2].createdAt).isEqualTo("2022-04-03T00:00:00.00Z") } mockk - kotlin

Slide 88

Slide 88 text

@Test fun `list products sorted by ascending creation date`() { db().productWithDependencies("created_at" to "2022-04-03T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-02T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-01T00:00:00.00Z").apply() val pageQueryInput = PagingQueryInput(sort = listOf(SortOrder.CREATED_AT_ASC)) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.content[0].createdAt).isEqualTo("2022-04-01T00:00:00.00Z") assertThat(result.content[1].createdAt).isEqualTo("2022-04-02T00:00:00.00Z") assertThat(result.content[2].createdAt).isEqualTo("2022-04-03T00:00:00.00Z") } mockk - kotlin

Slide 89

Slide 89 text

@Test fun `list products sorted by ascending creation date`() { db().productWithDependencies("created_at" to "2022-04-03T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-02T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-01T00:00:00.00Z").apply() val pageQueryInput = PagingQueryInput(sort = listOf(SortOrder.CREATED_AT_ASC)) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.content[0].createdAt).isEqualTo("2022-04-01T00:00:00.00Z") assertThat(result.content[1].createdAt).isEqualTo("2022-04-02T00:00:00.00Z") assertThat(result.content[2].createdAt).isEqualTo("2022-04-03T00:00:00.00Z") } mockk - kotlin

Slide 90

Slide 90 text

@Test fun `list products sorted by ascending creation date`() { db().productWithDependencies("created_at" to "2022-04-03T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-02T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-01T00:00:00.00Z").apply() val pageQueryInput = PagingQueryInput(sort = listOf(SortOrder.CREATED_AT_ASC)) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.content[0].createdAt).isEqualTo("2022-04-01T00:00:00.00Z") assertThat(result.content[1].createdAt).isEqualTo("2022-04-02T00:00:00.00Z") assertThat(result.content[2].createdAt).isEqualTo("2022-04-03T00:00:00.00Z") } mockk - kotlin

Slide 91

Slide 91 text

@Test fun `list products sorted by ascending creation date`() { db().productWithDependencies("created_at" to "2022-04-03T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-02T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-01T00:00:00.00Z").apply() val pageQueryInput = PagingQueryInput(sort = listOf(SortOrder.CREATED_AT_ASC)) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.content[0].createdAt).isEqualTo("2022-04-01T00:00:00.00Z") assertThat(result.content[1].createdAt).isEqualTo("2022-04-02T00:00:00.00Z") assertThat(result.content[2].createdAt).isEqualTo("2022-04-03T00:00:00.00Z") } mockk - kotlin

Slide 92

Slide 92 text

@Test fun `list products sorted by creation at date descending`() { db().productWithDependencies("created_at" to "2022-04-01T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-02T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-03T00:00:00.00Z").apply() val pageQueryInput = PagingQueryInput(sort = listOf(SortOrder.CREATED_AT_DESC)) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.content[0].createdAt).isEqualTo("2022-04-03T00:00:00.00Z") assertThat(result.content[1].createdAt).isEqualTo("2022-04-02T00:00:00.00Z") assertThat(result.content[2].createdAt).isEqualTo("2022-04-01T00:00:00.00Z") } mockk - kotlin

Slide 93

Slide 93 text

@Test fun `list products sorted by creation at date descending`() { db().productWithDependencies("created_at" to "2022-04-01T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-02T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-03T00:00:00.00Z").apply() val pageQueryInput = PagingQueryInput(sort = listOf(SortOrder.CREATED_AT_DESC)) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.content[0].createdAt).isEqualTo("2022-04-03T00:00:00.00Z") assertThat(result.content[1].createdAt).isEqualTo("2022-04-02T00:00:00.00Z") assertThat(result.content[2].createdAt).isEqualTo("2022-04-01T00:00:00.00Z") } mockk - kotlin

Slide 94

Slide 94 text

@Test fun `list products sorted by creation at date descending`() { db().productWithDependencies("created_at" to "2022-04-01T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-02T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-03T00:00:00.00Z").apply() val pageQueryInput = PagingQueryInput(sort = listOf(SortOrder.CREATED_AT_DESC)) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.content[0].createdAt).isEqualTo("2022-04-03T00:00:00.00Z") assertThat(result.content[1].createdAt).isEqualTo("2022-04-02T00:00:00.00Z") assertThat(result.content[2].createdAt).isEqualTo("2022-04-01T00:00:00.00Z") } mockk - kotlin

Slide 95

Slide 95 text

@Test fun `list products sorted by creation at date descending`() { db().productWithDependencies("created_at" to "2022-04-01T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-02T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-03T00:00:00.00Z").apply() val pageQueryInput = PagingQueryInput(sort = listOf(SortOrder.CREATED_AT_DESC)) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.content[0].createdAt).isEqualTo("2022-04-03T00:00:00.00Z") assertThat(result.content[1].createdAt).isEqualTo("2022-04-02T00:00:00.00Z") assertThat(result.content[2].createdAt).isEqualTo("2022-04-01T00:00:00.00Z") } mockk - kotlin

Slide 96

Slide 96 text

@Test fun `list products sorted by creation at date descending`() { db().productWithDependencies("created_at" to "2022-04-01T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-02T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-03T00:00:00.00Z").apply() val pageQueryInput = PagingQueryInput(sort = listOf(SortOrder.CREATED_AT_DESC)) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.content[0].createdAt).isEqualTo("2022-04-03T00:00:00.00Z") assertThat(result.content[1].createdAt).isEqualTo("2022-04-02T00:00:00.00Z") assertThat(result.content[2].createdAt).isEqualTo("2022-04-01T00:00:00.00Z") } mockk - kotlin

Slide 97

Slide 97 text

@Test fun `list products sorted by creation at date descending`() { db().productWithDependencies("created_at" to "2022-04-01T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-02T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-03T00:00:00.00Z").apply() val pageQueryInput = PagingQueryInput(sort = listOf(SortOrder.CREATED_AT_DESC)) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.content[0].createdAt).isEqualTo("2022-04-03T00:00:00.00Z") assertThat(result.content[1].createdAt).isEqualTo("2022-04-02T00:00:00.00Z") assertThat(result.content[2].createdAt).isEqualTo("2022-04-01T00:00:00.00Z") } mockk - kotlin

Slide 98

Slide 98 text

@Test fun `list products sorted by creation at date descending`() { db().productWithDependencies("created_at" to "2022-04-01T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-02T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-03T00:00:00.00Z").apply() val pageQueryInput = PagingQueryInput(sort = listOf(SortOrder.CREATED_AT_DESC)) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.content[0].createdAt).isEqualTo("2022-04-03T00:00:00.00Z") assertThat(result.content[1].createdAt).isEqualTo("2022-04-02T00:00:00.00Z") assertThat(result.content[2].createdAt).isEqualTo("2022-04-01T00:00:00.00Z") } mockk - kotlin

Slide 99

Slide 99 text

@Test fun `list products sorted by creation at date descending`() { db().productWithDependencies("created_at" to "2022-04-01T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-02T00:00:00.00Z").apply() db().productWithDependencies("created_at" to "2022-04-03T00:00:00.00Z").apply() val pageQueryInput = PagingQueryInput(sort = listOf(SortOrder.CREATED_AT_DESC)) val result = repository.listFilteredProducts("", pageQueryInput) assertThat(result.content[0].createdAt).isEqualTo("2022-04-03T00:00:00.00Z") assertThat(result.content[1].createdAt).isEqualTo("2022-04-02T00:00:00.00Z") assertThat(result.content[2].createdAt).isEqualTo("2022-04-01T00:00:00.00Z") } mockk - kotlin

Slide 100

Slide 100 text

Focus on a single thing PAGINATION

Slide 101

Slide 101 text

@Test fun `should list products`() { insertTenProducts() val page = PagingQueryInput(size = 10) val result = repository.listFilteredProductBriefs(null, null, page) assertThat(result.currentPage).isEqualTo(0) assertThat(result.totalPages).isEqualTo(1) } mockk - kotlin

Slide 102

Slide 102 text

@Test fun `should list products`() { insertTenProducts() val page = PagingQueryInput(size = 10) val result = repository.listFilteredProductBriefs(null, null, page) assertThat(result.currentPage).isEqualTo(0) assertThat(result.totalPages).isEqualTo(1) } mockk - kotlin

Slide 103

Slide 103 text

@Test fun `should list products`() { insertTenProducts() val page = PagingQueryInput(size = 10) val result = repository.listFilteredProductBriefs(null, null, page) assertThat(result.currentPage).isEqualTo(0) assertThat(result.totalPages).isEqualTo(1) } mockk - kotlin

Slide 104

Slide 104 text

@Test fun `should list products`() { insertTenProducts() val page = PagingQueryInput(size = 10) val result = repository.listFilteredProductBriefs(null, null, page) assertThat(result.currentPage).isEqualTo(0) assertThat(result.totalPages).isEqualTo(1) } mockk - kotlin

Slide 105

Slide 105 text

@Test fun `should list products`() { insertTenProducts() val page = PagingQueryInput(size = 10) val result = repository.listFilteredProductBriefs(null, null, page) assertThat(result.currentPage).isEqualTo(0) assertThat(result.totalPages).isEqualTo(1) } mockk - kotlin

Slide 106

Slide 106 text

@Test fun `should have one page when the list is ten`() { insertTenProducts() val page = PagingQueryInput(size = 10) val result = repository.listFilteredProducts( null, null, Page ) assertThat(result.totalPages).isEqualTo(1) } mockk - kotlin

Slide 107

Slide 107 text

@Test fun `should have one page when the list is ten`() { insertTenProducts() val page = PagingQueryInput(size = 10) val result = repository.listFilteredProducts( null, null, Page ) assertThat(result.totalPages).isEqualTo(1) } mockk - kotlin

Slide 108

Slide 108 text

@Test fun `should have one page when the list is ten`() { insertTenProducts() val page = PagingQueryInput(size = 10) val result = repository.listFilteredProducts( null, null, Page ) assertThat(result.totalPages).isEqualTo(1) } mockk - kotlin

Slide 109

Slide 109 text

@Test fun `should have one page when the list is ten`() { insertTenProducts() val page = PagingQueryInput(size = 10) val result = repository.listFilteredProducts( null, null, Page ) assertThat(result.totalPages).isEqualTo(1) } mockk - kotlin

Slide 110

Slide 110 text

@Test fun `should have one page when the list is ten`() { insertTenProducts() val page = PagingQueryInput(size = 10) val result = repository.listFilteredProducts( null, null, Page ) assertThat(result.totalPages).isEqualTo(1) } mockk - kotlin

Slide 111

Slide 111 text

@Test fun `should have one page when the list is ten`() { insertTenProducts() val page = PagingQueryInput(size = 10) val result = repository.listFilteredProducts( null, null, Page ) assertThat(result.totalPages).isEqualTo(1) } mockk - kotlin

Slide 112

Slide 112 text

mockk - kotlin @Test fun `should be on the first page by default`() { insertTenProducts() val page = PagingQueryInput(size = 10) val result = repository.listFilteredProducts( null, null, Page ) assertThat(result.currentPage).isEqualTo(0) }

Slide 113

Slide 113 text

mockk - kotlin @Test fun `should be on the first page by default`() { insertTenProducts() val page = PagingQueryInput(size = 10) val result = repository.listFilteredProducts( null, null, Page ) assertThat(result.currentPage).isEqualTo(0) }

Slide 114

Slide 114 text

mockk - kotlin @Test fun `should be on the first page by default`() { insertTenProducts() val page = PagingQueryInput(size = 10) val result = repository.listFilteredProducts( null, null, Page ) assertThat(result.currentPage).isEqualTo(0) }

Slide 115

Slide 115 text

mockk - kotlin @Test fun `should be on the first page by default`() { insertTenProducts() val page = PagingQueryInput(size = 10) val result = repository.listFilteredProducts( null, null, Page ) assertThat(result.currentPage).isEqualTo(0) }

Slide 116

Slide 116 text

@Test fun `should have one page when the list is ten`() { insertTenProducts() val page = PagingQueryInput(size = 10) val result = repository.listFilteredProducts( null, null, Page ) assertThat(result.totalPages).isEqualTo(1) } mockk - kotlin @Test fun `should be on the first page by default`() { insertTenProducts() val page = PagingQueryInput(size = 10) val result = repository.listFilteredProducts( null, null, Page ) assertThat(result.currentPage).isEqualTo(0) }

Slide 117

Slide 117 text

5. The Free Ride - 🏆8 Crafting code

Slide 118

Slide 118 text

describe('Request.headers', function () { it('should work', async () => { const { page, server, isChrome } = getTestState(); const response = await page.goto(server.EMPTY_PAGE); if (isChrome) expect(response.request().headers()['user-agent']).toContain('Chrome'); else expect(response.request().headers()['user-agent']).toContain('Firefox'); }); }); describe('Request.headers', function () { itChromeOnly('should define Chrome as user agent header', async () => { const { page, server } = getTestState(); const response = await page.goto(server.EMPTY_PAGE); expect(response.request().headers()['user-agent']).toContain('Chrome'); }); itFirefoxOnly('should define Firefox as user agent header', async () => { const { page, server } = getTestState(); const response = await page.goto(server.EMPTY_PAGE); expect(response.request().headers()['user-agent']).toContain('Firefox'); }); }); Puppeteer A

Slide 119

Slide 119 text

describe('Request.headers', function () { it('should work', async () => { const { page, server, isChrome } = getTestState(); const response = await page.goto(server.EMPTY_PAGE); if (isChrome) expect(response.request().headers()['user-agent']).toContain('Chrome'); else expect(response.request().headers()['user-agent']).toContain('Firefox'); }); }); describe('Request.headers', function () { itChromeOnly('should define Chrome as user agent header', async () => { const { page, server } = getTestState(); const response = await page.goto(server.EMPTY_PAGE); expect(response.request().headers()['user-agent']).toContain('Chrome'); }); itFirefoxOnly('should define Firefox as user agent header', async () => { const { page, server } = getTestState(); const response = await page.goto(server.EMPTY_PAGE); expect(response.request().headers()['user-agent']).toContain('Firefox'); }); }); Puppeteer B

Slide 120

Slide 120 text

describe('Request.headers', function () { it('should work', async () => { const { page, server, isChrome } = getTestState(); const response = await page.goto(server.EMPTY_PAGE); if (isChrome) expect(response.request().headers()['user-agent']).toContain('Chrome'); else expect(response.request().headers()['user-agent']).toContain('Firefox'); }); }); describe('Request.headers', function () { itChromeOnly('should define Chrome as user agent header', async () => { const { page, server } = getTestState(); const response = await page.goto(server.EMPTY_PAGE); expect(response.request().headers()['user-agent']).toContain('Chrome'); }); itFirefoxOnly('should define Firefox as user agent header', async () => { const { page, server } = getTestState(); const response = await page.goto(server.EMPTY_PAGE); expect(response.request().headers()['user-agent']).toContain('Firefox'); }); }); Puppeteer A B

Slide 121

Slide 121 text

describe('Request.headers', function () { it('should work', async () => { const { page, server, isChrome } = getTestState(); const response = await page.goto(server.EMPTY_PAGE); if (isChrome) expect(response.request().headers()['user-agent']).toContain('Chrome'); else expect(response.request().headers()['user-agent']).toContain('Firefox'); }); }); describe('Request.headers', function () { itChromeOnly('should define Chrome as user agent header', async () => { const { page, server } = getTestState(); const response = await page.goto(server.EMPTY_PAGE); expect(response.request().headers()['user-agent']).toContain('Chrome'); }); itFirefoxOnly('should define Firefox as user agent header', async () => { const { page, server } = getTestState(); const response = await page.goto(server.EMPTY_PAGE); expect(response.request().headers()['user-agent']).toContain('Firefox'); }); }); Puppeteer A B Could you spot something that seems to be a smell?

Slide 122

Slide 122 text

describe('Request.headers', function () { it('should work', async () => { const { page, server, isChrome } = getTestState(); const response = await page.goto(server.EMPTY_PAGE); if (isChrome) expect(response.request().headers()['user-agent']).toContain('Chrome'); else expect(response.request().headers()['user-agent']).toContain('Firefox'); }); }); describe('Request.headers', function () { itChromeOnly('should define Chrome as user agent header', async () => { const { page, server } = getTestState(); const response = await page.goto(server.EMPTY_PAGE); expect(response.request().headers()['user-agent']).toContain('Chrome'); }); itFirefoxOnly('should define Firefox as user agent header', async () => { const { page, server } = getTestState(); const response = await page.goto(server.EMPTY_PAGE); expect(response.request().headers()['user-agent']).toContain('Firefox'); }); }); Puppeteer A B What about anti-patterns?

Slide 123

Slide 123 text

5. The Free Ride - 🏆8 Rather than write a new test case method to test another feature or functionality, a new assertion rides along in an existing test case. Crafting code

Slide 124

Slide 124 text

it('Page.Events.RequestFailed', async () => { const { page, server, isChrome } = getTestState(); await page.setRequestInterception(true); page.on('request', (request) => { if (request.url().endsWith('css')) request.abort(); else request.continue(); }); const failedRequests = []; page.on('requestfailed', (request) => failedRequests.push(request)); await page.goto(server.PREFIX + '/one-style.html'); expect(failedRequests.length).toBe(1); expect(failedRequests[0].url()).toContain('one-style.css'); expect(failedRequests[0].response()).toBe(null); expect(failedRequests[0].resourceType()).toBe('stylesheet'); if (isChrome) expect(failedRequests[0].failure().errorText).toBe('net::ERR_FAILED'); else expect(failedRequests[0].failure().errorText).toBe('NS_ERROR_FAILURE'); expect(failedRequests[0].frame()).toBeTruthy(); }); Javascript/Typescript - Puppeteer

Slide 125

Slide 125 text

it('Page.Events.RequestFailed', async () => { const { page, server, isChrome } = getTestState(); await page.setRequestInterception(true); page.on('request', (request) => { if (request.url().endsWith('css')) request.abort(); else request.continue(); }); const failedRequests = []; page.on('requestfailed', (request) => failedRequests.push(request)); await page.goto(server.PREFIX + '/one-style.html'); expect(failedRequests.length).toBe(1); expect(failedRequests[0].url()).toContain('one-style.css'); expect(failedRequests[0].response()).toBe(null); expect(failedRequests[0].resourceType()).toBe('stylesheet'); if (isChrome) expect(failedRequests[0].failure().errorText).toBe('net::ERR_FAILED'); else expect(failedRequests[0].failure().errorText).toBe('NS_ERROR_FAILURE'); expect(failedRequests[0].frame()).toBeTruthy(); }); Javascript/Typescript - Puppeteer

Slide 126

Slide 126 text

it('Page.Events.RequestFailed', async () => { const { page, server, isChrome } = getTestState(); await page.setRequestInterception(true); page.on('request', (request) => { if (request.url().endsWith('css')) request.abort(); else request.continue(); }); const failedRequests = []; page.on('requestfailed', (request) => failedRequests.push(request)); await page.goto(server.PREFIX + '/one-style.html'); expect(failedRequests.length).toBe(1); expect(failedRequests[0].url()).toContain('one-style.css'); expect(failedRequests[0].response()).toBe(null); expect(failedRequests[0].resourceType()).toBe('stylesheet'); if (isChrome) expect(failedRequests[0].failure().errorText).toBe('net::ERR_FAILED'); else expect(failedRequests[0].failure().errorText).toBe('NS_ERROR_FAILURE'); expect(failedRequests[0].frame()).toBeTruthy(); }); Javascript/Typescript - Puppeteer

Slide 127

Slide 127 text

it('Page.Events.RequestFailed', async () => { const { page, server, isChrome } = getTestState(); await page.setRequestInterception(true); page.on('request', (request) => { if (request.url().endsWith('css')) request.abort(); else request.continue(); }); const failedRequests = []; page.on('requestfailed', (request) => failedRequests.push(request)); await page.goto(server.PREFIX + '/one-style.html'); expect(failedRequests.length).toBe(1); expect(failedRequests[0].url()).toContain('one-style.css'); expect(failedRequests[0].response()).toBe(null); expect(failedRequests[0].resourceType()).toBe('stylesheet'); if (isChrome) expect(failedRequests[0].failure().errorText).toBe('net::ERR_FAILED'); else expect(failedRequests[0].failure().errorText).toBe('NS_ERROR_FAILURE'); expect(failedRequests[0].frame()).toBeTruthy(); }); Javascript/Typescript - Puppeteer

Slide 128

Slide 128 text

it('Page.Events.RequestFailed', async () => { const { page, server, isChrome } = getTestState(); await page.setRequestInterception(true); page.on('request', (request) => { if (request.url().endsWith('css')) request.abort(); else request.continue(); }); const failedRequests = []; page.on('requestfailed', (request) => failedRequests.push(request)); await page.goto(server.PREFIX + '/one-style.html'); expect(failedRequests.length).toBe(1); expect(failedRequests[0].url()).toContain('one-style.css'); expect(failedRequests[0].response()).toBe(null); expect(failedRequests[0].resourceType()).toBe('stylesheet'); if (isChrome) expect(failedRequests[0].failure().errorText).toBe('net::ERR_FAILED'); else expect(failedRequests[0].failure().errorText).toBe('NS_ERROR_FAILURE'); expect(failedRequests[0].frame()).toBeTruthy(); }); Javascript/Typescript - Puppeteer

Slide 129

Slide 129 text

it('Page.Events.RequestFailed', async () => { const { page, server, isChrome } = getTestState(); await page.setRequestInterception(true); page.on('request', (request) => { if (request.url().endsWith('css')) request.abort(); else request.continue(); }); const failedRequests = []; page.on('requestfailed', (request) => failedRequests.push(request)); await page.goto(server.PREFIX + '/one-style.html'); expect(failedRequests.length).toBe(1); expect(failedRequests[0].url()).toContain('one-style.css'); expect(failedRequests[0].response()).toBe(null); expect(failedRequests[0].resourceType()).toBe('stylesheet'); if (isChrome) expect(failedRequests[0].failure().errorText).toBe('net::ERR_FAILED'); else expect(failedRequests[0].failure().errorText).toBe('NS_ERROR_FAILURE'); expect(failedRequests[0].frame()).toBeTruthy(); }); Javascript/Typescript - Puppeteer

Slide 130

Slide 130 text

it('Page.Events.RequestFailed', async () => { const { page, server, isChrome } = getTestState(); await page.setRequestInterception(true); page.on('request', (request) => { if (request.url().endsWith('css')) request.abort(); else request.continue(); }); const failedRequests = []; page.on('requestfailed', (request) => failedRequests.push(request)); await page.goto(server.PREFIX + '/one-style.html'); expect(failedRequests.length).toBe(1); expect(failedRequests[0].url()).toContain('one-style.css'); expect(failedRequests[0].response()).toBe(null); expect(failedRequests[0].resourceType()).toBe('stylesheet'); if (isChrome) expect(failedRequests[0].failure().errorText).toBe('net::ERR_FAILED'); else expect(failedRequests[0].failure().errorText).toBe('NS_ERROR_FAILURE'); expect(failedRequests[0].frame()).toBeTruthy(); }); Javascript/Typescript - Puppeteer

Slide 131

Slide 131 text

it('Page.Events.RequestFailed', async () => { const { page, server, isChrome } = getTestState(); await page.setRequestInterception(true); page.on('request', (request) => { if (request.url().endsWith('css')) request.abort(); else request.continue(); }); const failedRequests = []; page.on('requestfailed', (request) => failedRequests.push(request)); await page.goto(server.PREFIX + '/one-style.html'); expect(failedRequests.length).toBe(1); expect(failedRequests[0].url()).toContain('one-style.css'); expect(failedRequests[0].response()).toBe(null); expect(failedRequests[0].resourceType()).toBe('stylesheet'); if (isChrome) expect(failedRequests[0].failure().errorText).toBe('net::ERR_FAILED'); else expect(failedRequests[0].failure().errorText).toBe('NS_ERROR_FAILURE'); expect(failedRequests[0].frame()).toBeTruthy(); }); Javascript/Typescript - Puppeteer

Slide 132

Slide 132 text

it('Page.Events.RequestFailed', async () => { const { page, server, isChrome } = getTestState(); await page.setRequestInterception(true); page.on('request', (request) => { if (request.url().endsWith('css')) request.abort(); else request.continue(); }); const failedRequests = []; page.on('requestfailed', (request) => failedRequests.push(request)); await page.goto(server.PREFIX + '/one-style.html'); expect(failedRequests.length).toBe(1); expect(failedRequests[0].url()).toContain('one-style.css'); expect(failedRequests[0].response()).toBe(null); expect(failedRequests[0].resourceType()).toBe('stylesheet'); if (isChrome) expect(failedRequests[0].failure().errorText).toBe('net::ERR_FAILED'); else expect(failedRequests[0].failure().errorText).toBe('NS_ERROR_FAILURE'); expect(failedRequests[0].frame()).toBeTruthy(); }); Javascript/Typescript - Puppeteer

Slide 133

Slide 133 text

it('Page.Events.RequestFailed', async () => { const { page, server, isChrome } = getTestState(); await page.setRequestInterception(true); page.on('request', (request) => { if (request.url().endsWith('css')) request.abort(); else request.continue(); }); const failedRequests = []; page.on('requestfailed', (request) => failedRequests.push(request)); await page.goto(server.PREFIX + '/one-style.html'); expect(failedRequests.length).toBe(1); expect(failedRequests[0].url()).toContain('one-style.css'); expect(failedRequests[0].response()).toBe(null); expect(failedRequests[0].resourceType()).toBe('stylesheet'); if (isChrome) expect(failedRequests[0].failure().errorText).toBe('net::ERR_FAILED'); else expect(failedRequests[0].failure().errorText).toBe('NS_ERROR_FAILURE'); expect(failedRequests[0].frame()).toBeTruthy(); }); Javascript/Typescript - Puppeteer

Slide 134

Slide 134 text

it('Page.Events.RequestFailed', async () => { const { page, server, isChrome } = getTestState(); await page.setRequestInterception(true); page.on('request', (request) => { if (request.url().endsWith('css')) request.abort(); else request.continue(); }); const failedRequests = []; page.on('requestfailed', (request) => failedRequests.push(request)); await page.goto(server.PREFIX + '/one-style.html'); expect(failedRequests.length).toBe(1); expect(failedRequests[0].url()).toContain('one-style.css'); expect(failedRequests[0].response()).toBe(null); expect(failedRequests[0].resourceType()).toBe('stylesheet'); if (isChrome) expect(failedRequests[0].failure().errorText).toBe('net::ERR_FAILED'); else expect(failedRequests[0].failure().errorText).toBe('NS_ERROR_FAILURE'); expect(failedRequests[0].frame()).toBeTruthy(); }); Javascript/Typescript - Puppeteer

Slide 135

Slide 135 text

public class ToolLocationTest { @Rule public JenkinsRule j = new JenkinsRule(); @Test public void toolCompatibility() { Maven.MavenInstallation[] maven = j.jenkins.getDescriptorByType(Maven.DescriptorImpl.class).getInstallations(); assertEquals(1, maven.length); assertEquals("bar", maven[0].getHome()); assertEquals("Maven 1", maven[0].getName()); Ant.AntInstallation[] ant = j.jenkins.getDescriptorByType(Ant.DescriptorImpl.class).getInstallations(); assertEquals(1, ant.length); assertEquals("foo", ant[0].getHome()); assertEquals("Ant 1", ant[0].getName()); JDK[] jdk = j.jenkins.getDescriptorByType(JDK.DescriptorImpl.class).getInstallations(); assertEquals(Arrays.asList(jdk), j.jenkins.getJDKs()); assertEquals(2, jdk.length); // JenkinsRule adds a 'default' JDK assertEquals("default", jdk[1].getName()); // make sure it's really that we're seeing assertEquals("FOOBAR", jdk[0].getHome()); Java - Jenkins

Slide 136

Slide 136 text

public class ToolLocationTest { @Rule public JenkinsRule j = new JenkinsRule(); @Test public void toolCompatibility() { Maven.MavenInstallation[] maven = j.jenkins.getDescriptorByType(Maven.DescriptorImpl.class).getInstallations(); assertEquals(1, maven.length); assertEquals("bar", maven[0].getHome()); assertEquals("Maven 1", maven[0].getName()); Ant.AntInstallation[] ant = j.jenkins.getDescriptorByType(Ant.DescriptorImpl.class).getInstallations(); assertEquals(1, ant.length); assertEquals("foo", ant[0].getHome()); assertEquals("Ant 1", ant[0].getName()); JDK[] jdk = j.jenkins.getDescriptorByType(JDK.DescriptorImpl.class).getInstallations(); assertEquals(Arrays.asList(jdk), j.jenkins.getJDKs()); assertEquals(2, jdk.length); // JenkinsRule adds a 'default' JDK assertEquals("default", jdk[1].getName()); // make sure it's really that we're seeing assertEquals("FOOBAR", jdk[0].getHome()); Java - Jenkins

Slide 137

Slide 137 text

public class ToolLocationTest { @Rule public JenkinsRule j = new JenkinsRule(); @Test public void toolCompatibility() { Maven.MavenInstallation[] maven = j.jenkins.getDescriptorByType(Maven.DescriptorImpl.class).getInstallations(); assertEquals(1, maven.length); assertEquals("bar", maven[0].getHome()); assertEquals("Maven 1", maven[0].getName()); Ant.AntInstallation[] ant = j.jenkins.getDescriptorByType(Ant.DescriptorImpl.class).getInstallations(); assertEquals(1, ant.length); assertEquals("foo", ant[0].getHome()); assertEquals("Ant 1", ant[0].getName()); JDK[] jdk = j.jenkins.getDescriptorByType(JDK.DescriptorImpl.class).getInstallations(); assertEquals(Arrays.asList(jdk), j.jenkins.getJDKs()); assertEquals(2, jdk.length); // JenkinsRule adds a 'default' JDK assertEquals("default", jdk[1].getName()); // make sure it's really that we're seeing assertEquals("FOOBAR", jdk[0].getHome()); Java - Jenkins

Slide 138

Slide 138 text

public class ToolLocationTest { @Rule public JenkinsRule j = new JenkinsRule(); @Test public void toolCompatibility() { Maven.MavenInstallation[] maven = j.jenkins.getDescriptorByType(Maven.DescriptorImpl.class).getInstallations(); assertEquals(1, maven.length); assertEquals("bar", maven[0].getHome()); assertEquals("Maven 1", maven[0].getName()); Ant.AntInstallation[] ant = j.jenkins.getDescriptorByType(Ant.DescriptorImpl.class).getInstallations(); assertEquals(1, ant.length); assertEquals("foo", ant[0].getHome()); assertEquals("Ant 1", ant[0].getName()); JDK[] jdk = j.jenkins.getDescriptorByType(JDK.DescriptorImpl.class).getInstallations(); assertEquals(Arrays.asList(jdk), j.jenkins.getJDKs()); assertEquals(2, jdk.length); // JenkinsRule adds a 'default' JDK assertEquals("default", jdk[1].getName()); // make sure it's really that we're seeing assertEquals("FOOBAR", jdk[0].getHome()); Java - Jenkins

Slide 139

Slide 139 text

public class ToolLocationTest { @Rule public JenkinsRule j = new JenkinsRule(); @Test public void toolCompatibility() { Maven.MavenInstallation[] maven = j.jenkins.getDescriptorByType(Maven.DescriptorImpl.class).getInstallations(); assertEquals(1, maven.length); assertEquals("bar", maven[0].getHome()); assertEquals("Maven 1", maven[0].getName()); Ant.AntInstallation[] ant = j.jenkins.getDescriptorByType(Ant.DescriptorImpl.class).getInstallations(); assertEquals(1, ant.length); assertEquals("foo", ant[0].getHome()); assertEquals("Ant 1", ant[0].getName()); JDK[] jdk = j.jenkins.getDescriptorByType(JDK.DescriptorImpl.class).getInstallations(); assertEquals(Arrays.asList(jdk), j.jenkins.getJDKs()); assertEquals(2, jdk.length); // JenkinsRule adds a 'default' JDK assertEquals("default", jdk[1].getName()); // make sure it's really that we're seeing assertEquals("FOOBAR", jdk[0].getHome()); Java - Jenkins

Slide 140

Slide 140 text

The automation server

Slide 141

Slide 141 text

public class ToolLocationTest { @Test @LocalData public void shouldBeCompatibleWithMaven() { Maven.MavenInstallation[] maven = j.jenkins.getDescriptorByType(Maven.DescriptorImpl.class).getInstallations(); assertEquals(1, maven.length); assertEquals("bar", maven[0].getHome()); assertEquals("Maven 1", maven[0].getName()); } @Test @LocalData public void shouldBeCompatibleWithAnt() { Ant.AntInstallation[] ant = j.jenkins.getDescriptorByType(Ant.DescriptorImpl.class).getInstallations(); assertEquals(1, ant.length); assertEquals("foo", ant[0].getHome()); assertEquals("Ant 1", ant[0].getName()); } @Test @LocalData Java - Jenkins

Slide 142

Slide 142 text

@Test @LocalData public void shouldBeCompatibleWithJdk() { JDK[] jdk = j.jenkins.getDescriptorByType(JDK.DescriptorImpl.class).getInstallations(); assertEquals(Arrays.asList(jdk), j.jenkins.getJDKs()); assertEquals(2, jdk.length); // JenkinsRule adds a 'default' JDK assertEquals("default", jdk[1].getName()); // make sure it's really that we're seeing assertEquals("FOOBAR", jdk[0].getHome()); assertEquals("FOOBAR", jdk[0].getJavaHome()); assertEquals("1.6", jdk[0].getName()); } } Java - Jenkins

Slide 143

Slide 143 text

6. Wrapping up We are almost done! Crafting code

Slide 144

Slide 144 text

● The stranger ● The operating system evangelist ● Success against all odds ● The free ride ● and two more left! What we covered

Slide 145

Slide 145 text

https://www.codurance.com/publications/building-testing-culture https://www.codurance.com/publications/tdd-anti-patterns-chapter-1

Slide 146

Slide 146 text

https://www.codurance.com/publications/building-testing-culture https://www.codurance.com/publications/building-testing-culture

Slide 147

Slide 147 text

https://www.codurance.com/events

Slide 148

Slide 148 text

Matheus Marabesi Hello there, you can call me Marabesi, But my name is Matheus Marabesi, I work at Codurance as a Software craftsperson. I enjoy talking about anything related to: testing, patterns and gamification. You can find me at @MatheusMarabesi or https://marabesi.com Codurance Crafting Code