简体   繁体   English

理解JUnit4测试失败吗?

[英]Understand failing JUnit4 test?

The following is supposed to throw an InvalidExpression exeption if the string "what" doesn't contain an operation (+, -, *, /). 如果字符串“ what”不包含运算符(+,-,*,/),则下面的代码应该抛出InvalidExpression异常。

//Check if the input contains at least one operation
else if(!what.matches(".*[+ \\- * \\/].*")) {
    throw new InvalidExpression("No operation in the expression");
}

However if I run the following JUnit4 Test I don't get correct exception. 但是,如果我运行以下JUnit4测试,则不会得到正确的异常。 The evaluate method calculates the math expression, and in theory should contain an operation. 评估方法将计算数学表达式,并且理论上应包含一个运算。

@Test (expected = InvalidExpression.class)
public void test() throws InvalidExpression {
    testCalc.evaluate("5 5");
}

You have spaces inside the brackets. 括号内有空格。 So a space is a valid operator. 因此,空格是有效的运算符。

Your regex should be 您的正则表达式应为

".*[+\\-*/].*"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM