main方法的测试

首先main方法测试的主要原理参见下面的链接

http://www.cnblogs.com/ITEagle/archive/2010/07/10/1774888.html


1、callMainFailure

    

代码
   
     
/**
* <p>
* Tests the main method. <code>System.exit</code> is expected.
* </p>
*
*
@param args
* the arguments.
*/
private static void callMainFailure(String[] args) {
try {
PriceConversionUtility.main(args);

fail(
" System.exit is expected. " );
}
catch (SecurityException e) {
// Good
}
}
不论main方法正常结束还是异常结束都要System.exit因此这个函数在准确性测试和异常测试中都可以用的到

2、MockSecurityManager

    一个测试的帮助类

   

代码
   
     
import java.security.Permission;

public final class MockSecurityManager extends SecurityManager {
/**
* <p>
* This method throws exception for testing if the status is 1 or 0.
* </p>
*
*
@param status
* The exit status code.
*
*
@throws SecurityException
* Always.
*/
public void checkExit( int status) {
super .checkExit(status);
if (status == 1 || status == 0 || status == - 1 ) {
throw new SecurityException( " Halt for testing. " );
}
}

/**
* <p>
* This method does nothing.
* </p>
*
*
@param perm
* Ignored.
*/
public void checkPermission(Permission perm) {
}
}

 

你可能感兴趣的:(main)