Tim Orme
Posts: 3
Nickname: backspace
Registered: Jan, 2004
|
|
Re: Are Tests First Class Clients?
|
Posted: Feb 2, 2005 6:52 AM
|
|
> To what extent do you feel automated tests should be > allowed to change the API being tested?
I don't like changing my API just for tests, either. I like the public API to be as spare as possible. Adding "test-only" methods to the public API violates this and potentially causes confusion to the API consumer/client.
In a similar situation to the case described I took the following approach. The class in question was "fronted" by an interface. I added the test-only methods to the class and not the interface. Accessibility for the interface and imlementing class was such that only the interface, and not the implementing class, was available to clients. (Clients could only access the functionality of the class via interface references returned by a factory). The tests I wrote also used interface references, but when I needed to call the test only methods, I cast my reference to the class reference so I could make those calls. A bit of a hack, perhaps, but it keeps my API clean. (The test class, as is typical with JUnit, was in the same package as the interface and its implementing class, allowing it to access the package-visible implementing class.
The whole structure I described above was not coincedental. Where I work the standard is that any business logic class is fronted by an interface, the implementing class has only package level access, and references can only be gained via a factory. Client code is implemented in different packages than where the business logic interfaces and implementation classes reside. At first I thought this was overkill. Most of our interfaces had, and ever would have, only one implementation behind them. But it does provide that nice clean separation between implementation and interface. In some cases you may never actually need it; but when it turns out you do and you already have it in place, it's really nice.
|
|