2013-01-18 28 views
-1

從我的JUnit測試中,我想調用此方法,但是它可以讓我調用它,但是它應該會導致錯誤,它會通過測試而不是失敗。從創建失敗的JUint測試中調用方法

public void handleErrors(String string, boolean b) { 
    System.out.println(string + ", " + b); 
    if(b == false){ 
     collector.addError(new Throwable(string + ", " + b)); 
    } 
} 

如果我通過這個測試中應該導致失敗,但它不會失敗。

@Test 
public void test() throws InterruptedException { 
    handleErrors("Button was found", false); 
} 

爲什麼JUint沒有報告失敗?

編輯:

package com.selenium.tests; 
import org.junit.After; 
import org.junit.Before; 
import org.junit.Rule; 
import org.junit.Test; 
import org.openqa.selenium.By; 
import org.junit.rules.ErrorCollector; 
import com.selenium.AbstractScreenTest; 
public class test1 extends AbstractScreenTest{ 

@Rule 
public ErrorCollector collector= new ErrorCollector(); 

@Before 
public void initialize() { 
createTest(); 
} 

@Test 
public void test() throws InterruptedException { 
handleErrors("Button was found", false); 
} 

public void handleErrors(String string, boolean b) { 
System.out.println(string + ", " + b); 
if(b == false){ 
collector.addError(new Throwable(string + ", " + b)); 
} 
} 

@After 
public void tearDown(){ 
closeTest(); 
} 

} 
+1

爲什麼這會失敗?它不會拋出異常,測試不會斷言任何事情。 –

+0

因爲我通過了錯誤,如果它是錯誤的,它會添加一個新的可拋出錯誤,並且它在JUint測試文件中有效,但不在這個單獨的類中。 – Colin747

+0

請向我們展示可用的代碼。但是,當測試被寫入時,沒有東西會導致它失敗。如果拋出異常(技術上是可拋出的)或斷言失敗,並且在您的示例中都沒有發生,則測試將失敗。 –

回答

0

修正這一點。我沒有移動

@Rule public ErrorCollector collector= new ErrorCollector(); 

行。

0

我想你沒有與@rule註釋收藏家。它應該是ErrorCollector。有一個從API文檔的例子:

public static class UsesErrorCollectorTwice { 
    @Rule 
    public ErrorCollector collector= new ErrorCollector(); 

    @Test 
    public void example() { 
      collector.addError(new Throwable("first thing went wrong")); 
      collector.addError(new Throwable("second thing went wrong")); 
      collector.checkThat(getResult(), not(containsString("ERROR!"))); 
      // all lines will run, and then a combined failure logged at the end. 
    } 
} 

希望這有助於

+0

它在不同的類中,當我將'@ Rule'放入時,出現錯誤,指出'@規則不允許用於此位置。 – Colin747

相關問題