Do you want to master writing super clean, testable Java code? Join my "Test First Immersion" workshop at QCon San Francisco on 11/16/2017!
Sign up: https://qconsf.com/sf2017/workshop/test-first-immersion-workshop-java (reach out if you need a discount code!)
The GitHub project used during the workshop: https://github.com/mockito/testing-workshop
Workshop agenda, as described on QCon website:
Are you a Java engineer eager to improve your skill in writing great tests? In this workshop you will:
- get excited to try out and practice "test first" approach to designing and implementing features.
- adopt practical Java tactics to create beautiful, highly maintainable unit tests.
- drill refactoring needed to keep the code easy to test
learn IntelliJ IDEA tricks that streamline test first development
Method of Instruction: hands-on programming workshop, started with a short lecture and completed with team code review.
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
QCon'17 workshop: Test First Immersion
1. @mockitoguy
Test First Immersion Workshop
Writing Great Unit Tests
QCon, San Francisco, 11-16-2017
● Szczepan Faber, @mockitoguy
● Born in Poland, in US since 2015
● Codes professionally since 2002
● Creator of mockito.org in 2007
● Core dev of gradle.org 1.x and 2.x in 2011-2015
● Tech Lead at LinkedIn Dev Tools since 2015
● Author on linkedin.com/in/mockitoguy
● Believer of Great Code Reviews:
https://thenewstack.io/linkedin-code-review
Tell me and I forget.
Teach and I remember.
Involve me and I will learn.
~ Benjamin Franklin
Live as if you were to die
tomorrow. Learn as if you
were to live forever.
~ Mahatma Gandhi
Want to innovate in Open Source?
Join Shipkit - new project
used by Mockito: http://shipkit.org
3. @mockitoguy
How to best use
this workshop?
Ask questions, interrupt me
Work together, you will learn more!
Take active part. Challenge me!
4. @mockitoguy
We will practice together
Techniques for writing great tests so that...
… you can write code that is clean, maintainable and bug-free
● No way to learn it in a day
● Clean tests force clean code
● Develop sensitivity to great tests and code
● Want to truly master writing testable code? Use TDD every day for a year
9. @mockitoguy
To learn, we need
rules initially
Can you give examples of
programming rules?
1. KISS - keep it simple, stupid
2. DRY - don’t repeat yourself
3. SOLID - Single responsibility, Open-closed, Liskov,
Interface segregation, Dependency inversion)
4. One test case per method
10. @mockitoguy
Your thoughts about TDD?
● great practice but it is not about tests. It is about…
● behaviors, best described using examples
11. @mockitoguy
The rules of the workshop
● before implementing anything - write a test that demonstrates the behavior
● write only one test and run it before implementing code
Test
ImplementationRefactoring
● generate classes / methods from the test (alt+enter or ctrl+1)
● use //given //when //then comments template in each test
Start of iteration 1
13. @mockitoguy
Why test first?
● Forces us to think through the functionality before implementing it
- we think about the code before we write it
- how should the system work? how should the API look like?
● We automate from the start
- testable architecture, code optimized for testability
- accelerated learning how to write testable code
- test utilities help us building future tests and functionalities
● We leverage IDE big time
- code generated from tests
- very efficient
● By the way, it gets us the test coverage, too :)
14. @mockitoguy
How to develop test first habit?
● Practice (kata). Watch your ego and practice every day (SHU)
● Scale your expectations
- you will be slow at start, that’s ok
- you will have dilemmas how to test, code, when to refactor, that’s ok
- you will frequently refactor and rewrite the code, that’s ok
● Develop sensitivity and taste for great tests
- bad tests frustrate and discourage
- great tests guide you to write clean and well designed code
● Scale difficulty level
- don’t start by writing tests for the biggest, ugliest class in your code base
● Enjoy the journey to mastery (RI)
15. @mockitoguy
Your daily practice
● don’t write tests, write examples of behavior
● one test -> one “should”
● one test -> one behavior
● write //given //when //then comments
● alt+enter (or ctrl+1) tells you how proficient you are