我正在爲使用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");
}
}
}
}
}
};
}
}
您可能需要顯示一些代碼才能提供建議。 – notionquest
請顯示您用作測試的代碼示例,並說明您在自定義規則中所做的操作。 –
請提供與斷言錯誤關聯的消息或缺陷跟蹤。 –