954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

java junit - how to write test cases

hi could someone help me in giving a tutorial in learning how to write testcases in java.
would preffer in netbeans if eclipse dosen't matter,

thanks in advance

CodeJava1
Newbie Poster
4 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Well, the basics are quite simple. In junit 4 you annotate your test methods like the following. Your class doesn't have to extend anything.

public class SimpleTest {
   @Test
   public void aTestCase() {
      //Some test code...
   }
}


In Junit 3 you method name has to end with Test and the class has to extend TestCase.

public class SimpleTest extends TestCase {
   public void aSimpleTest() {
      //Some test code...
   }
}


I would recommend starting with Junit 4 right away. It's easier in my opinion. Especially when setting up test suites. Just google Junit 4 tutorial.

Regarding netbeans and other IDE:s, There are probably plugins that make creation of test classes and stuff a bit faster, google that.

SasseMan
Junior Poster
176 posts since Jan 2010
Reputation Points: 70
Solved Threads: 19
 

You of course also have to import some Junit classes in both cases.

SasseMan
Junior Poster
176 posts since Jan 2010
Reputation Points: 70
Solved Threads: 19
 
peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 
JUnit tutorials

thanks everyone for the comments

krishnisilva
Junior Poster
122 posts since Apr 2010
Reputation Points: 10
Solved Threads: 4
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: