2016-08-05 26 views
2

我正在爲使用java的java編寫定製聲納規則。我遇到了一個不容易修復的斷言錯誤。我確定源代碼是正確的。但測試用例不能通過。我想知道使用TDD過程時應該關心什麼,以及如何解決這個問題。如何在爲java編寫定製聲納規則時處理assertionError

public class logTCheckFile { 
    private static Logger logger = Logger.getLogger(logTCheckFile.class); 
    public void loggingWithID(String nonsense) throws myException{ 
     logger.error("errorID:20160801 this is an error"); 
     return; 
    } 

    public void loggingWithoutID(String nonsens){ 
     try{ 
      logger.error("this is an error"); 
     }catch(NullPointerException e){ 
      logger.error("what",e); 
     } 
     return; 
    } 

    public void specific(){ 
     logger.error("only the logger"); 
     try{ 
      logger.error("this is an error"); 
     }catch(NullPointerException e){ 
      logger.error("without an exception"); 
     } 
     return; 
    } 
} 

我在測試上面的文件,我寫了一個規則來測試在記錄器中是否打印出未拋出的異常。

的消息是AssertionError: Unexpected at [20](這裏是失敗的堆棧跟蹤的圖片)

的代碼我寫檢查文件如下:

public class logTCheck extends IssuableSubscriptionVisitor { 


Logger log = Logger.getLogger(logTCheck.class); 

@Override 
public List<Kind> nodesToVisit() { 
    return ImmutableList.of(Kind.METHOD); 
} 

@Override 
public void visitNode(Tree tree){ 
    MethodTree method = (MethodTree) tree; 
    if(method.throwsClauses().size()==0){ 
     log.info("this method does not have a throw clause"); 
     BlockTree bt = method.block(); 
     for(StatementTree st:bt.body()){ 
      if(st.is(Kind.TRY_STATEMENT)){ 
       TryStatementTree tst = (TryStatementTree) st; 
       for(CatchTree ct:tst.catches()){ 
        for(StatementTree state:ct.block().body()){ 
         ExpressionStatementTree ex = (ExpressionStatementTree)state; 
         MethodInvocationTree mit = (MethodInvocationTree) ex.expression(); 
         if(mit.arguments().size()!=2){ 
          log.error(method.simpleName()); 
          reportIssue(method.simpleName(), "you didn't print the exception in the log"); 
         } 
        } 
       } 
      } 
     } 
    }; 
} 
} 
+2

您可能需要顯示一些代碼才能提供建議。 – notionquest

+0

請顯示您用作測試的代碼示例,並說明您在自定義規則中所做的操作。 –

+0

請提供與斷言錯誤關聯的消息或缺陷跟蹤。 –

回答

1

The message is AssertionError: Unexpected at [20]

這意味着測試數據的第20行包含違反了您正在檢查的規則。

您需要告訴驗證程序此違規是故意在那裏。 一個簡單的方法做,這是違反之前,合適就行添加註釋是這樣的:

// [email protected]+1 {{the violation message}} 

@+1指侵犯,是對下一行。 適當調整數量。

註釋必須位於行的開頭, 或//前面可能有空格。

{{...}}中包含的違規消息是可選的,但強烈建議。 在做TDD時, 輸入正確信息的簡單方法是添加{{x}}這樣會導致測試失敗的信息, 然後您可以將測試輸出中的信息複製到測試文件中進行修復。

0

我終於發現了問題的另一個答案來自Michael。我沒有告訴測試者該問題應該在哪裏。我應該使用註釋// Noncompliant來標記問題。