Startup Project – Part 3 : Testing, Mocking and Learning

Untitled design (5)It’s amazingly interesting what you learn when you’re in the throws of unit testing code. It particular when you’ve written more code than you’ve written unit tests. Yeah, yeah, I know, unit tests should be written with the code as you go; TDD. But let’s face it sometimes you just need to get stuff done. Until you get to the point where you think that now’s a good time to unit test everything.

I spent the whole afternoon, up until 11:30pm writing unit tests. Refactoring some code to make some of it easy to mock. And other parts I rewrote simply because it’s the right thing to do for OOP’s sake. It’s fun. Not everyone likes to unit test, but I love it. It gives me that satisfaction that my code is well written. Especially that feeling you get when you know the architecture you designed is solid. And the libraries you’ve chosen haven’t failed you. Plus learning something new about Java 8.

Default Interface Method

A while back, I had learned about Java 8’s Default Interface Methods. It’s a feature I didn’t understand the need for considering we have abstract classes. But I was in a situation where I had written a method that generates a JSON output of my beans that I could use for testing. But I found myself writing the same method for my request beans, my DTOs, my DAO objects, etc. And it didn’t make sense to write one unifying abstract class just to have that one method. Especially, since there was no unifying design except that they were all beans. That’s when I decided that the Default Interface Method would be the perfect. So I made the conversion. Tested it. Amazingly, all my unit tests and mocks still worked with no adjustment.

Spark Framework and unirest

I’m glad I chose Spark to build my website. It is super easy to implement. Unit tests are a breeze. I say unit tests even though it’s really quasi-integration tests. Particularly, since I am doing HTTP requests to test the app. But considering it’s a self-contained environment, I’m happy with the results. To do the HTTP requests I used a library called unirest which is just a simple to use as Spark. The future of Java is in these super simple libraries. As powerful as the Spring Framework is, it’s not worth the headache.

Integration Tests

I’ve started looking at ways to do integration tests with JUnit. I’ve already had a quick look and it seems there is a way. I’m familiar with JBehave. But since I’m testing my own code, I’m looking for something closer to Java. If anyone has some good suggestions, let me know.

My goal is to have as much of the testing done via some sort of automated tests. Since it’s just me, the more time I can save from manual testing the better. Just executing the same tests over and over, the faster I can get the application done.

For anyone who has advice on the advice on the above, please comment below.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s