Java单元测试进阶之如何打桩(扫除打桩的最后一道坎-使用easymockclassextension打桩类)

来源:互联网 发布:马蓉出轨的代价 知乎 编辑:程序博客网 时间:2024/05/03 17:59
用easymockclassextension进行类的打桩跟用easymock进行接口的打桩的步骤差不多,能执行的操作也差不多,看看下面代码:

欲打桩类:

 

/************************************************************
 * 项目名称: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;
    }


}

 欲测试类:

 

/************************************************************
 * Project Name: lhjTest
 * File Name   : UseCoolClass.java
 * File Desc   : UseCoolClass.java
 * Author      : Administrator
 * Create      : 2007-3-25
 * Modify:
 **********************************************************
*/

package org.lhj.cool.mock;

import org.lhj.cool.junit.CoolClass;

/**
 * 
@author Administrator
 
*/

public class UseCoolClass
{
    
private CoolClass cool;
    
    
public void setCoolClass(CoolClass cool)
    
{
        
this.cool = cool;
    }

    
    
public String reverseString(String inputStr) throws NullPointerException
    
{
        
return cool.reverseString(inputStr);
    }


}

 

测试代码如下:

 

/************************************************************
 * Project Name: lhjTest
 * File Name   : UseCoolClassJTest.java
 * File Desc   : UseCoolClassJTest.java
 * Author      : Administrator
 * Create      : 2007-3-25
 * Modify:
 **********************************************************
*/

package org.lhj.cool.mock;

import junit.framework.TestCase;

import org.easymock.MockControl;
import org.easymock.classextension.MockClassControl;
import org.lhj.cool.junit.CoolClass;
import org.lhj.cool.mock.UseCoolClass;

/**
 * 
@author Administrator
 
*/

public class UseCoolClassJTest extends TestCase
{
    
private MockControl ctrl;
    
    
private CoolClass cool;
    
    
private UseCoolClass testClass;

    
/* (non-Javadoc)
     * @see junit.framework.TestCase#setUp()
     
*/

    
protected void setUp() throws Exception
    
{
        
super.setUp();
        
        ctrl 
= MockClassControl.createControl(CoolClass.class);
        cool 
= (CoolClass) ctrl.getMock();
        
        testClass 
= new UseCoolClass();
        testClass.setCoolClass(cool);
    }


    
/* (non-Javadoc)
     * @see junit.framework.TestCase#tearDown()
     
*/

    
protected void tearDown() throws Exception
    
{
        
super.tearDown();
    }


    
/**
     * Test method for {
@link org.lhj.cool.mock.UseCoolClass#reverseString(java.lang.String)}.
     
*/

    
public void testReverseString()
    
{
        ctrl.expectAndThrow(cool.reverseString(
null), new NullPointerException());        
        ctrl.expectAndReturn(cool.reverseString(
""), null);
        ctrl.expectAndReturn(cool.reverseString(
"AAaa"), "aaAA");
        ctrl.expectAndReturn(cool.reverseString(
"aaAA"), "AAaa");
        ctrl.replay();
        
        
try
        
{
            testClass.reverseString(
null);
            fail();
        }

        
catch (NullPointerException e)
        
{
            
//donothing
        }

        assertNull(testClass.reverseString(
""));
        assertEquals(
"aaAA", testClass.reverseString("AAaa"));
        assertEquals(
"AAaa", testClass.reverseString("aaAA"));
        
        ctrl.verify();
    }


}

 

这里操作步骤跟easymock一模一样,所以就不多说了。

        如果读者能够从第一篇一直读到这里,并且把每个例子都试了一下,相信要进行单元测试已经没用技术上的问题了。剩下的问题就是如何高效地进行单元测试,如何提高单元测试的测试质量,这些问题已经不在本系列文章的谈论范围之内,有时间再写一下。

0 0
原创粉丝点击