【发布时间】:2016-12-11 11:38:20
【问题描述】:
我正在使用 java 为 java 编写自定义声纳规则。我遇到了一个无法轻易修复的断言错误。我确定源代码是正确的。但是测试用例不能通过。我想知道在使用TDD过程时我应该注意什么以及如何解决它。
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;
}
}
我在测试上面的文件,我写了一个规则来测试是否在logger中打印了没有抛出的异常。
消息是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");
}
}
}
}
}
};
}
}
【问题讨论】:
-
您可能需要展示一些代码来提供建议。
-
请展示您用作测试的代码示例,并说明您在自定义规则中所做的工作。
-
请提供与断言错误相关的消息或轨迹。