首先main方法测试的主要原理参见下面的链接
http://www.cnblogs.com/ITEagle/archive/2010/07/10/1774888.html
1、callMainFailure
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
/**
* <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
}
}
* <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
}
}
2、MockSecurityManager
一个测试的帮助类
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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) {
}
}
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) {
}
}