JAVAExpectedException导致错误
设置:JAVAExpectedException导致错误,java,exception,junit,Java,Exception,Junit,设置: java 1.8 JUnit4.8.1 Java不是我真正喜欢的东西。但我还是在和JUnit玩。 我想写一个测试用例,他的结果是一个例外 我遵循了一些我发现没有运气的例子 import biblioteca.exception.InvalidAuthorException; import biblioteca.util.Validator; import org.junit.Rule; import org.junit.Test; import org.junit.rules.Exp
- java 1.8
- JUnit4.8.1
import biblioteca.exception.InvalidAuthorException;
import biblioteca.util.Validator;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* Test
*/
public class MyTest {
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void testValidator()
{
assertTrue(Validator.isAlphaNumeric("A1"));
assertFalse(Validator.isAlphaNumeric("><"));
assertTrue(Validator.validateLength("vasile", 2, 64));
assertFalse(Validator.validateLength("vasile", 24, 64));
assertTrue(Validator.containsAtLeastOneLetter("1234a"));
assertFalse(Validator.containsAtLeastOneLetter("1234"));
}
@Test
public void testException() {
Validator.validateAuthor("123");
exception.expect(InvalidAuthorException.class);
}
}
导入biblioteca.exception.invalidauthoreexception;
导入biblioteca.util.Validator;
导入org.junit.Rule;
导入org.junit.Test;
导入org.junit.rules.ExpectedException;
导入静态org.junit.Assert.assertFalse;
导入静态org.junit.Assert.assertTrue;
/**
*试验
*/
公共类MyTest{
@统治
public ExpectedException exception=ExpectedException.none();
@试验
公共void testValidator()
{
资产真实(Validator.isAlphaNumeric(“A1”));
assertFalse(Validator.isAlphaNumeric(“>您只需要颠倒
Validator.validateAuthor("123");
及
如果
抛出异常时,您的测试函数(与任何Java函数一样)将在该未捕获异常时立即终止,因此exception.expect永远不会发生
编写预期异常的测试的另一种方法是:
@Test
public void testException() {
boolean exceptionOccurred = false;
try
{
Validator.validateAuthor("123");
}
catch( InvalidAuthorException e )
{
exceptionOccurred = true;
}
Assert.assertTrue( "Error: Expected test to throw an InvalidAuthorException",exceptionOccurred );
}
@Test( expected = InvalidAuthorException.class )
public void testException() {
Validator.validateAuthor("123");
}
更详细的是,不太清楚测试是否为否定测试,但它有一个重要的优点,异常不会终止测试功能,因此您可以执行其他操作,如日志记录和进一步检查故障状态,这是您可以检查实际异常消息的唯一方法,如下所示:
@Test
public void testException() {
try
{
Validator.validateAuthor("123");
Assert.fail("Error: Expected to get an InvalidAuthorException"); // Necessary for failing the test if there is no exception. Without this the test will pass
}
catch( InvalidAuthorException e )
{
Assert.assertEquals("Bad Author", e.getMessage());
}
}
还有一种方法(在JUnit引入规则之前)是这样的:
@Test
public void testException() {
boolean exceptionOccurred = false;
try
{
Validator.validateAuthor("123");
}
catch( InvalidAuthorException e )
{
exceptionOccurred = true;
}
Assert.assertTrue( "Error: Expected test to throw an InvalidAuthorException",exceptionOccurred );
}
@Test( expected = InvalidAuthorException.class )
public void testException() {
Validator.validateAuthor("123");
}
它很好,因为它非常简洁,而且很明显,它是一个否定测试,如果没有异常发生,它将使您的测试失败,当异常发生时,它将通过您的测试,但是您将无法在该测试函数中添加任何更多的测试语句,因为异常再次终止测试函数,因此无法检查异常n消息。请输入文本代码,而不是图片。@Chieftwo Pencils Doneal所以你的测试有点糟糕。这是个人的偏好,但我总是被教导在测试用例中只测试一件事。原因是如果你测试失败,你不想花太多时间来找出失败的原因。测试很简单单元测试的目的是加快开发人员的进度,并放慢速度,使用调试器(或类似工具)找出导致单元测试失败的原因,这往往与“速度”相反。"事实上,如果抛出异常,则无法达到预期。由于try/catch示例,我必须收回我的投票。这是一种方法,但通常是一个糟糕的选择。它添加了不必要的样板代码,并消除了异常可能带来的任何有意义的信息。例如,您的测试在将作为开始的断言上失败堆栈跟踪的一部分。