I need to test the following code:
我需要测试以下代码:
try{
while((testBean = csvReader.read(TestBean.class,headers,getCellProcessors()))!=null){
System.out.println("no excpetion");
i=5;
}
}catch(SuperCsvException csvExc){
System.out.println("superCSV excpetion");
i=0;
}catch(Exception ex){
System.out.println("excpetion");
i=0;
}
How to test that whether SuperCsvException
is thrown and caught.
如何测试是否抛出并捕获了SuperCsvException。
7 个解决方案
#1
4
JUnit 4 supports this
@Test(expected=<YourExpectedException>.class)
public void testExceptionThrown() {
// call the method that throws exception
}
#2
1
The "new" JUnit way of expecting exceptions is the way of rules:
期望例外的“新”JUnit方式是规则的方式:
public class ThisIsTheTestClass {
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void testingSomeBehaviorThatShouldThrowAnException() throws SuperCsvException {
exception.expect(SuperCsvException.class);
// Put your testing effort here (setup, call, assertions, ...).
}
}
Look at some information about rules, if interested further.
如果有兴趣,请查看有关规则的一些信息。
#3
0
Either the exception is expected or it's not expected. If expected then you need something like this:
要么是预期的,要么是预期的。如果预期那么你需要这样的东西:
try {
doSomething();
fail("No exception thrown");
}
catch (MyException e) {
// expected
}
#4
0
JUnit 4 has support for this. You can declare your unit test as:
JUnit 4支持这一点。您可以将单元测试声明为:
@Test(expected=SuperCsvException.class)
public void _testSuperCsvException() {
// call to function containing your code
}
#5
0
My personal experience is always like
我的个人经历总是如此
boolean caught = false;
try {
runCodeToThrowException();
} catch (MyException e) {
caught = true;
}
assertTrue(caught);
The reason I dislike to have JUnit handle it is I couldn't control exactly where and how this exception is thrown. That might not be good if I am trying to catch anything generic in a big trunk of code, e.g. IllegalArgumentException
.
我不喜欢JUnit处理它的原因是我无法准确控制抛出此异常的位置和方式。如果我试图在一大段代码中捕获任何通用内容,那可能不太好,例如IllegalArgumentException异常。
#6
0
Your testing approach is wrong. You should consider each method or class you are testing as a unit that has a specification, an accessible interface (non private
methods and fields) and an implementation. You should test that the unit, when manipulated through its accessible interface, conforms to its specification. You should not test that the unit has a particular implementation. You may use your knowledge of the implementation to guide your selection of test cases, to choose cases that are likely to be incorrectly implemented.
你的测试方法是错误的。您应该将您正在测试的每个方法或类视为具有规范,可访问接口(非私有方法和字段)和实现的单元。您应该测试该单元在通过其可访问接口进行操作时是否符合其规范。您不应该测试该单元是否具有特定实现。您可以使用您对实现的了解来指导您选择测试用例,以选择可能未正确实现的案例。
So in your case, the fact that an exception can be thrown is an implementation detail. You would be wise to have a test case that will also cause that exception to be thrown. As your code tries to catch the exception, I guess this is because the specification of your method says that the method should not throw any exceptions if there is a problem with csvReader
. So you could have a test case that sets up csvReader
to throw an exception. JUnit will fail the test if the method does throw the exception.
因此,在您的情况下,可以抛出异常的事实是实现细节。你最好有一个测试用例,它也会引发异常。当您的代码试图捕获异常时,我想这是因为您的方法的规范说如果csvReader存在问题,该方法不应抛出任何异常。所以你可以有一个测试用例来设置csvReader来抛出异常。如果方法抛出异常,JUnit将无法通过测试。
Note the difference: that test case does not test that the exception is thrown and caught (an implementation detail); it tests that the method does not throw or propagate an exception in the situation that a call to csvReader.read
will throw an exception. The implementation is allowed to satisfy that constraint by catching the exception or by refraining from calling csvReader.read
.
注意区别:测试用例不测试抛出和捕获异常(实现细节);它测试该方法在调用csvReader.read将引发异常的情况下不抛出或传播异常。允许实现通过捕获异常或禁止调用csvReader.read来满足该约束。
#7
-1
If SuperCsvException is subclass of Exception try something like this:
如果SuperCsvException是Exception的子类,请尝试以下方法:
Exception ex=null;
try {
while ((testBean = csvReader.read(TestBean.class, headers, getCellProcessors())) != null) {
System.out.println("no excpetion");
i = 5;
}
} catch (SuperCsvException csvExc) {
System.out.println("superCSV excpetion");
i = 0;
ex=csvExc;
} catch (Exception ex) {
System.out.println("excpetion");
i = 0;
}
Assert.assertNotNull(ex);
#1
4
JUnit 4 supports this
@Test(expected=<YourExpectedException>.class)
public void testExceptionThrown() {
// call the method that throws exception
}
#2
1
The "new" JUnit way of expecting exceptions is the way of rules:
期望例外的“新”JUnit方式是规则的方式:
public class ThisIsTheTestClass {
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void testingSomeBehaviorThatShouldThrowAnException() throws SuperCsvException {
exception.expect(SuperCsvException.class);
// Put your testing effort here (setup, call, assertions, ...).
}
}
Look at some information about rules, if interested further.
如果有兴趣,请查看有关规则的一些信息。
#3
0
Either the exception is expected or it's not expected. If expected then you need something like this:
要么是预期的,要么是预期的。如果预期那么你需要这样的东西:
try {
doSomething();
fail("No exception thrown");
}
catch (MyException e) {
// expected
}
#4
0
JUnit 4 has support for this. You can declare your unit test as:
JUnit 4支持这一点。您可以将单元测试声明为:
@Test(expected=SuperCsvException.class)
public void _testSuperCsvException() {
// call to function containing your code
}
#5
0
My personal experience is always like
我的个人经历总是如此
boolean caught = false;
try {
runCodeToThrowException();
} catch (MyException e) {
caught = true;
}
assertTrue(caught);
The reason I dislike to have JUnit handle it is I couldn't control exactly where and how this exception is thrown. That might not be good if I am trying to catch anything generic in a big trunk of code, e.g. IllegalArgumentException
.
我不喜欢JUnit处理它的原因是我无法准确控制抛出此异常的位置和方式。如果我试图在一大段代码中捕获任何通用内容,那可能不太好,例如IllegalArgumentException异常。
#6
0
Your testing approach is wrong. You should consider each method or class you are testing as a unit that has a specification, an accessible interface (non private
methods and fields) and an implementation. You should test that the unit, when manipulated through its accessible interface, conforms to its specification. You should not test that the unit has a particular implementation. You may use your knowledge of the implementation to guide your selection of test cases, to choose cases that are likely to be incorrectly implemented.
你的测试方法是错误的。您应该将您正在测试的每个方法或类视为具有规范,可访问接口(非私有方法和字段)和实现的单元。您应该测试该单元在通过其可访问接口进行操作时是否符合其规范。您不应该测试该单元是否具有特定实现。您可以使用您对实现的了解来指导您选择测试用例,以选择可能未正确实现的案例。
So in your case, the fact that an exception can be thrown is an implementation detail. You would be wise to have a test case that will also cause that exception to be thrown. As your code tries to catch the exception, I guess this is because the specification of your method says that the method should not throw any exceptions if there is a problem with csvReader
. So you could have a test case that sets up csvReader
to throw an exception. JUnit will fail the test if the method does throw the exception.
因此,在您的情况下,可以抛出异常的事实是实现细节。你最好有一个测试用例,它也会引发异常。当您的代码试图捕获异常时,我想这是因为您的方法的规范说如果csvReader存在问题,该方法不应抛出任何异常。所以你可以有一个测试用例来设置csvReader来抛出异常。如果方法抛出异常,JUnit将无法通过测试。
Note the difference: that test case does not test that the exception is thrown and caught (an implementation detail); it tests that the method does not throw or propagate an exception in the situation that a call to csvReader.read
will throw an exception. The implementation is allowed to satisfy that constraint by catching the exception or by refraining from calling csvReader.read
.
注意区别:测试用例不测试抛出和捕获异常(实现细节);它测试该方法在调用csvReader.read将引发异常的情况下不抛出或传播异常。允许实现通过捕获异常或禁止调用csvReader.read来满足该约束。
#7
-1
If SuperCsvException is subclass of Exception try something like this:
如果SuperCsvException是Exception的子类,请尝试以下方法:
Exception ex=null;
try {
while ((testBean = csvReader.read(TestBean.class, headers, getCellProcessors())) != null) {
System.out.println("no excpetion");
i = 5;
}
} catch (SuperCsvException csvExc) {
System.out.println("superCSV excpetion");
i = 0;
ex=csvExc;
} catch (Exception ex) {
System.out.println("excpetion");
i = 0;
}
Assert.assertNotNull(ex);