When I first heard about unit testing, I immediately thought it was stupid. Write code that tests your code. How absurd. Now I find it difficult to write code without tests.
One of the things that really pained me was writing setters for code that didn’t use them other than for unit tests. This led me to write a utility called Otasco. Why the name Otasco you ask? Well that story is for another post.
Consider the following unit test:
@RunWith(MockitoJUnitRunner.class)
public class InvoiceManagerTest {
@ClassUnderTest
private InvoiceManager manager;
@Mock
@Dependency
private InvoiceCalculator invoiceCalculator;
@Mock
@Dependency
private InvoiceDao invoiceDao;
@Before
public void setup() {
manager = new InvoiceManager();
Otasco.init(this);
}
}
This above would allow us to test the class under test InvoiceManager without actually having to write useless setters for members invoiceDao and invoiceCalculator.
I hope this helps you out. It has really helped out our development team to be a bit more productive. Let me know what you think.
You can check out the source at GitHub.
To use in your Maven project use the following repository:
<repository>
<id>otasco-releases</id>
<name>Otasco Releases</name>
<url>https://github.com/kenyattaclark/otasco-mvn-repo/raw/master/releases</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>