[英]How to unit test java main method?
我有一個類,我必須通過它的main方法傳遞2個參數,如果傳遞少於2個args,它會顯示一個系統錯誤消息。 我在這里為main方法編寫了一個單元測試,當我運行測試時,它停止在“running”(顯示既不通過也不失敗)。 請建議。
Example.java
public class Example
{
private static String str1 = null;
private static String str2 = null;
public static void main(String[] args)
{
if( args.length != 2 )
{
call();
}
Example ex = new Example(args[0], args[1]);
ex.getData();
}
public Example(String str1, String str2)
{
Example.str1 = str1;
Example.str2 = str2;
}
public void getData(){
System.out.println("Name is: "+str1);
System.out.println("City is: "+str2);
}
private static void call()
{
System.err.println("Usage: String1 String2");
System.err.println("Where: ");
System.err.println(" String1 - Name");
System.err.println(" String1 - City");
System.exit(1);
}
}
ExampleTest.java
public class ExampleTest {
@Test
public void testPassingWrongNumberOfInputs() {
StringBuffer sb = new StringBuffer();
sb.append("Usage: String1 String2")
.append("Where: ")
.append(" String1 - Name")
.append(" String1 - City");
String expectedErrorMessage = sb.toString();
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
System.setErr(new PrintStream(outContent));
String[] args = {"one"};
Example.main(args);
assertEquals(expectedErrorMessage, outContent.toString());
}
}
以下內容如何:
class TestingSecurityManager extends SecurityManager {
@Override public void checkExit(int status) {
throw new SecurityException();
}
}
然后在你的測試中......
public class ExampleTest {
@Test
public void testPassingWrongNumberOfInputs() {
StringBuffer sb = new StringBuffer();
sb.append("Usage: String1 String2")
.append("Where: ")
.append(" String1 - Name")
.append(" String1 - City");
String expectedErrorMessage = sb.toString();
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
System.setErr(new PrintStream(outContent));
String[] args = {"one"};
TestSecurityManager sm = new TestSecurityManager ();
System.setSecurityManager(sm);
try {
Example.main(args);
//should throw
fail("Should have thrown exception");
} catch (SecurityException se) {
}
assertEquals(expectedErrorMessage, outContent.toString());
}
}
我終於能夠編寫單元測試,如下所示。 我只測試了該方法是否符合System.exit(1)代碼。
public class ExampleTest {
private SecurityManager m;
private TestSecurityManager sm;
@Before
public void setUp()
{
m = System.getSecurityManager();
sm = new TestSecurityManager ();
System.setSecurityManager(sm);
}
@After
public void tearDown()
{
System.setSecurityManager(m);
}
@Test
public void testPassingWrongNumberOfInputs() {
try {
Example.main(new String[] {"one"});
} catch (SecurityException se) {
assertEquals("1", se.getMessage());
}
}
}
class TestSecurityManager extends SecurityManager {
@Override
public void checkPermission(Permission permission) {
if ("exitVM".equals(permission.getName()))
{
throw new SecurityException("System.exit attempted and blocked.");
}
}
@Override
public void checkExit(int status) {
throw new SecurityException(Integer.toString(status));
}
}
刪除System.exit(1)調用,您不需要它。 您的應用程序將在main()完成后退出,無需不必要的調用即可顯式終止VM。 此調用很可能導致您的JUnit在到達assertEquals語句之前停止執行,因為您剛剛告訴VM退出。
重命名main方法,並添加返回值,以便進行測試。 從main調用這個新方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.