Code Testing:

## Before In my early years of developer, I must say with some shame how i ignored the code testing. Not giving it the importance and discarded all the benefits it came with it. These are primarily few of my reasons why I discard it: 1. Lack of time - Given by poor planning. 2. It is useless - I test my code myself calling it manually. So why? 3. The youth pride - I code perfectly, my code is flawless. 4. Ignorance - I didn´t researched all the tools for testing. 5. Others would test - Leave my testing to others departments.

Now

I must say I’m amazed on how I didn't use all the testing tools to bullet proof my code against all the errors that I didn't foresee with my coding. Now these reasons make me not have code untested: 1. Saves time - Saves a lot of time to put time on testing. The amount of time to make the test is an investment cause often a lot of errors can be found before going to production or qa. 2. Code error free - Well it can be discussed about this, because it always is going to be the logic errors, and so on but it will eliminate horrendous exceptions concerning your code. 3. Code style - It makes you change the way of coding, to make all the code testable some necessary changes have to be made, refactoring some classes on helper classes, deleting some private methods. Helping overall on reducing the amount of code smell. 4. TDD - After reading TDD I found the do the test first then the code part, it just didn´t clicked with me, I tried the approach several times finding it hard to connect the logic with a separate test. So, my approach became to code then test it, then i will go on a cycle testing and code, testing and code. Until i became satisfied with the result. 5. Integration Testing - I love integration testing, I became fascinated with Arquillian to test the persistence, queries, business logic on my backend or api. 6. Changes - The changes on the code became less scary with the tests over it. So, any change breaks your code the test would proof them against some logic errors and unforeseen consequences.

Testing Types

I discovered fooling around the tests that an application has to have these tests: 1. Functional test - Testing all the code on a functional level mocking everything else. 2. Integration test - I don't like to make a gigantic integration test, yet all the ejb,persistence should have an integration test. Even it’s a pain to do the test on arquillian for the dependencies. 3. Stress test - Test all the code with some real conditions, concurrency. 4. Profile test - After all the test pass, it became very useful to profile the application and found bottlenecks and refactor the application or use some new library.

Tools

So, the tools I became used to have to test my application: 1. Mockito - This library is incredible useful for mocking all the classes injected often by the container. So, it lets us focus on the test of our code. 2. Junit - This could be the standard tool for testing. 3. Arquillian - Used for integration test i can test my code on different servers, and on an isolated environment proving a time saver, testing all the queries, ejb, etc. 4. PowerMock - I add this on this list cause it’s a great tool to test some private methods, yet i discouraged testing the private methods overall. It's better to move the private methods to a helper class and made them public.

Conclusion

So, for me the journey to became a test freak on my code, make me a believer on testing, the results of using it are too good to not do it. Yet i founded a lot of code without testing and try to change the mentality about testing is what i found to be the most challenging.

Links

http://site.mockito.org/

http://arquillian.org/

http://agiledata.org/essays/tdd.html

Share this post: