1
votes

I am developing a REST API. I have Jmeter tests already for the functional testing. I wanted to add unit tests and also want to follow test driven development(TDD) from now on to make any enhancement and add new functionality to my existing project (which is bound to grow very complex and does not have any unit tests in place).

After reading a lot about TDD I am little confused about whether I should be going for TDD. There are extreme outlooks for and against it.

I think I will follow TDD only to develop my service layer, which encompasses only business logic.

Any suggestions about my approach ?

3
Think about asking this here: programmers.stackexchange.comFazoM

3 Answers

2
votes

TDD is more than that. It is not only a way for you to check if a system is externally working fine. TDD is also a means to accelerate development of your classes, even if they do not interact with other systems.

Think of a test as a response to the following questions:

  1. Am I done developing this class?
  2. Do the classes I already developed and tested still work fine after a change I've made?
  3. How to represent requirements as source code?

An explanation about each question:

  1. How do you know you are done writing a class? A test can tell you that by only showing a test successful message after your class does everything it was supposed to do.
  2. You need test automation in order to test often.
  3. Whenever you have a new requirement, write a new test that represents this requirement.
1
votes

TDD is one of the best practices to follow as you would test before you develop. And you would know at each and every step during developing the application/service if you have broken any previous functionality.

You are at the right path and I would always encourage using TDD. If you are working on a project right from scratch then just go for it.

Since yours is an existing project it may be a headache at first till you cover unit test cases for all the existing functionality.

So the best approach is:

  • Write down all the unit test cases for existing functionality before you start development for new functionality.
  • While doing that you may come up with a lot of surprises and may end up re-factoring a lot of code. This will help you while developing future new functionality.
  • Now since your code looks much better you can start TDD for the new functionality.

Let me know if this helps. I have used TDD in many of my projects and I am comfortable with that.

1
votes

Since you're using Spring, I'd suggest that the object to unit test should not be a web service. I'd make it an interface-based POJO. The behavior should not be affected by the choice to deploy as REST.

Marshaling and unmarshaling the HTTP request and response to objects for the POJO to consume can be separate.

This arrangement will have the added benefit of not requiring deployment to a container in order to test.