Entries Tagged as 'Testability'

Singletons are Pathological Liars

August 17th, 2008 · 77 Comments

by Miško Hevery So you join a new project, which has an extensive mature code base. Your new lead asks you to implement a new feature, and, as a good developer, you start by writing a test. But since you are new to the project, you do a lot of exploratory “What happens if I [...]

Tags: Advice · OO · Rant · Testability

Procedural Language Eliminated GOTOs; OO Eliminated IFs

August 14th, 2008 · 7 Comments

by Miško Hevery Procedural languages allowed us to remove GOTOs in our code. I would like to think that OO languages allow us to remove IFs (conditionals). I know you can’t remove of all the IFs, but it is interesting just how many IFs you can remove in an application. You can’t remove these IFs: [...]

Tags: Advice · OO · Testability · Uncategorized

Circular Dependency in constructors and Dependency Injection

August 1st, 2008 · 27 Comments

by Miško Hevery So you discovered dependency injection and GUICE and you are happily refactoring and writing new tests for you code until you come across this circular reference. class A { final B b; A(B b){ this.b = b; } } class B { final A a; B(){ this.a = new A(this); } } [...]

Tags: Advice · OO · Testability

Top 10 things which make your code hard to test

July 30th, 2008 · 29 Comments

by Miško Hevery So you decided to finally give this testing thing a try. But somehow you just can’t figure out how to write a unit-test for your class. Well there are no tricks to writing tests, there are only tricks to writing testable code. If I gave you testable code you would have no [...]

Tags: Advice · Rant · Testability

How to Write 3v1L, Untestable Code

July 24th, 2008 · 15 Comments

by Miško Hevery, Jonathan Wolter, Russ Ruffer, Brad Cross, and lots of other test infected Googlers This guide lists principles that will help you write impossible to tests code. Or, avoiding these techniques will help you write code that can be tested. Make Your Own Dependencies – Instantiate objects using new in the middle of [...]

Tags: Advice · Rant · Testability

Breaking the Law of Demeter is Like Looking for a Needle in the Haystack

July 18th, 2008 · 20 Comments

by Miško Hevery Every time I see Law of Demeter violation I imagine a haystack where the code is desperately trying to locate the needle. class Mechanic { Engine engine; Mechanic(Context context) { this.engine = context.getEngine(); } } The Mechanic does not care for the Context. You can tell because Mechanic does not store the [...]

Tags: Testability

How to Think About the “new” Operator with Respect to Unit Testing

July 8th, 2008 · 44 Comments

By Miško Hevery Unit Testing as the name implies asks you to test a Class (Unit) in isolation. If your code mixes Object Construction with Logic you will never be able to achieve isolation. In order to unit-test you need to separate object graph construction from the application logic into two different classes The end [...]

Tags: Testability