1

I just wanted to ask if it is necessary to test the java servlet that calls another java method (in another file I might add) to do the bulk of the work.

Like for instance, my java servlet currently only is being used to get parameters and displaying the results of the method that was called in html format. So I am only planning on testing my main java file that does all the work!

I am not very familiar with testing so if there would be specific ways to test this simple Java servlet, please let me know!

Rachel Solomon
  • 177
  • 2
  • 13
  • 1
    *Would testing my Java Servlet be neccessary?* Define **necessary**. What if your servlet is broken? Do you want to know before you turn it in? If so, I'd test it. – Elliott Frisch May 26 '17 at 11:28
  • Thanks for your helpful comment! You are probably right in saying that. I am just not really sure how one would go about conducting either a unit test on a servlet that does so little. Would you have any tips? – Rachel Solomon May 26 '17 at 12:42

2 Answers2

1

Like for instance, my java servlet currently only is being used to get parameters and displaying the result of the method is has called in html format. So I am only planning on testing my main java file that does all the work!

Ideally, you should Why so?

  • If something messy happens and servlet broke befoe calling the Main business logic
  • Testing how parameters are fetched so you can be sure which kind of parameters my Servlet can handle (I personally don't like junk values to be passed to my business logic).
Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
  • Thanks for your answer! You make valid points. I can't believe I didn't really think about that before..testing the parameters and making sure I know what types the Servlet can handle!! I'll definitely try to implement that. – Rachel Solomon May 26 '17 at 12:43
1

Testing your Java Servlet or for that case, any Java code is very necessary. The way we always tend to write any code is called the HAPPY PATH, which sometimes overlooks possible scenarios that might break your service. It is always a best practice to code via the TDD way (test driven).

  • You can Unit Test your Servlet alone, if it does perform the main logic as a single unit.
  • You can perform Integration testing if your Servlet along with other Java code performs the logic as a whole.

There is this question on SO which covers almost all the details on how to perform this and also the various choices to choose from to perform them.

Hope this helps!

N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46