Slide 1

Slide 1 text

Ragunath Jawahar • @ragunathjawahar • https://ragunath.xyz Building Robust Software Episode 3: Not going to convince you to write tests

Slide 2

Slide 2 text

Slide 3

Slide 3 text

Episode 1: Small ideas, big impact • Robustness principle & a tweaked version of it • Algebraic data types • Total & partial functions • Minimising defensive programming • Using the type system to short-circuit failures (a.k.a failing fast) • Avoiding primitive obsession

Slide 4

Slide 4 text

Episode 2: Exceptions, side-effects and boundaries • Exception anti-patterns • Handling • Throwing • Tester-Doer pattern • Try-Parse Pattern • Converting exceptions to values • Introduction to values as boundaries • Architectures that use values as boundaries

Slide 5

Slide 5 text

Building Robust Software Playlist https://bit.ly/3F2ijgm

Slide 6

Slide 6 text

< / recap>

Slide 7

Slide 7 text

Why this talk?

Slide 8

Slide 8 text

Scenario An event management startup has hired you and needs your help to build a feature that sends reminder emails to potential attendees for an upcoming event.

Slide 9

Slide 9 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 10

Slide 10 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 11

Slide 11 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 12

Slide 12 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 13

Slide 13 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 14

Slide 14 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 15

Slide 15 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 16

Slide 16 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 17

Slide 17 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 18

Slide 18 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 19

Slide 19 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 20

Slide 20 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 21

Slide 21 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 22

Slide 22 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 23

Slide 23 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 24

Slide 24 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 25

Slide 25 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 26

Slide 26 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 27

Slide 27 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 28

Slide 28 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 29

Slide 29 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 30

Slide 30 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 31

Slide 31 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 32

Slide 32 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 33

Slide 33 text

Scenario Your company is losing money 💸 💸 💸 due to bug fi xes in production, so your team has decided to write tests. It's your responsibility to bring your modules under test.

Slide 34

Slide 34 text

🤔

Slide 35

Slide 35 text

No registrations Everyone RSVP’d “Yes” Everyone RSVP’d “No” Everyone RSVP’d “Maybe” A mix of “Yes”, “No”, and “Maybe”

Slide 36

Slide 36 text

No registrations Everyone RSVP’d “Yes” Everyone RSVP’d “No” Everyone RSVP’d “Maybe” A mix of “Yes”, “No”, and “Maybe”

Slide 37

Slide 37 text

No registrations Everyone RSVP’d “Yes” Everyone RSVP’d “No” Everyone RSVP’d “Maybe” A mix of “Yes”, “No”, and “Maybe”

Slide 38

Slide 38 text

No registrations Everyone RSVP’d “Yes” Everyone RSVP’d “No” Everyone RSVP’d “Maybe” A mix of “Yes”, “No”, and “Maybe”

Slide 39

Slide 39 text

No registrations Everyone RSVP’d “Yes” Everyone RSVP’d “No” Everyone RSVP’d “Maybe” A mix of “Yes”, “No”, and “Maybe”

Slide 40

Slide 40 text

No registrations Everyone RSVP’d “Yes” Everyone RSVP’d “No” Everyone RSVP’d “Maybe” A mix of “Yes”, “No”, and “Maybe”

Slide 41

Slide 41 text

Test: No registrations

Slide 42

Slide 42 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 43

Slide 43 text

@Test fun `don't send emails when there are no registrants`() { // given val dao = mock() val mailer = mock() val eventManager = EventManager(dao, mailer) whenever(dao.getRegistrants(event.id)).thenReturn(emptyList()) val event = Event( "Building Robust Software III", "Event is on 16th December, please attend." ) // when eventManager.sendReminder(event) // then verifyNoInteractions(mailer) }

Slide 44

Slide 44 text

@Test fun `don't send emails when there are no registrants`() { // given val dao = mock() val mailer = mock() val eventManager = EventManager(dao, mailer) whenever(dao.getRegistrants(event.id)).thenReturn(emptyList()) val event = Event( "Building Robust Software III", "Event is on 16th December, please attend." ) // when eventManager.sendReminder(event) // then verifyNoInteractions(mailer) }

Slide 45

Slide 45 text

@Test fun `don't send emails when there are no registrants`() { // given val dao = mock() val mailer = mock() val eventManager = EventManager(dao, mailer) whenever(dao.getRegistrants(event.id)).thenReturn(emptyList()) val event = Event( "Building Robust Software III", "Event is on 16th December, please attend." ) // when eventManager.sendReminder(event) // then verifyNoInteractions(mailer) }

Slide 46

Slide 46 text

@Test fun `don't send emails when there are no registrants`() { // given val dao = mock() val mailer = mock() val eventManager = EventManager(dao, mailer) whenever(dao.getRegistrants(event.id)).thenReturn(emptyList()) val event = Event( "Building Robust Software III", "Event is on 16th December, please attend." ) // when eventManager.sendReminder(event) // then verifyNoInteractions(mailer) }

Slide 47

Slide 47 text

@Test fun `don't send emails when there are no registrants`() { // given val dao = mock() val mailer = mock() val eventManager = EventManager(dao, mailer) whenever(dao.getRegistrants(event.id)).thenReturn(emptyList()) val event = Event( "Building Robust Software III", "Event is on 16th December, please attend." ) // when eventManager.sendReminder(event) // then verifyNoInteractions(mailer) }

Slide 48

Slide 48 text

@Test fun `don't send emails when there are no registrants`() { // given val dao = mock() val mailer = mock() val eventManager = EventManager(dao, mailer) whenever(dao.getRegistrants(event.id)).thenReturn(emptyList()) val event = Event( "Building Robust Software III", "Event is on 16th December, please attend." ) // when eventManager.sendReminder(event) // then verifyNoInteractions(mailer) }

Slide 49

Slide 49 text

@Test fun `don't send emails when there are no registrants`() { // given val dao = mock() val mailer = mock() val eventManager = EventManager(dao, mailer) whenever(dao.getRegistrants(event.id)).thenReturn(emptyList()) val event = Event( "Building Robust Software III", "Event is on 16th December, please attend." ) // when eventManager.sendReminder(event) // then verifyNoInteractions(mailer) }

Slide 50

Slide 50 text

@Test fun `don't send emails when there are no registrants`() { // given val dao = mock() val mailer = mock() val eventManager = EventManager(dao, mailer) whenever(dao.getRegistrants(event.id)).thenReturn(emptyList()) val event = Event( "Building Robust Software III", "Event is on 16th December, please attend." ) // when eventManager.sendReminder(event) // then verifyNoInteractions(mailer) }

Slide 51

Slide 51 text

@Test fun `don't send emails when there are no registrants`() { // given val dao = mock() val mailer = mock() val eventManager = EventManager(dao, mailer) whenever(dao.getRegistrants(event.id)).thenReturn(emptyList()) val event = Event( "Building Robust Software III", "Event is on 16th December, please attend." ) // when eventManager.sendReminder(event) // then verifyNoInteractions(mailer) }

Slide 52

Slide 52 text

@Test fun `don't send emails when there are no registrants`() { // given val dao = mock() val mailer = mock() val eventManager = EventManager(dao, mailer) whenever(dao.getRegistrants(event.id)).thenReturn(emptyList()) val event = Event( "Building Robust Software III", "Event is on 16th December, please attend." ) // when eventManager.sendReminder(event) // then verifyNoInteractions(mailer) }

Slide 53

Slide 53 text

@Test fun `don't send emails when there are no registrants`() { // given val dao = mock() val mailer = mock() val eventManager = EventManager(dao, mailer) whenever(dao.getRegistrants(event.id)).thenReturn(emptyList()) val event = Event( "Building Robust Software III", "Event is on 16th December, please attend." ) // when eventManager.sendReminder(event) // then verifyNoInteractions(mailer) }

Slide 54

Slide 54 text

Test: A mix of “Yes”, “No” and “Maybe”

Slide 55

Slide 55 text

@Test fun `email only those who RSVP'd yes and maybe`() { // given val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) whenever(dao.getRegistrants(event.id)).thenReturn(allRegistrants) // when eventManager.sendReminder(event) // then verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer) }

Slide 56

Slide 56 text

@Test fun `email only those who RSVP'd yes and maybe`() { // given val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) whenever(dao.getRegistrants(event.id)).thenReturn(allRegistrants) // when eventManager.sendReminder(event) // then verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer) }

Slide 57

Slide 57 text

@Test fun `email only those who RSVP'd yes and maybe`() { // given val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) whenever(dao.getRegistrants(event.id)).thenReturn(allRegistrants) // when eventManager.sendReminder(event) // then verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer) }

Slide 58

Slide 58 text

@Test fun `email only those who RSVP'd yes and maybe`() { // given val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) whenever(dao.getRegistrants(event.id)).thenReturn(allRegistrants) // when eventManager.sendReminder(event) // then verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer) }

Slide 59

Slide 59 text

@Test fun `email only those who RSVP'd yes and maybe`() { // given val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) whenever(dao.getRegistrants(event.id)).thenReturn(allRegistrants) // when eventManager.sendReminder(event) // then verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer) }

Slide 60

Slide 60 text

@Test fun `email only those who RSVP'd yes and maybe`() { // given val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) whenever(dao.getRegistrants(event.id)).thenReturn(allRegistrants) // when eventManager.sendReminder(event) // then verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer) }

Slide 61

Slide 61 text

@Test fun `email only those who RSVP'd yes and maybe`() { // given val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) whenever(dao.getRegistrants(event.id)).thenReturn(allRegistrants) // when eventManager.sendReminder(event) // then verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer) }

Slide 62

Slide 62 text

@Test fun `email only those who RSVP'd yes and maybe`() { // given val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) whenever(dao.getRegistrants(event.id)).thenReturn(allRegistrants) // when eventManager.sendReminder(event) // then verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer) }

Slide 63

Slide 63 text

@Test fun `email only those who RSVP'd yes and maybe`() { // given val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) whenever(dao.getRegistrants(event.id)).thenReturn(allRegistrants) // when eventManager.sendReminder(event) // then verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer) }

Slide 64

Slide 64 text

@Test fun `email only those who RSVP'd yes and maybe`() { // given val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) whenever(dao.getRegistrants(event.id)).thenReturn(allRegistrants) // when eventManager.sendReminder(event) // then verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer) }

Slide 65

Slide 65 text

✅ No registrations Everyone RSVP’d “Yes” Everyone RSVP’d “No” Everyone RSVP’d “Maybe” ✅ A mix of “Yes”, “No” and “Maybe”

Slide 66

Slide 66 text

Behaviour testing

Slide 67

Slide 67 text

Event Manager Event Dao Mailer

Slide 68

Slide 68 text

Event Manager Event Dao Mailer System under test (SUT)

Slide 69

Slide 69 text

Event Manager Event Dao Mailer Collaborators

Slide 70

Slide 70 text

Collaborators • Real thing • Sorta real thing

Slide 71

Slide 71 text

Sorta real thing • Stubs • Mocks • Fakes • Dummies • Spies https://martinfowler.com/articles/mocksArentStubs.html

Slide 72

Slide 72 text

Current EventManager design • Encapsulates business logic • Who should be noti fi ed?

Slide 73

Slide 73 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 74

Slide 74 text

Current EventManager design • Encapsulates business logic • Who should be noti fi ed? • Coordination logic • Who is talking to who?

Slide 75

Slide 75 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES || it.rsvp = = MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } a.EventManager.kt

Slide 76

Slide 76 text

Current EventManager design • Encapsulates business logic • Who should be noti fi ed? • Coordination logic • Who is talking to who?

Slide 77

Slide 77 text

Testing the business logic

Slide 78

Slide 78 text

Testing the business logic verifyNoInteractions(mailer) verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer)

Slide 79

Slide 79 text

Testing the business logic verifyNoInteractions(mailer) verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer)

Slide 80

Slide 80 text

Testing the business logic verifyNoInteractions(mailer) verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer)

Slide 81

Slide 81 text

Testing the business logic verifyNoInteractions(mailer) verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer)

Slide 82

Slide 82 text

Testing the business logic verifyNoInteractions(mailer) verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer)

Slide 83

Slide 83 text

Testing the coordination logic

Slide 84

Slide 84 text

Testing the coordination logic verifyNoInteractions(mailer) verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer)

Slide 85

Slide 85 text

Do-Over

Slide 86

Slide 86 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .potentialAttendees() potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES | | it.rsvp == MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } }

Slide 87

Slide 87 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .potentialAttendees() potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES | | it.rsvp == MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } }

Slide 88

Slide 88 text

class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .potentialAttendees() potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } } class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .filter { it.rsvp == YES | | it.rsvp == MAYBE } potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } }

Slide 89

Slide 89 text

b.EventManager.kt class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .potentialAttendees() potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } }

Slide 90

Slide 90 text

b.EventManager.kt class EventManager( private val dao: EventDao, private val mailer: Mailer ) { fun sendReminder(event: Event) { val registrants = dao.getRegistrants(event.id) val potentialAttendees = registrants .potentialAttendees() potentialAttendees.onEach { attendee -> mailer.mail(attendee.emailAddress, event.reminder) } } }

Slide 91

Slide 91 text

Users.kt fun List.potentialAttendees(): List = this.filter { user -> user.rsvp = = Rsvp.YES | | user.rsvp == Rsvp.MAYBE }

Slide 92

Slide 92 text

Users.kt fun List.potentialAttendees(): List = this.filter { user -> user.rsvp = = Rsvp.YES | | user.rsvp == Rsvp.MAYBE }

Slide 93

Slide 93 text

Users.kt fun List.potentialAttendees(): List = this.filter { user -> user.rsvp = = Rsvp.YES | | user.rsvp == Rsvp.MAYBE }

Slide 94

Slide 94 text

Users.kt fun List.potentialAttendees(): List = this.filter { user -> user.rsvp = = Rsvp.YES | | user.rsvp == Rsvp.MAYBE }

Slide 95

Slide 95 text

Users.kt fun List.potentialAttendees(): List = this.filter { user -> user.rsvp = = Rsvp.YES | | user.rsvp == Rsvp.MAYBE }

Slide 96

Slide 96 text

Users.kt fun List.potentialAttendees(): List = this.filter { user -> user.rsvp = = Rsvp.YES | | user.rsvp == Rsvp.MAYBE }

Slide 97

Slide 97 text

Users.kt fun List.potentialAttendees(): List = this.filter { user -> user.rsvp = = Rsvp.YES | | user.rsvp == Rsvp.MAYBE }

Slide 98

Slide 98 text

No registrations Everyone RSVP’d “Yes” Everyone RSVP’d “No” Everyone RSVP’d “Maybe” A mix of “Yes”, “No”, and “Maybe”

Slide 99

Slide 99 text

Test: No registrations

Slide 100

Slide 100 text

@Test fun `no potential attendees for an empty list`() { // given val noRegistrants = emptyList() // when val potentialAttendees = noRegistrants.potentialAttendees() // then assertThat(potentialAttendees) .isEmpty() }

Slide 101

Slide 101 text

@Test fun `no potential attendees for an empty list`() { // given val noRegistrants = emptyList() // when val potentialAttendees = noRegistrants.potentialAttendees() // then assertThat(potentialAttendees) .isEmpty() }

Slide 102

Slide 102 text

@Test fun `no potential attendees for an empty list`() { // given val noRegistrants = emptyList() // when val potentialAttendees = noRegistrants.potentialAttendees() // then assertThat(potentialAttendees) .isEmpty() }

Slide 103

Slide 103 text

@Test fun `no potential attendees for an empty list`() { // given val noRegistrants = emptyList() // when val potentialAttendees = noRegistrants.potentialAttendees() // then assertThat(potentialAttendees) .isEmpty() }

Slide 104

Slide 104 text

@Test fun `no potential attendees for an empty list`() { // given val noRegistrants = emptyList() // when val potentialAttendees = noRegistrants.potentialAttendees() // then assertThat(potentialAttendees) .isEmpty() }

Slide 105

Slide 105 text

@Test fun `no potential attendees for an empty list`() { // given val noRegistrants = emptyList() // when val potentialAttendees = noRegistrants.potentialAttendees() // then assertThat(potentialAttendees) .isEmpty() }

Slide 106

Slide 106 text

@Test fun `no potential attendees for an empty list`() { // given val noRegistrants = emptyList() // when val potentialAttendees = noRegistrants.potentialAttendees() // then assertThat(potentialAttendees) .isEmpty() }

Slide 107

Slide 107 text

Test: A mix of “Yes”, “No” and “Maybe”

Slide 108

Slide 108 text

@Test fun `potential attendees contains only registrants those who RSVP'd yes or maybe`() { // given val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) // when val potentialAttendees = allRegistrants.potentialAttendees() // then assertThat(potentialAttendees) .containsExactly( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.MAYBE) ) }

Slide 109

Slide 109 text

@Test fun `potential attendees contains only registrants those who RSVP'd yes or maybe`() { // given val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) // when val potentialAttendees = allRegistrants.potentialAttendees() // then assertThat(potentialAttendees) .containsExactly( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.MAYBE) ) }

Slide 110

Slide 110 text

@Test fun `potential attendees contains only registrants those who RSVP'd yes or maybe`() { // given val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) // when val potentialAttendees = allRegistrants.potentialAttendees() // then assertThat(potentialAttendees) .containsExactly( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.MAYBE) ) }

Slide 111

Slide 111 text

@Test fun `potential attendees contains only registrants those who RSVP'd yes or maybe`() { // given val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) // when val potentialAttendees = allRegistrants.potentialAttendees() // then assertThat(potentialAttendees) .containsExactly( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.MAYBE) ) }

Slide 112

Slide 112 text

@Test fun `potential attendees contains only registrants those who RSVP'd yes or maybe`() { // given val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) // when val potentialAttendees = allRegistrants.potentialAttendees() // then assertThat(potentialAttendees) .containsExactly( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.MAYBE) ) }

Slide 113

Slide 113 text

@Test fun `potential attendees contains only registrants those who RSVP'd yes or maybe`() { // given val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) // when val potentialAttendees = allRegistrants.potentialAttendees() // then assertThat(potentialAttendees) .containsExactly( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.MAYBE) ) }

Slide 114

Slide 114 text

@Test fun `potential attendees contains only registrants those who RSVP'd yes or maybe`() { // given val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) // when val potentialAttendees = allRegistrants.potentialAttendees() // then assertThat(potentialAttendees) .containsExactly( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.MAYBE) ) }

Slide 115

Slide 115 text

✅ No registrations Everyone RSVP’d “Yes” Everyone RSVP’d “No” Everyone RSVP’d “Maybe” ✅ A mix of “Yes”, “No” and “Maybe”

Slide 116

Slide 116 text

🤨

Slide 117

Slide 117 text

Value testing

Slide 118

Slide 118 text

I O

Slide 119

Slide 119 text

I O Input

Slide 120

Slide 120 text

I O Output

Slide 121

Slide 121 text

I O System under test (SUT)

Slide 122

Slide 122 text

Collaborators • None • We use real things

Slide 123

Slide 123 text

Newer EventManager design • Does not encapsulate business logic • Can use micro unit tests to test various cases • Value-in, value-out results in decoupled design • No dependencies for business logic

Slide 124

Slide 124 text

Testing the business logic assertThat(potentialAttendees) .isEmpty() assertThat(potentialAttendees) .containsExactly( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.MAYBE) )

Slide 125

Slide 125 text

Testing the business logic assertThat(potentialAttendees) .isEmpty() assertThat(potentialAttendees) .containsExactly( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.MAYBE) )

Slide 126

Slide 126 text

Testing the business logic assertThat(potentialAttendees) .isEmpty() assertThat(potentialAttendees) .containsExactly( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.MAYBE) )

Slide 127

Slide 127 text

Testing the business logic assertThat(potentialAttendees) .isEmpty() assertThat(potentialAttendees) .containsExactly( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.MAYBE) )

Slide 128

Slide 128 text

Testing the business logic assertThat(potentialAttendees) .isEmpty() assertThat(potentialAttendees) .containsExactly( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.MAYBE) )

Slide 129

Slide 129 text

Testing the coordination logic @Test fun `query and email folks who RSVP'd yes and maybe to the event`() { // given val dao = mock() val mailer = mock() val eventManager = EventManager(dao, mailer) val event = Event( 1, "Building Robust Software III", "Event is on 16th December, please attend." ) val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) whenever(dao.getRegistrants(event.id)).thenReturn(allRegistrants) // when eventManager.sendReminder(event) // then verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer) }

Slide 130

Slide 130 text

Testing the coordination logic @Test fun `query and email folks who RSVP'd yes and maybe to the event`() { // given val dao = mock() val mailer = mock() val eventManager = EventManager(dao, mailer) val event = Event( 1, "Building Robust Software III", "Event is on 16th December, please attend." ) val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) whenever(dao.getRegistrants(event.id)).thenReturn(allRegistrants) // when eventManager.sendReminder(event) // then verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer) }

Slide 131

Slide 131 text

Testing the coordination logic @Test fun `query and email folks who RSVP'd yes and maybe to the event`() { // given val dao = mock() val mailer = mock() val eventManager = EventManager(dao, mailer) val event = Event( 1, "Building Robust Software III", "Event is on 16th December, please attend." ) val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) whenever(dao.getRegistrants(event.id)).thenReturn(allRegistrants) // when eventManager.sendReminder(event) // then verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer) }

Slide 132

Slide 132 text

Testing the coordination logic @Test fun `query and email folks who RSVP'd yes and maybe to the event`() { // given val dao = mock() val mailer = mock() val eventManager = EventManager(dao, mailer) val event = Event( 1, "Building Robust Software III", "Event is on 16th December, please attend." ) val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) whenever(dao.getRegistrants(event.id)).thenReturn(allRegistrants) // when eventManager.sendReminder(event) // then verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer) }

Slide 133

Slide 133 text

Testing the coordination logic @Test fun `query and email folks who RSVP'd yes and maybe to the event`() { // given val dao = mock() val mailer = mock() val eventManager = EventManager(dao, mailer) val event = Event( 1, "Building Robust Software III", "Event is on 16th December, please attend." ) val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) whenever(dao.getRegistrants(event.id)).thenReturn(allRegistrants) // when eventManager.sendReminder(event) // then verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer) }

Slide 134

Slide 134 text

Testing the coordination logic @Test fun `query and email folks who RSVP'd yes and maybe to the event`() { // given val dao = mock() val mailer = mock() val eventManager = EventManager(dao, mailer) val event = Event( 1, "Building Robust Software III", "Event is on 16th December, please attend." ) val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) whenever(dao.getRegistrants(event.id)).thenReturn(allRegistrants) // when eventManager.sendReminder(event) // then verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer) }

Slide 135

Slide 135 text

Implementation A vs. B @Test fun `RSVP'd yes or maybe`() { // given val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) // when val potentialAttendees = allRegistrants.potentialAttendees() // then assertThat(potentialAttendees) .containsExactly( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.MAYBE) ) } @Test fun `RSVP'd yes and maybe to the event`() { // given val dao = mock() val mailer = mock() val eventManager = EventManager(dao, mailer) val event = Event( 1, "Building Robust Software III", "Event is on 16th December, please attend." ) val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) whenever(dao.getRegistrants(event.id)).thenReturn(allRegistrants) // when eventManager.sendReminder(event) // then verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer) }

Slide 136

Slide 136 text

Implementation A vs. B @Test fun `RSVP'd yes or maybe`() { // given val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) // when val potentialAttendees = allRegistrants.potentialAttendees() // then assertThat(potentialAttendees) .containsExactly( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.MAYBE) ) } @Test fun `RSVP'd yes and maybe to the event`() { // given val dao = mock() val mailer = mock() val eventManager = EventManager(dao, mailer) val event = Event( 1, "Building Robust Software III", "Event is on 16th December, please attend." ) val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) whenever(dao.getRegistrants(event.id)).thenReturn(allRegistrants) // when eventManager.sendReminder(event) // then verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer) }

Slide 137

Slide 137 text

Implementation A vs. B @Test fun `RSVP'd yes or maybe`() { // given val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) // when val potentialAttendees = allRegistrants.potentialAttendees() // then assertThat(potentialAttendees) .containsExactly( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.MAYBE) ) } @Test fun `RSVP'd yes and maybe to the event`() { // given val dao = mock() val mailer = mock() val eventManager = EventManager(dao, mailer) val event = Event( 1, "Building Robust Software III", "Event is on 16th December, please attend." ) val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) whenever(dao.getRegistrants(event.id)).thenReturn(allRegistrants) // when eventManager.sendReminder(event) // then verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer) }

Slide 138

Slide 138 text

Implementation A vs. B @Test fun `RSVP'd yes or maybe`() { // given val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) // when val potentialAttendees = allRegistrants.potentialAttendees() // then assertThat(potentialAttendees) .containsExactly( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.MAYBE) ) } @Test fun `RSVP'd yes and maybe to the event`() { // given val dao = mock() val mailer = mock() val eventManager = EventManager(dao, mailer) val event = Event( 1, "Building Robust Software III", "Event is on 16th December, please attend." ) val allRegistrants = listOf( User("[email protected]", Rsvp.YES), User("[email protected]", Rsvp.NO), User("[email protected]", Rsvp.MAYBE) ) whenever(dao.getRegistrants(event.id)).thenReturn(allRegistrants) // when eventManager.sendReminder(event) // then verify(mailer).mail("[email protected]", event.reminder) verify(mailer).mail("[email protected]", event.reminder) verifyNoMoreInteractions(mailer) }

Slide 139

Slide 139 text

Implementation A vs. B • 5 integration tests • + Dependencies • ↑ Coupling • Wider change propagation • Harder to maintain • More assertions • 5 micro-unit tests • 1 integration test • - Dependencies • ↓ Coupling • Limited change propagation • Easier to maintain • Fewer assertions 💯

Slide 140

Slide 140 text

Implementation A vs. B • 5 integration tests • + Dependencies • ↑ Coupling • Wider change propagation • Harder to maintain • More assertions • 5 micro-unit tests • 1 integration test • - Dependencies • ↓ Coupling • Limited change propagation • Easier to maintain • Fewer assertions 💯

Slide 141

Slide 141 text

Implementation A vs. B • 5 integration tests • + Dependencies • ↑ Coupling • Wider change propagation • Harder to maintain • More assertions • 5 micro-unit tests • 1 integration test • - Dependencies • ↓ Coupling • Limited change propagation • Easier to maintain • Fewer assertions 💯

Slide 142

Slide 142 text

Implementation A vs. B • 5 integration tests • + Dependencies • ↑ Coupling • Wider change propagation • Harder to maintain • More assertions • 5 micro-unit tests • 1 integration test • - Dependencies • ↓ Coupling • Limited change propagation • Easier to maintain • Fewer assertions 💯

Slide 143

Slide 143 text

Implementation A vs. B • 5 integration tests • + Dependencies • ↑ Coupling • Wider change propagation • Harder to maintain • More assertions • 5 micro-unit tests • 1 integration test • - Dependencies • ↓ Coupling • Limited change propagation • Easier to maintain • Fewer assertions 💯

Slide 144

Slide 144 text

Implementation A vs. B • 5 integration tests • + Dependencies • ↑ Coupling • Wider change propagation • Harder to maintain • More assertions • 5 micro-unit tests • 1 integration test • - Dependencies • ↓ Coupling • Limited change propagation • Easier to maintain • Fewer assertions 💯

Slide 145

Slide 145 text

Implementation A vs. B import com.google.common.truth.Truth.assertThat import org.junit.jupiter.api.Test import xyz.ragunath.events.Rsvp import xyz.ragunath.events.User import org.junit.jupiter.api.Test import org.mockito.kotlin.mock import org.mockito.kotlin.verify import org.mockito.kotlin.verifyNoInteractions import org.mockito.kotlin.verifyNoMoreInteractions import org.mockito.kotlin.whenever import xyz.ragunath.events.Event import xyz.ragunath.events.EventDao import xyz.ragunath.events.Mailer import xyz.ragunath.events.Rsvp import xyz.ragunath.events.User

Slide 146

Slide 146 text

Implementation A vs. B import com.google.common.truth.Truth.assertThat import org.junit.jupiter.api.Test import xyz.ragunath.events.Rsvp import xyz.ragunath.events.User import org.junit.jupiter.api.Test import org.mockito.kotlin.mock import org.mockito.kotlin.verify import org.mockito.kotlin.verifyNoInteractions import org.mockito.kotlin.verifyNoMoreInteractions import org.mockito.kotlin.whenever import xyz.ragunath.events.Event import xyz.ragunath.events.EventDao import xyz.ragunath.events.Mailer import xyz.ragunath.events.Rsvp import xyz.ragunath.events.User

Slide 147

Slide 147 text

Implementation A vs. B import com.google.common.truth.Truth.assertThat import org.junit.jupiter.api.Test import xyz.ragunath.events.Rsvp import xyz.ragunath.events.User import org.junit.jupiter.api.Test import org.mockito.kotlin.mock import org.mockito.kotlin.verify import org.mockito.kotlin.verifyNoInteractions import org.mockito.kotlin.verifyNoMoreInteractions import org.mockito.kotlin.whenever import xyz.ragunath.events.Event import xyz.ragunath.events.EventDao import xyz.ragunath.events.Mailer import xyz.ragunath.events.Rsvp import xyz.ragunath.events.User

Slide 148

Slide 148 text

Implementation A vs. B import com.google.common.truth.Truth.assertThat import org.junit.jupiter.api.Test import xyz.ragunath.events.Rsvp import xyz.ragunath.events.User import org.junit.jupiter.api.Test import org.mockito.kotlin.mock import org.mockito.kotlin.verify import org.mockito.kotlin.verifyNoInteractions import org.mockito.kotlin.verifyNoMoreInteractions import org.mockito.kotlin.whenever import xyz.ragunath.events.Event import xyz.ragunath.events.EventDao import xyz.ragunath.events.Mailer import xyz.ragunath.events.Rsvp import xyz.ragunath.events.User

Slide 149

Slide 149 text

Implementation A vs. B • 5 integration tests • + Dependencies • ↑ Coupling • Wider change propagation • Harder to maintain • More assertions • 5 micro-unit tests • 1 integration test • - Dependencies • ↓ Coupling • Limited change propagation • Easier to maintain • Fewer assertions 💯

Slide 150

Slide 150 text

Implementation A vs. B • 5 integration tests • + Dependencies • ↑ Coupling • Wider change propagation • Harder to maintain • More assertions • 5 micro-unit tests • 1 integration test • - Dependencies • ↓ Coupling • Limited change propagation • Easier to maintain • Fewer assertions 💯

Slide 151

Slide 151 text

Implementation A vs. B • 5 integration tests • + Dependencies • ↑ Coupling • Wider change propagation • Harder to maintain • More assertions • 5 micro-unit tests • 1 integration test • - Dependencies • ↓ Coupling • Limited change propagation • Easier to maintain • Fewer assertions 💯

Slide 152

Slide 152 text

Implementation A vs. B import com.google.common.truth.Truth.assertThat import org.junit.jupiter.api.Test import xyz.ragunath.events.Rsvp import xyz.ragunath.events.User import org.junit.jupiter.api.Test import org.mockito.kotlin.mock import org.mockito.kotlin.verify import org.mockito.kotlin.verifyNoInteractions import org.mockito.kotlin.verifyNoMoreInteractions import org.mockito.kotlin.whenever import xyz.ragunath.events.Event import xyz.ragunath.events.EventDao import xyz.ragunath.events.Mailer import xyz.ragunath.events.Rsvp import xyz.ragunath.events.User

Slide 153

Slide 153 text

Implementation A vs. B • 5 integration tests • + Dependencies • ↑ Coupling • Wider change propagation • Harder to maintain • More assertions • 5 micro-unit tests • 1 integration test • - Dependencies • ↓ Coupling • Limited change propagation • Easier to maintain • Fewer assertions 💯

Slide 154

Slide 154 text

Implementation A vs. B • 5 integration tests • + Dependencies • ↑ Coupling • Wider change propagation • Harder to maintain • More assertions • 5 micro-unit tests • 1 integration test • - Dependencies • ↓ Coupling • Limited change propagation • Easier to maintain • Fewer assertions 💯

Slide 155

Slide 155 text

Implementation A vs. B • 5 integration tests • + Dependencies • ↑ Coupling • Wider change propagation • Harder to maintain • More assertions • 5 micro-unit tests • 1 integration test • - Dependencies • ↓ Coupling • Limited change propagation • Easier to maintain • Fewer assertions 💯

Slide 156

Slide 156 text

Implementation A vs. B • 5 integration tests • + Dependencies • ↑ Coupling • Wider change propagation • Harder to maintain • More assertions • 5 micro-unit tests • 1 integration test • - Dependencies • ↓ Coupling • Limited change propagation • Easier to maintain • Fewer assertions 💯

Slide 157

Slide 157 text

Implementation A vs. B • 5 integration tests • + Dependencies • ↑ Coupling • Wider change propagation • Harder to maintain • More assertions • 5 micro-unit tests • 1 integration test • - Dependencies • ↓ Coupling • Limited change propagation • Easier to maintain • Fewer assertions 💯

Slide 158

Slide 158 text

Implementation A vs. B • 5 integration tests • + Dependencies • ↑ Coupling • Wider change propagation • Harder to maintain • More assertions • 5 micro-unit tests • 1 integration test • - Dependencies • ↓ Coupling • Limited change propagation • Easier to maintain • Fewer assertions 💯

Slide 159

Slide 159 text

Implementation A vs. B • 5 integration tests • + Dependencies • ↑ Coupling • Wider change propagation • Harder to maintain • More assertions • 5 micro-unit tests • 1 integration test • - Dependencies • ↓ Coupling • Limited change propagation • Easier to maintain • Fewer assertions 💯

Slide 160

Slide 160 text

Implementation A vs. B • 5 integration tests • + Dependencies • ↑ Coupling • Wider change propagation • Harder to maintain • More assertions • 5 micro-unit tests • 1 integration test • - Dependencies • ↓ Coupling • Limited change propagation • Easier to maintain • Fewer assertions 💯

Slide 161

Slide 161 text

Implementation A vs. B • 5 integration tests • + Dependencies • ↑ Coupling • Wider change propagation • Harder to maintain • More assertions • 5 micro-unit tests • 1 integration test • - Dependencies • ↓ Coupling • Limited change propagation • Easier to maintain • Fewer assertions 💯

Slide 162

Slide 162 text

Destroy All Software—Boundaries https://bit.ly/3yuuxvQ

Slide 163

Slide 163 text

What did we learn? • Behaviour testing • Value testing • Micro-unit tests • Complementary tests • Integration tests

Slide 164

Slide 164 text

Scenario You inherit a codebase that does not have any tests, the previous developers have left the organisation and you are expected to enhance an existing feature.

Slide 165

Slide 165 text

package com.gildedrose class GildedRose(var items: Array) { fun updateQuality() { for (i in items.indices) { if (items[i].name != "Aged Brie" && items[i].name ! = "Backstage passes to a TAFKAL80ETC concert") { if (items[i].quality > 0) { if (items[i].name != "Sulfuras, Hand of Ragnaros") { items[i].quality = items[i].quality - 1 } } } else { if (items[i].quality < 50) { items[i].quality = items[i].quality + 1 if (items[i].name == "Backstage passes to a TAFKAL80ETC concert") { if (items[i].sellIn < 11) { if (items[i].quality < 50) { items[i].quality = items[i].quality + 1 } } if (items[i].sellIn < 6) { if (items[i].quality < 50) { items[i].quality = items[i].quality + 1 } } } } } if (items[i].name != "Sulfuras, Hand of Ragnaros") { items[i].sellIn = items[i].sellIn - 1 } if (items[i].sellIn < 0) { if (items[i].name != "Aged Brie") { if (items[i].name != "Backstage passes to a TAFKAL80ETC concert") { if (items[i].quality > 0) { if (items[i].name != "Sulfuras, Hand of Ragnaros") { items[i].quality = items[i].quality - 1 } } } else { items[i].quality = items[i].quality - items[i].quality } } else { if (items[i].quality < 50) { items[i].quality = items[i].quality + 1 } } } } } }

Slide 166

Slide 166 text

Writing tests for code you wrote vs. Writing tests for someone else’s code (characterisation tests)

Slide 167

Slide 167 text

@Test fun `aged brie`() { // given val itemName = "Aged Brie" val sellIns = arrayOf(-1, 0, 1, 5, 6, 7, 10, 11, 12) val qualities = arrayOf(-1, 0, 1, 49, 50, 51) // when val function = updateQualityFunction(itemName) // (Int, Int) - > String // then CombinationApprovals.verifyAllCombinations(function, sellIns, qualities) }

Slide 168

Slide 168 text

@Test fun `aged brie`() { // given val itemName = "Aged Brie" val sellIns = arrayOf(-1, 0, 1, 5, 6, 7, 10, 11, 12) val qualities = arrayOf(-1, 0, 1, 49, 50, 51) // when val function = updateQualityFunction(itemName) // (Int, Int) - > String // then CombinationApprovals.verifyAllCombinations(function, sellIns, qualities) }

Slide 169

Slide 169 text

@Test fun `aged brie`() { // given val itemName = "Aged Brie" val sellIns = arrayOf(-1, 0, 1, 5, 6, 7, 10, 11, 12) val qualities = arrayOf(-1, 0, 1, 49, 50, 51) // when val function = updateQualityFunction(itemName) // (Int, Int) - > String // then CombinationApprovals.verifyAllCombinations(function, sellIns, qualities) }

Slide 170

Slide 170 text

[-1, -1] => [Aged Brie, -2, 1] [-1, 0] => [Aged Brie, -2, 2] [-1, 1] => [Aged Brie, -2, 3] [-1, 49] => [Aged Brie, -2, 50] [-1, 50] => [Aged Brie, -2, 50] [-1, 51] => [Aged Brie, -2, 51] [0, -1] => [Aged Brie, -1, 1] [0, 0] => [Aged Brie, -1, 2] [0, 1] => [Aged Brie, -1, 3] [0, 49] => [Aged Brie, -1, 50] [0, 50] => [Aged Brie, -1, 50] [0, 51] => [Aged Brie, -1, 51] [1, -1] => [Aged Brie, 0, 0] [1, 0] => [Aged Brie, 0, 1] [1, 1] => [Aged Brie, 0, 2] [1, 49] => [Aged Brie, 0, 50] [1, 50] => [Aged Brie, 0, 50] [1, 51] => [Aged Brie, 0, 51] [5, -1] => [Aged Brie, 4, 0] [5, 0] => [Aged Brie, 4, 1] [5, 1] => [Aged Brie, 4, 2] [5, 49] => [Aged Brie, 4, 50] [5, 50] => [Aged Brie, 4, 50] [5, 51] => [Aged Brie, 4, 51] [6, -1] => [Aged Brie, 5, 0] [6, 0] => [Aged Brie, 5, 1] [6, 1] => [Aged Brie, 5, 2] [6, 49] => [Aged Brie, 5, 50] [6, 50] => [Aged Brie, 5, 50] [6, 51] => [Aged Brie, 5, 51] [7, -1] => [Aged Brie, 6, 0] [7, 0] => [Aged Brie, 6, 1] [7, 1] => [Aged Brie, 6, 2] [7, 49] => [Aged Brie, 6, 50] [7, 50] => [Aged Brie, 6, 50] [7, 51] => [Aged Brie, 6, 51] [10, -1] => [Aged Brie, 9, 0] [10, 0] => [Aged Brie, 9, 1] [10, 1] => [Aged Brie, 9, 2] [10, 49] => [Aged Brie, 9, 50] [10, 50] => [Aged Brie, 9, 50] [10, 51] => [Aged Brie, 9, 51] [11, -1] => [Aged Brie, 10, 0] [11, 0] => [Aged Brie, 10, 1] [11, 1] => [Aged Brie, 10, 2] [11, 49] => [Aged Brie, 10, 50] [11, 50] => [Aged Brie, 10, 50] [11, 51] => [Aged Brie, 10, 51] [12, -1] => [Aged Brie, 11, 0] [12, 0] => [Aged Brie, 11, 1] [12, 1] => [Aged Brie, 11, 2] [12, 49] => [Aged Brie, 11, 50] [12, 50] => [Aged Brie, 11, 50] [12, 51] => [Aged Brie, 11, 51]

Slide 171

Slide 171 text

No content

Slide 172

Slide 172 text

Refactor ✌

Slide 173

Slide 173 text

No content

Slide 174

Slide 174 text

Approval Tests • Snapshot testing, golden master testing, etc., • Helps bring large portions of code under tests quickly • Can work on di ff erent kinds of data

Slide 175

Slide 175 text

Approval Tests • https://github.com/approvals/ApprovalTests.Java • https://github.com/approvals/ApprovalTests.Plugins.IntelliJ • https://github.com/approvals

Slide 176

Slide 176 text

More testing tools & techniques • Performance testing • Property testing

Slide 177

Slide 177 text

Workflows • TDD • https://www.coderetreat.org/ • TCR • https://medium.com/@kentbeck_7670/test-commit-revert-870bbd756864 • https://github.com/LarsEckart/tcr-extension • https://github.com/ragunathjawahar/GildedRose-Refactoring-Kata/tree/rj/ attempt-04

Slide 178

Slide 178 text

Fin Questions? @ragunathjawahar • https://ragunath.xyz