Slide 1

Slide 1 text

TDD - EP 6 codurance.com Testing anti-patterns - The One, The Peeping Tom, The jumper and The flash

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 One 3. The Peeping Tom 4. The Flash 5. The Jumper 6. Wrapping up Crafting code Agenda

Slide 4

Slide 4 text

1. Intro - Recap 2. The One 3. The Peeping Tom 4. The Flash 5. The Jumper 6. Wrapping up Crafting code Agenda

Slide 5

Slide 5 text

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

Slide 6

Slide 6 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 7

Slide 7 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 7 The Operating System Evangelist 8 Success Against All Odds 5 The Free Ride 8 The One 7 The Peeping Tom 7 The Slow Poke 6

Slide 8

Slide 8 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 9

Slide 9 text

MOST POPULAR

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

MOST POPULAR

Slide 13

Slide 13 text

2. Anti-patterns - Episode 6 The One, The Peeping Tom, The jumper and The flash 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 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 7 The Operating System Evangelist 8 Success Against All Odds 5 The Free Ride 8 The One 7 The Peeping Tom 7 The Slow Poke 6

Slide 16

Slide 16 text

2. The One - 🏆 7 Crafting code

Slide 17

Slide 17 text

2. The One - 🏆 7 A combination of several patterns, particularly TheFreeRide and TheGiant, a unit test that contains only one test method which tests the entire set of functionality an object has. A common indicator is that the test method is often the same as the unit test name, and contains multiple lines of setup and assertions. Crafting code

Slide 18

Slide 18 text

2. The One - 🏆 7 A combination of several patterns, particularly TheFreeRide and TheGiant, a unit test that contains only one test method which tests the entire set of functionality an object has. A common indicator is that the test method is often the same as the unit test name, and contains multiple lines of setup and assertions. Crafting code

Slide 19

Slide 19 text

2. The One - 🏆 7 A combination of several patterns, particularly TheFreeRide and TheGiant, a unit test that contains only one test method which tests the entire set of functionality an object has. A common indicator is that the test method is often the same as the unit test name, and contains multiple lines of setup and assertions. Crafting code

Slide 20

Slide 20 text

The giant A unit test that, although it is validly testing the object under test, can span thousands of lines and contain many many test cases. This can be an indicator that the system under tests is a God Object. Crafting code

Slide 21

Slide 21 text

namespace PhpOffice\PhpWord\Shared; class ConverterTest extends \PHPUnit\Framework\TestCase { public function testUnitConversions() { $values = array(); $values[] = 0; // zero value $values[] = rand(1, 100) / 100; // fraction number $values[] = rand(1, 100); // integer foreach ($values as $value) { $result = Converter::cmToTwip($value); $this->assertEquals($value / 2.54 * 1440, $result); $result = Converter::cmToInch($value); $this->assertEquals($value / 2.54, $result); PHPUnit - PHPOffice/Word

Slide 22

Slide 22 text

namespace PhpOffice\PhpWord\Shared; class ConverterTest extends \PHPUnit\Framework\TestCase { public function testUnitConversions() { $values = array(); $values[] = 0; // zero value $values[] = rand(1, 100) / 100; // fraction number $values[] = rand(1, 100); // integer foreach ($values as $value) { $result = Converter::cmToTwip($value); $this->assertEquals($value / 2.54 * 1440, $result); $result = Converter::cmToInch($value); $this->assertEquals($value / 2.54, $result); PHPUnit - PHPOffice/Word

Slide 23

Slide 23 text

namespace PhpOffice\PhpWord\Shared; class ConverterTest extends \PHPUnit\Framework\TestCase { public function testUnitConversions() { $values = array(); $values[] = 0; // zero value $values[] = rand(1, 100) / 100; // fraction number $values[] = rand(1, 100); // integer foreach ($values as $value) { $result = Converter::cmToTwip($value); $this->assertEquals($value / 2.54 * 1440, $result); $result = Converter::cmToInch($value); $this->assertEquals($value / 2.54, $result); PHPUnit - PHPOffice/Word

Slide 24

Slide 24 text

namespace PhpOffice\PhpWord\Shared; class ConverterTest extends \PHPUnit\Framework\TestCase { public function testUnitConversions() { $values = array(); $values[] = 0; // zero value $values[] = rand(1, 100) / 100; // fraction number $values[] = rand(1, 100); // integer foreach ($values as $value) { $result = Converter::cmToTwip($value); $this->assertEquals($value / 2.54 * 1440, $result); $result = Converter::cmToInch($value); $this->assertEquals($value / 2.54, $result); PHPUnit - PHPOffice/Word

Slide 25

Slide 25 text

namespace PhpOffice\PhpWord\Shared; class ConverterTest extends \PHPUnit\Framework\TestCase { public function testUnitConversions() { $values = array(); $values[] = 0; // zero value $values[] = rand(1, 100) / 100; // fraction number $values[] = rand(1, 100); // integer foreach ($values as $value) { $result = Converter::cmToTwip($value); $this->assertEquals($value / 2.54 * 1440, $result); $result = Converter::cmToInch($value); $this->assertEquals($value / 2.54, $result); PHPUnit - PHPOffice/Word

Slide 26

Slide 26 text

$result = Converter::cmToPixel($value); $this->assertEquals($value / 2.54 * 96, $result); $result = Converter::cmToPoint($value); $this->assertEquals($value / 2.54 * 72, $result); $result = Converter::cmToEmu($value); $this->assertEquals(round($value / 2.54 * 96 * 9525), $result); $result = Converter::inchToTwip($value); $this->assertEquals($value * 1440, $result); $result = Converter::inchToCm($value); $this->assertEquals($value * 2.54, $result); $result = Converter::inchToPixel($value); $this->assertEquals($value * 96, $result); PHPUnit - PHPOffice/Word

Slide 27

Slide 27 text

$result = Converter::cmToPixel($value); $this->assertEquals($value / 2.54 * 96, $result); $result = Converter::cmToPoint($value); $this->assertEquals($value / 2.54 * 72, $result); $result = Converter::cmToEmu($value); $this->assertEquals(round($value / 2.54 * 96 * 9525), $result); $result = Converter::inchToTwip($value); $this->assertEquals($value * 1440, $result); $result = Converter::inchToCm($value); $this->assertEquals($value * 2.54, $result); $result = Converter::inchToPixel($value); $this->assertEquals($value * 96, $result); PHPUnit - PHPOffice/Word

Slide 28

Slide 28 text

$result = Converter::cmToPixel($value); $this->assertEquals($value / 2.54 * 96, $result); $result = Converter::cmToPoint($value); $this->assertEquals($value / 2.54 * 72, $result); $result = Converter::cmToEmu($value); $this->assertEquals(round($value / 2.54 * 96 * 9525), $result); $result = Converter::inchToTwip($value); $this->assertEquals($value * 1440, $result); $result = Converter::inchToCm($value); $this->assertEquals($value * 2.54, $result); $result = Converter::inchToPixel($value); $this->assertEquals($value * 96, $result); PHPUnit - PHPOffice/Word

Slide 29

Slide 29 text

$result = Converter::cmToPixel($value); $this->assertEquals($value / 2.54 * 96, $result); $result = Converter::cmToPoint($value); $this->assertEquals($value / 2.54 * 72, $result); $result = Converter::cmToEmu($value); $this->assertEquals(round($value / 2.54 * 96 * 9525), $result); $result = Converter::inchToTwip($value); $this->assertEquals($value * 1440, $result); $result = Converter::inchToCm($value); $this->assertEquals($value * 2.54, $result); $result = Converter::inchToPixel($value); $this->assertEquals($value * 96, $result); PHPUnit - PHPOffice/Word

Slide 30

Slide 30 text

$result = Converter::cmToPixel($value); $this->assertEquals($value / 2.54 * 96, $result); $result = Converter::cmToPoint($value); $this->assertEquals($value / 2.54 * 72, $result); $result = Converter::cmToEmu($value); $this->assertEquals(round($value / 2.54 * 96 * 9525), $result); $result = Converter::inchToTwip($value); $this->assertEquals($value * 1440, $result); $result = Converter::inchToCm($value); $this->assertEquals($value * 2.54, $result); $result = Converter::inchToPixel($value); $this->assertEquals($value * 96, $result); PHPUnit - PHPOffice/Word

Slide 31

Slide 31 text

$result = Converter::cmToPixel($value); $this->assertEquals($value / 2.54 * 96, $result); $result = Converter::cmToPoint($value); $this->assertEquals($value / 2.54 * 72, $result); $result = Converter::cmToEmu($value); $this->assertEquals(round($value / 2.54 * 96 * 9525), $result); $result = Converter::inchToTwip($value); $this->assertEquals($value * 1440, $result); $result = Converter::inchToCm($value); $this->assertEquals($value * 2.54, $result); $result = Converter::inchToPixel($value); $this->assertEquals($value * 96, $result); PHPUnit - PHPOffice/Word

Slide 32

Slide 32 text

$result = Converter::inchToPoint($value); $this->assertEquals($value * 72, $result); $result = Converter::inchToEmu($value); $this->assertEquals(round($value * 96 * 9525), $result); $result = Converter::pixelToTwip($value); $this->assertEquals($value / 96 * 1440, $result); $result = Converter::pixelToCm($value); $this->assertEquals($value / 96 * 2.54, $result); $result = Converter::pixelToPoint($value); $this->assertEquals($value / 96 * 72, $result); $result = Converter::pixelToEmu($value); $this->assertEquals(round($value * 9525), $result); PHPUnit - PHPOffice/Word

Slide 33

Slide 33 text

$result = Converter::inchToPoint($value); $this->assertEquals($value * 72, $result); $result = Converter::inchToEmu($value); $this->assertEquals(round($value * 96 * 9525), $result); $result = Converter::pixelToTwip($value); $this->assertEquals($value / 96 * 1440, $result); $result = Converter::pixelToCm($value); $this->assertEquals($value / 96 * 2.54, $result); $result = Converter::pixelToPoint($value); $this->assertEquals($value / 96 * 72, $result); $result = Converter::pixelToEmu($value); $this->assertEquals(round($value * 9525), $result); PHPUnit - PHPOffice/Word

Slide 34

Slide 34 text

$result = Converter::inchToPoint($value); $this->assertEquals($value * 72, $result); $result = Converter::inchToEmu($value); $this->assertEquals(round($value * 96 * 9525), $result); $result = Converter::pixelToTwip($value); $this->assertEquals($value / 96 * 1440, $result); $result = Converter::pixelToCm($value); $this->assertEquals($value / 96 * 2.54, $result); $result = Converter::pixelToPoint($value); $this->assertEquals($value / 96 * 72, $result); $result = Converter::pixelToEmu($value); $this->assertEquals(round($value * 9525), $result); PHPUnit - PHPOffice/Word

Slide 35

Slide 35 text

$result = Converter::inchToPoint($value); $this->assertEquals($value * 72, $result); $result = Converter::inchToEmu($value); $this->assertEquals(round($value * 96 * 9525), $result); $result = Converter::pixelToTwip($value); $this->assertEquals($value / 96 * 1440, $result); $result = Converter::pixelToCm($value); $this->assertEquals($value / 96 * 2.54, $result); $result = Converter::pixelToPoint($value); $this->assertEquals($value / 96 * 72, $result); $result = Converter::pixelToEmu($value); $this->assertEquals(round($value * 9525), $result); PHPUnit - PHPOffice/Word

Slide 36

Slide 36 text

$result = Converter::inchToPoint($value); $this->assertEquals($value * 72, $result); $result = Converter::inchToEmu($value); $this->assertEquals(round($value * 96 * 9525), $result); $result = Converter::pixelToTwip($value); $this->assertEquals($value / 96 * 1440, $result); $result = Converter::pixelToCm($value); $this->assertEquals($value / 96 * 2.54, $result); $result = Converter::pixelToPoint($value); $this->assertEquals($value / 96 * 72, $result); $result = Converter::pixelToEmu($value); $this->assertEquals(round($value * 9525), $result); PHPUnit - PHPOffice/Word

Slide 37

Slide 37 text

$result = Converter::inchToPoint($value); $this->assertEquals($value * 72, $result); $result = Converter::inchToEmu($value); $this->assertEquals(round($value * 96 * 9525), $result); $result = Converter::pixelToTwip($value); $this->assertEquals($value / 96 * 1440, $result); $result = Converter::pixelToCm($value); $this->assertEquals($value / 96 * 2.54, $result); $result = Converter::pixelToPoint($value); $this->assertEquals($value / 96 * 72, $result); $result = Converter::pixelToEmu($value); $this->assertEquals(round($value * 9525), $result); PHPUnit - PHPOffice/Word

Slide 38

Slide 38 text

$result = Converter::degreeToAngle($value); $this->assertEquals((int) round($value * 60000), $result); $result = Converter::angleToDegree($value); $this->assertEquals(round($value / 60000), $result); } } PHPUnit - PHPOffice/Word

Slide 39

Slide 39 text

public void testFlightMileage_asKm2() throws Exception { // set up fixture // exercise constructor Flight newFlight = new Flight(validFlightNumber); // verify constructed object assertEquals(validFlightNumber, newFlight.number); assertEquals("", newFlight.airlineCode); assertNull(newFlight.airline); // set up mileage newFlight.setMileage(1122); // exercise mileage translator int actualKilometres = newFlight.getMileageAsKm(); // verify results int expectedKilometres = 1810; assertEquals( expectedKilometres, actualKilometres); // now try it with a canceled flight newFlight.cancel(); try { newFlight.getMileageAsKm(); fail("Expected exception"); } catch (InvalidRequestException e) { assertEquals( "Cannot get cancelled flight mileage", Assertion Roulette - xUnit

Slide 40

Slide 40 text

public void testFlightMileage_asKm2() throws Exception { // set up fixture // exercise constructor Flight newFlight = new Flight(validFlightNumber); // verify constructed object assertEquals(validFlightNumber, newFlight.number); assertEquals("", newFlight.airlineCode); assertNull(newFlight.airline); // set up mileage newFlight.setMileage(1122); // exercise mileage translator int actualKilometres = newFlight.getMileageAsKm(); // verify results int expectedKilometres = 1810; assertEquals( expectedKilometres, actualKilometres); // now try it with a canceled flight newFlight.cancel(); try { newFlight.getMileageAsKm(); fail("Expected exception"); } catch (InvalidRequestException e) { assertEquals( "Cannot get cancelled flight mileage", Assertion Roulette - xUnit

Slide 41

Slide 41 text

public void testFlightMileage_asKm2() throws Exception { // set up fixture // exercise constructor Flight newFlight = new Flight(validFlightNumber); // verify constructed object assertEquals(validFlightNumber, newFlight.number); assertEquals("", newFlight.airlineCode); assertNull(newFlight.airline); // set up mileage newFlight.setMileage(1122); // exercise mileage translator int actualKilometres = newFlight.getMileageAsKm(); // verify results int expectedKilometres = 1810; assertEquals( expectedKilometres, actualKilometres); // now try it with a canceled flight newFlight.cancel(); try { newFlight.getMileageAsKm(); fail("Expected exception"); } catch (InvalidRequestException e) { assertEquals( "Cannot get cancelled flight mileage", Assertion Roulette - xUnit

Slide 42

Slide 42 text

public void testFlightMileage_asKm2() throws Exception { // set up fixture // exercise constructor Flight newFlight = new Flight(validFlightNumber); // verify constructed object assertEquals(validFlightNumber, newFlight.number); assertEquals("", newFlight.airlineCode); assertNull(newFlight.airline); // set up mileage newFlight.setMileage(1122); // exercise mileage translator int actualKilometres = newFlight.getMileageAsKm(); // verify results int expectedKilometres = 1810; assertEquals( expectedKilometres, actualKilometres); // now try it with a canceled flight newFlight.cancel(); try { newFlight.getMileageAsKm(); fail("Expected exception"); } catch (InvalidRequestException e) { assertEquals( "Cannot get cancelled flight mileage", Assertion Roulette - xUnit

Slide 43

Slide 43 text

public void testFlightMileage_asKm2() throws Exception { // set up fixture // exercise constructor Flight newFlight = new Flight(validFlightNumber); // verify constructed object assertEquals(validFlightNumber, newFlight.number); assertEquals("", newFlight.airlineCode); assertNull(newFlight.airline); // set up mileage newFlight.setMileage(1122); // exercise mileage translator int actualKilometres = newFlight.getMileageAsKm(); // verify results int expectedKilometres = 1810; assertEquals( expectedKilometres, actualKilometres); // now try it with a canceled flight newFlight.cancel(); try { newFlight.getMileageAsKm(); fail("Expected exception"); } catch (InvalidRequestException e) { assertEquals( "Cannot get cancelled flight mileage", Assertion Roulette - xUnit

Slide 44

Slide 44 text

try { newFlight.getMileageAsKm(); fail("Expected exception"); } catch (InvalidRequestException e) { assertEquals( "Cannot get cancelled flight mileage", e.getMessage()); } } Assertion Roulette - xUnit

Slide 45

Slide 45 text

try { newFlight.getMileageAsKm(); fail("Expected exception"); } catch (InvalidRequestException e) { assertEquals( "Cannot get cancelled flight mileage", e.getMessage()); } } Assertion Roulette - xUnit

Slide 46

Slide 46 text

try { newFlight.getMileageAsKm(); fail("Expected exception"); } catch (InvalidRequestException e) { assertEquals( "Cannot get cancelled flight mileage", e.getMessage()); } } Assertion Roulette - xUnit

Slide 47

Slide 47 text

try { newFlight.getMileageAsKm(); fail("Expected exception"); } catch (InvalidRequestException e) { assertEquals( "Cannot get cancelled flight mileage", e.getMessage()); } } Assertion Roulette - xUnit

Slide 48

Slide 48 text

The Free Ride 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 49

Slide 49 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 50

Slide 50 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 51

Slide 51 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 52

Slide 52 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 53

Slide 53 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 54

Slide 54 text

The automation server

Slide 55

Slide 55 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 56

Slide 56 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 57

Slide 57 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 58

Slide 58 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 59

Slide 59 text

Web automation

Slide 60

Slide 60 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 61

Slide 61 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 62

Slide 62 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 63

Slide 63 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 64

Slide 64 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 65

Slide 65 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 66

Slide 66 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 67

Slide 67 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 68

Slide 68 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 69

Slide 69 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 70

Slide 70 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 71

Slide 71 text

3. The Peeping Tom - 🏆7 Crafting code

Slide 72

Slide 72 text

3. The Peeping Tom - 🏆7 A test that, due to shared resources, can see the result data of another test, and may cause the test to fail even though the system under test is perfectly valid. This has been seen commonly in fitnesse, where the use of static member variables to hold collections aren’t properly cleaned after test execution, often popping up unexpectedly in other test runs. Also known as TheUninvitedGuests Crafting code

Slide 73

Slide 73 text

3. The Peeping Tom - 🏆7 A test that, due to shared resources, can see the result data of another test, and may cause the test to fail even though the system under test is perfectly valid. This has been seen commonly in fitnesse, where the use of static member variables to hold collections aren’t properly cleaned after test execution, often popping up unexpectedly in other test runs. Also known as TheUninvitedGuests Crafting code

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

public class MySingleton{ private static MySingleton instance; private String property; private MySingleton(String property) { this.property = property; } public static synchronized MySingleton getInstance() { if (instance == null) { instance = new MySingleton(System.getProperty("com.example")); } return instance; } public Object getSomething() { return this.property; } } Java - Singleton

Slide 77

Slide 77 text

public class MySingleton{ private static MySingleton instance; private String property; private MySingleton(String property) { this.property = property; } public static synchronized MySingleton getInstance() { if (instance == null) { instance = new MySingleton(System.getProperty("com.example")); } return instance; } public Object getSomething() { return this.property; } } Java - Singleton

Slide 78

Slide 78 text

public class MySingleton{ private static MySingleton instance; private String property; private MySingleton(String property) { this.property = property; } public static synchronized MySingleton getInstance() { if (instance == null) { instance = new MySingleton(System.getProperty("com.example")); } return instance; } public Object getSomething() { return this.property; } } Java - Singleton

Slide 79

Slide 79 text

public class MySingleton{ private static MySingleton instance; private String property; private MySingleton(String property) { this.property = property; } public static synchronized MySingleton getInstance() { if (instance == null) { instance = new MySingleton(System.getProperty("com.example")); } return instance; } public Object getSomething() { return this.property; } } Java - Singleton

Slide 80

Slide 80 text

public class MySingleton{ private static MySingleton instance; private String property; private MySingleton(String property) { this.property = property; } public static synchronized MySingleton getInstance() { if (instance == null) { instance = new MySingleton(System.getProperty("com.example")); } return instance; } public Object getSomething() { return this.property; } } Java - Singleton

Slide 81

Slide 81 text

public class MySingleton{ private static MySingleton instance; private String property; private MySingleton(String property) { this.property = property; } public static synchronized MySingleton getInstance() { if (instance == null) { instance = new MySingleton(System.getProperty("com.example")); } return instance; } public Object getSomething() { return this.property; } } Java - Singleton

Slide 82

Slide 82 text

public class MySingleton{ private static MySingleton instance; private String property; private MySingleton(String property) { this.property = property; } public static synchronized MySingleton getInstance() { if (instance == null) { instance = new MySingleton(System.getProperty("com.example")); } return instance; } public Object getSomething() { return this.property; } } Java - Singleton

Slide 83

Slide 83 text

import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; class MySingletonTest { @Test public void somethingIsDoneWithAbcIsSetAsASystemProperty(){ System.setProperty("com.example", "abc"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("abc"); } @Test public void somethingElseIsDoneWithXyzIsSetAsASystemProperty(){ System.setProperty("com.example", "xyz"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("xyz"); } } Java - Singleton

Slide 84

Slide 84 text

import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; class MySingletonTest { @Test public void somethingIsDoneWithAbcIsSetAsASystemProperty(){ System.setProperty("com.example", "abc"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("abc"); } @Test public void somethingElseIsDoneWithXyzIsSetAsASystemProperty(){ System.setProperty("com.example", "xyz"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("xyz"); } } Java - Singleton

Slide 85

Slide 85 text

import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; class MySingletonTest { @Test public void somethingIsDoneWithAbcIsSetAsASystemProperty(){ System.setProperty("com.example", "abc"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("abc"); } @Test public void somethingElseIsDoneWithXyzIsSetAsASystemProperty(){ System.setProperty("com.example", "xyz"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("xyz"); } } Java - Singleton

Slide 86

Slide 86 text

import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; class MySingletonTest { @Test public void somethingIsDoneWithAbcIsSetAsASystemProperty(){ System.setProperty("com.example", "abc"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("abc"); } @Test public void somethingElseIsDoneWithXyzIsSetAsASystemProperty(){ System.setProperty("com.example", "xyz"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("xyz"); } } Java - Singleton

Slide 87

Slide 87 text

import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; class MySingletonTest { @Test public void somethingIsDoneWithAbcIsSetAsASystemProperty(){ System.setProperty("com.example", "abc"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("abc"); } @Test public void somethingElseIsDoneWithXyzIsSetAsASystemProperty(){ System.setProperty("com.example", "xyz"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("xyz"); } } Java - Singleton

Slide 88

Slide 88 text

import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; class MySingletonTest { @Test public void somethingIsDoneWithAbcIsSetAsASystemProperty(){ System.setProperty("com.example", "abc"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("abc"); } @Test public void somethingElseIsDoneWithXyzIsSetAsASystemProperty(){ System.setProperty("com.example", "xyz"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("xyz"); } } Java - Singleton

Slide 89

Slide 89 text

import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; class MySingletonTest { @Test public void somethingIsDoneWithAbcIsSetAsASystemProperty(){ System.setProperty("com.example", "abc"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("abc"); } @Test public void somethingElseIsDoneWithXyzIsSetAsASystemProperty(){ System.setProperty("com.example", "xyz"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("xyz"); } } Java - Singleton

Slide 90

Slide 90 text

import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; class MySingletonTest { @Test public void somethingIsDoneWithAbcIsSetAsASystemProperty(){ System.setProperty("com.example", "abc"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("abc"); } @Test public void somethingElseIsDoneWithXyzIsSetAsASystemProperty(){ System.setProperty("com.example", "xyz"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("xyz"); } } Java - Singleton ✅

Slide 91

Slide 91 text

import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; class MySingletonTest { @Test public void somethingIsDoneWithAbcIsSetAsASystemProperty(){ System.setProperty("com.example", "abc"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("abc"); } @Test public void somethingElseIsDoneWithXyzIsSetAsASystemProperty(){ System.setProperty("com.example", "xyz"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("xyz"); } } Java - Singleton

Slide 92

Slide 92 text

import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; class MySingletonTest { @Test public void somethingIsDoneWithAbcIsSetAsASystemProperty(){ System.setProperty("com.example", "abc"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("abc"); } @Test public void somethingElseIsDoneWithXyzIsSetAsASystemProperty(){ System.setProperty("com.example", "xyz"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("xyz"); } } Java - Singleton ❌

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

class MySingletonTest { @BeforeEach public void resetSingleton() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field instance = MySingleton.class.getDeclaredField("instance"); instance.setAccessible(true); instance.set(null, null); } } Java - Singleton

Slide 95

Slide 95 text

class MySingletonTest { @BeforeEach public void resetSingleton() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field instance = MySingleton.class.getDeclaredField("instance"); instance.setAccessible(true); instance.set(null, null); } } Java - Singleton

Slide 96

Slide 96 text

class MySingletonTest { @BeforeEach public void resetSingleton() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field instance = MySingleton.class.getDeclaredField("instance"); instance.setAccessible(true); instance.set(null, null); } } Java - Singleton

Slide 97

Slide 97 text

class MySingletonTest { @BeforeEach public void resetSingleton() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field instance = MySingleton.class.getDeclaredField("instance"); instance.setAccessible(true); instance.set(null, null); } } Java - Singleton

Slide 98

Slide 98 text

import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; class MySingletonTest { @Test public void somethingIsDoneWithAbcIsSetAsASystemProperty(){ System.setProperty("com.example", "abc"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("abc"); } @Test public void somethingElseIsDoneWithXyzIsSetAsASystemProperty(){ System.setProperty("com.example", "xyz"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("xyz"); } } Java - Singleton ✅

Slide 99

Slide 99 text

import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; class MySingletonTest { @Test public void somethingIsDoneWithAbcIsSetAsASystemProperty(){ System.setProperty("com.example", "abc"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("abc"); } @Test public void somethingElseIsDoneWithXyzIsSetAsASystemProperty(){ System.setProperty("com.example", "xyz"); MySingleton singleton = MySingleton.getInstance(); assertThat(singleton.getSomething()).isEqualTo("xyz"); } } Java - Singleton ✅

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

No content

Slide 107

Slide 107 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 7 The Operating System Evangelist 8 Success Against All Odds 5 The Free Ride 8 The One 7 The Peeping Tom 7 The Slow Poke 6

Slide 108

Slide 108 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 7 The Operating System Evangelist 8 Success Against All Odds 5 The Free Ride 8 The One 7 The Peeping Tom 7 The Slow Poke 6

Slide 109

Slide 109 text

● The One ● The Peeping Tom What we covered

Slide 110

Slide 110 text

● The One ○ Anti patterns can be mixed between each other ○ In open source projects it is usually a mix ● The Peeping Tom What we covered

Slide 111

Slide 111 text

● The One ○ Anti patterns can be mixed between each other ○ In open source projects it is usually a mix ● The Peeping Tom ○ Once again, avoid global state What we covered

Slide 112

Slide 112 text

4. The flash ⚡ Crafting code

Slide 113

Slide 113 text

4. The flash ⚡ Developers that gets blocked trying to practice TDD, and before splitting up the test in smaller chunks, they are thinking about all the edge cases that the test would cover. This can lead to another anti patterns itself. Crafting code

Slide 114

Slide 114 text

Test methods should be easy to read, pretty much straight line code. If a test method is getting long and complicated, then you need to play "Baby Steps." Kent Beck - TDD by example

Slide 115

Slide 115 text

Focuses on generalization or performance issues from the start The flash

Slide 116

Slide 116 text

Aggregates It potentially leads to a combination of different anti-patterns, like The One The flash

Slide 117

Slide 117 text

5. The jumper Crafting code

Slide 118

Slide 118 text

5. The jumper The practice of trying to follow the test driven development approach, but jumping up to the next steps of the flow before completing the one before. Crafting code

Slide 119

Slide 119 text

Tdd loop here (and potentially share the xp meetup)

Slide 120

Slide 120 text

Tdd loop here (and potentially share the xp meetup) Trying to make it "perfect" from the start

Slide 121

Slide 121 text

Tdd loop here (and potentially share the xp meetup) Trying to make it "perfect" from the start Get blocked by not knowing what to code

Slide 122

Slide 122 text

Tdd loop here (and potentially share the xp meetup) - Refactor on the red - Changing class name - Fixing styles (any kind of style) - Changing files that are not related to the test Trying to make it "perfect" from the start Get blocked by not knowing what to code

Slide 123

Slide 123 text

Tdd loop here (and potentially share the xp meetup) - Refactor on the red - Changing class name - Fixing styles (any kind of style) - Changing files that are not related to the test Trying to make it "perfect" from the start Get blocked by not knowing what to code

Slide 124

Slide 124 text

Tdd loop here (and potentially share the xp meetup) Not trusting the hard coded ("I know it will pass")

Slide 125

Slide 125 text

Tdd loop here (and potentially share the xp meetup) Making changes that break the tests

Slide 126

Slide 126 text

6. Wrapping up We are almost done! Crafting code

Slide 127

Slide 127 text

● The One ● The Peeping Tom ● The flash ● The jumper What we covered

Slide 128

Slide 128 text

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

Slide 129

Slide 129 text

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

Slide 130

Slide 130 text

https://www.codurance.com/events

Slide 131

Slide 131 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