Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

New to JUnit

  Asked By: Leona    Date: Dec 24    Category: Java    Views: 682
  

I am new to junit. I've downloaded 3.8.1. I didn't know how to start. I
have developed a project using java swing. It has 3 modules and 8 java programs.
I have to test my project (unit testing, integration testing). Can anyone tell
me
a way to start testing and what are the procedures. I can understand if you
provide any example. Kindly provide if there is any tutorial link to start with
JUnit.

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Kawthar Malik     Answered On: Dec 24

import junit.framework.TestCase;
import junit.framework.Assert;

/**
* Very basic example  of a test  case
*/
public class SampleTest extends TestCase {

/**
* the object that you are testing
*/
Object o;

/**
* Constructor
*
* @param name
*/
public SampleTest(String name) {
super(name);
}

/**
* called every time it runs a test method.
*/
protected void setUp() {
o = new Object();
}

/**
* called after any test method is run.
*/
protected void tearDown() {
o = null;
}

/**
* A simple main method.
*
* @param args
*/
public static void main(String args[]) {
junit.textui.TestRunner.run(SampleTest.class);
}

/**
* TestClass uses reflection to find every single method that
* has the word 'test' in it. So in this case it is detected
* on the method testSomething() method.
* <p/>
* Bascily to make it work you manipulate some data in the
* Object you are testing, and then make a bunch of Assertions
* that what you want to happen based off of that manipulation
* really happens
* <p/>
* In this case, I just want to know if the Object.toString()
* method returns a String containing "someText". Of course this
* test will fail :)
*/
public void testSomething() {
Assert.assertEquals(o.toString(), "someText");
}
}

 
Didn't find what you were looking for? Find more on New to JUnit Or get search suggestion and latest updates.




Tagged: