在单元测试框架出现之前,进行单元测试最简单的方法就是在main方法里面写测试代码。请看下面例子:
/************************************************************
* 项目名称:lhjTest
* 文件名称:CoolClass.java
* 文件描述:CoolClass.java
* 作者:Administrator
* 创建日期:2007-3-23
* 修改记录:
***********************************************************/
package org.lhj.cool.junit;
/**
* @author Administrator
*/
public class CoolClass
{
/**
* constructor
*/
public CoolClass()
{
super();
}
/**
* @param inputStr -the string need to be reverse.
* @return null if the inputStr is "",others the reverse string of the inputStr.
*/
public String reverseString(String inputStr)
{
if (null == inputStr)
{
throw new NullPointerException("Error:the input string is null.");
}
if ("".equals(inputStr))
{
return null;
}
int length = inputStr.length();
char[] outputChars = new char[length];
for (int i = 0; i < length; i++)
{
outputChars[i] = inputStr.charAt(length - i - 1);
}
return new String(outputChars);
}
/**
* this method is for test.
* @param args
*/
public static void main(String[] args)
{
//set up the invironment
CoolClass testClass = new CoolClass();
//test the null param
try
{
testClass.reverseString(null);
System.out.println("Error:expect throw nullPointerExceptioin but not.");
}
catch (NullPointerException e)
{
System.out.println("Succeed:test the null param for reverseString succeed.");
}
//test the "" param
if (!(null == testClass.reverseString("")))
{
System.out.println("Error:expect return the null value but not.");
}
else
{
System.out.println("Succeed:test the "" param for reverseString succeed.");
}
//test the "aaAA" param
if (!"AAaa".equals(testClass.reverseString("aaAA")))
{
System.out.println("Error:expect return AAaa but not.");
}
else
{
System.out.println("Succeed:test the aaAA param for reverseString succeed.");
}
//test the "AAaa" param
if (!"aaAA".equals(testClass.reverseString("AAaa")))
{
System.out.println("Error:expect return aaAA but not");
}
else
{
System.out.println("Succeed:test the AAaa param for reverseString succeed.");
}
//the other tests...
//tear down the environment
testClass = null;
}
}
* 项目名称:lhjTest
* 文件名称:CoolClass.java
* 文件描述:CoolClass.java
* 作者:Administrator
* 创建日期:2007-3-23
* 修改记录:
***********************************************************/
package org.lhj.cool.junit;
/**
* @author Administrator
*/
public class CoolClass
{
/**
* constructor
*/
public CoolClass()
{
super();
}
/**
* @param inputStr -the string need to be reverse.
* @return null if the inputStr is "",others the reverse string of the inputStr.
*/
public String reverseString(String inputStr)
{
if (null == inputStr)
{
throw new NullPointerException("Error:the input string is null.");
}
if ("".equals(inputStr))
{
return null;
}
int length = inputStr.length();
char[] outputChars = new char[length];
for (int i = 0; i < length; i++)
{
outputChars[i] = inputStr.charAt(length - i - 1);
}
return new String(outputChars);
}
/**
* this method is for test.
* @param args
*/
public static void main(String[] args)
{
//set up the invironment
CoolClass testClass = new CoolClass();
//test the null param
try
{
testClass.reverseString(null);
System.out.println("Error:expect throw nullPointerExceptioin but not.");
}
catch (NullPointerException e)
{
System.out.println("Succeed:test the null param for reverseString succeed.");
}
//test the "" param
if (!(null == testClass.reverseString("")))
{
System.out.println("Error:expect return the null value but not.");
}
else
{
System.out.println("Succeed:test the "" param for reverseString succeed.");
}
//test the "aaAA" param
if (!"AAaa".equals(testClass.reverseString("aaAA")))
{
System.out.println("Error:expect return AAaa but not.");
}
else
{
System.out.println("Succeed:test the aaAA param for reverseString succeed.");
}
//test the "AAaa" param
if (!"aaAA".equals(testClass.reverseString("AAaa")))
{
System.out.println("Error:expect return aaAA but not");
}
else
{
System.out.println("Succeed:test the AAaa param for reverseString succeed.");
}
//the other tests...
//tear down the environment
testClass = null;
}
}
以上代码在main方法中对CoolClass的reverseString方法进行各种测试:包括抛异常、返回null、以及正常测试。在测试之前先设置好测试环境(这里很简单,只是新建一个CoolClass对象);测试之后要释放掉所用资源(这里只是简单的把CoolClass对象设置为null);测试过程中通过传入不同参数调用CoolClass对象的reverseString方法,然后对返回值(对于抛异常的测试用try、catch语句)进行判断,假如成功就显示成功的信息,假如失败就显示失败的信息,通过控制台的显示可以知道测试的运行结果。
这就是进行单元测试的过程,无论是使用框架还是不使用框架,过程大概就这样,只不过框架可以省很多工作,并且可以自动化测试罢了。
这样进行单元测试有以下缺点:
1、随着类的增大,测试代码也会增多,main方法就会越来越大,整个类会迅速地膨胀。
2、无法自动化测试,必须一个一个地输入java ...(当然,用脚本的形式也可以做到批量执行)
3、main可以对类的private成员即方法进行访问,测试结果跟预期并不一定符合
4、软件发布时无法将测试代码分离。
下一篇会介绍解决以上问题的方法。