retro testing (devcontlv jan 2014)

20
1 Allon Mureinik - Retro Testing Retro Testing Allon Mureinik Team Lead, Cloud Storage, Red Hat [email protected] / @mureinik January 2014

Upload: allon-mureinik

Post on 02-Jul-2015

125 views

Category:

Technology


0 download

DESCRIPTION

Unit testing is easy... In a perfect world. Our world is not. This talk will cover a bunch of tips, tricks, and techniques to retrofit ugly legacy applications so parts of them can be unit tested. (Examples given in Java using JUnit and Mockito)

TRANSCRIPT

Page 1: Retro Testing (DevConTLV Jan 2014)

1Allon Mureinik - Retro Testing

Retro Testing

Allon MureinikTeam Lead, Cloud Storage, Red [email protected] / @mureinik

January 2014

Page 2: Retro Testing (DevConTLV Jan 2014)

2Allon Mureinik - Retro Testing

TDD Is Easy...

Page 3: Retro Testing (DevConTLV Jan 2014)

3Allon Mureinik - Retro Testing

... or is it?

● The first two part should be● Write a test● Make sure it fails

● The question is why it fails.

● In a legacy system, it will often fail for “bad” reasons:● Unable to access the database● Some static resource not set up● Need to spin up an application server● Etc., etc., etc...

Page 4: Retro Testing (DevConTLV Jan 2014)

4Allon Mureinik - Retro Testing

Will the mistakes of the past haunt me forever?

Page 5: Retro Testing (DevConTLV Jan 2014)

5Allon Mureinik - Retro Testing

It’s not an all-or-nothing situation

Page 6: Retro Testing (DevConTLV Jan 2014)

6Allon Mureinik - Retro Testing

One baby step at a time

Page 7: Retro Testing (DevConTLV Jan 2014)

7Allon Mureinik - Retro Testing

Refactor, refactor, refactor

● Your first task would probably be to do some refactoring

● Resist the urge to improve the code● Frankly, this step may make the code look worse

● Your only goal here is to create an opportunity to write tests

Page 8: Retro Testing (DevConTLV Jan 2014)

8Allon Mureinik - Retro Testing

Refacroting with no tests is like...

Page 9: Retro Testing (DevConTLV Jan 2014)

9Allon Mureinik - Retro Testing

Example : Bad Code

Page 10: Retro Testing (DevConTLV Jan 2014)

10Allon Mureinik - Retro Testing

Some minimal refactoring

Page 11: Retro Testing (DevConTLV Jan 2014)

11Allon Mureinik - Retro Testing

Now we can start writing tests...

● Now we have the tools to separate external resources from logic

● There are a couple of ways to do so:● Override the relevant methods in your test● Use Mockito/EasyMock to spy the tested object● Use @Rules to set up common mocking once

Page 12: Retro Testing (DevConTLV Jan 2014)

12Allon Mureinik - Retro Testing

I @Spy With My Little Eye

Page 13: Retro Testing (DevConTLV Jan 2014)

13Allon Mureinik - Retro Testing

Some more refactoring

Page 14: Retro Testing (DevConTLV Jan 2014)

14Allon Mureinik - Retro Testing

Let’s modernize our code

Page 15: Retro Testing (DevConTLV Jan 2014)

15Allon Mureinik - Retro Testing

But how can I write asserts?

● Overriding, mocking and all that jazz are fine and well

● But any test boils down to writing an assert...

● ... and I have no idea what this function is supposed to do

● Remember this is a legacy system

● You may not need to test it for correctness ...

● Just for backwards compatibility

Page 16: Retro Testing (DevConTLV Jan 2014)

16Allon Mureinik - Retro Testing

Page 17: Retro Testing (DevConTLV Jan 2014)

17Allon Mureinik - Retro Testing

The real challenge is changing mindset

● We can discusses refactoring till we’re blue in the face

● But the real challenge isn’t changing the way we code

● It’s changing the way we approach the problem

Page 18: Retro Testing (DevConTLV Jan 2014)

18Allon Mureinik - Retro Testing

It’s all too easy to slip back to bad habits

● This bug is blocking the release...

● It’s a ton of work to refactor this logic out...

● The rest of the code is bad anyway...

Page 19: Retro Testing (DevConTLV Jan 2014)

19Allon Mureinik - Retro Testing

Have the courage to stand up to excuses

Page 20: Retro Testing (DevConTLV Jan 2014)

20Allon Mureinik - Retro Testing

Questions?