Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Leona Wright   on Dec 24 In Java Category.

  
Question Answered By: Kawthar Malik   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");
}
}

Share: 

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


Tagged: