Tutorial :How to test java code?



Question:

I've recently starting learning java web development and I want to learn/understand:

  1. What constitutes good java test good? For example, if there is back-end java code that validates user input from a form and then inserts into a db table, what should the respective test code look like?

  2. How do testing frameworks like TestNG, JUnit, etc. fit into the java code testing process?

Thanks.


Solution:1

That's a pretty big topic... you might want to pickup or download a copy of "Pragmatic Unit Testing with JUnit" (http://pragprog.com/titles/utj/pragmatic-unit-testing-in-java-with-junit) it covers what you are looking for.

Also, the JUnit (http://junit.org) website should be a good starting point.

Good luck.


Solution:2

I dare to say that JUnit is the most popular Java testing framework out nowadays. There are fairly a lot of resources around. To start, there's an book called JUnit in Action which covers servlet (web) testing as well. Then there are lot of online resources to learn how to JUnit-test servlets.

As to the actual testing: best would be to test the complete request-response cycle as well as the individual steps involved in the cycle. This may sound a bit tedious but it also depends on business requirements what use cases exactly you need to test.


Solution:3

If you really want to get started with Java Unit testing for web development you may want to look into the Appfuse/Appfuse 2 projects, they're both Java based and both have stupendous tutorials to get you started.

Appfuse generates a pre-setup project for you so you can see how it fits together and works. Additionally it allows you to pick from some frameworks of your choosing to do web-development with, and integrates those with JUnit.

Although be warned that the projects are a bit old; even so it's a great way to get started learning about unit testing.


Solution:4

I'd start with JUnit. Google some examples of its usage. Keep your methods small.

ECLEmma is then a useful Eclipse plugin for monitoring test coverage.


Solution:5

The jUnit plugin for eclipse is quite useful, also the metrics package will analyse code coverage, and look at what code has been tested and what needs to be tested. Also analysing the different paths through your program, pretty useful if needing something quick on how 'tested' your code is...


Solution:6

Junit is more direct and simpel to use if you start to learn Java: TestNG has more features but maybe is more complex.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »