简体   繁体   English

在doThrow JUNIT之后捕获异常

[英]catching an exception after doThrow JUNIT

I have a JUnit test where I am testing a method with null arguments. 我有一个JUnit测试,我正在测试一个带有null参数的方法。 If the argument /arguments are null, then I would throw a NullPointerException. 如果参数/参数为null,那么我将抛出NullPointerException。 The method by itself will just throw an IOException. 该方法本身只会抛出IOException。 I am using doThrow on mock object of the class but it seems to me that I am losing the exception in the doThrow() construct and I am unable to catch it. 我在类的模拟对象上使用doThrow,但在我看来,我在doThrow()构造中丢失了异常,我无法捕获它。 Additionally, I strictly do not want to use a try catch in my unit tests. 另外,我严格不想在单元测试中使用try catch。 So I am using @Rules for the same. 所以我正在使用@Rules。 Here is the code snippet: 这是代码片段:

public class TableTest {

@Rule
public ExpectedException exception = ExpectedException.none();
private static Table spyTable;

@Test
public void testCreateTableWithNullTableName_throwsIOEXception() throws IOException {
    final String tableName = null;
    mockConfig = mock(Configuration.class);
    spyPersonTable = spy(new PersonTable());
    doThrow(new IllegalArgumentException()).when(spyPersonTable).createTable(tableName, mockConfig);
    // exception.expect(IllegalArgumentException.class);
}

Using the @rule's exception object, If I use the commented line to catch my exception, the exception created in the doThrow() construct will be lost and I cannot catch it. 使用@ rule的异常对象,如果我使用注释行来捕获我的异常,doThrow()构造中创建的异常将丢失,我无法捕获它。 My unit test will fail and complain that: 我的单元测试将失败并抱怨:

Expected test to throw an instance of java.lang.IllegalArgumentException

The test works fine if I the line is commented as I have it. 如果我对该行进行了评论,那么测试工作正常。 Here is how my method looks like that I am trying to test: 以下是我试图测试的方法:

public void createTable(final String tableName, final Configuration config) throws IOException 

The method needs to throw IOException since the specific exception thrown during table creation is a subclass of IOException. 该方法需要抛出IOException,因为在表创建期间抛出的特定异常是IOException的子类。

Is my catching of exception in the JUnit test wrong for this type of checking exception. 我在JUnit测试中捕获异常对于这种类型的检查异常是错误的吗?

You seem to want to test whether that method throws an exception; 您似乎想测试该方法是否抛出异常; but by using doThrow().when(sut).createTable(...) you prevent the SUT from having its normal behaviour. 但是通过使用doThrow().when(sut).createTable(...)可以防止 SUT出现正常行为。

Just do: 做就是了:

final PersonTable table = new PersonTable();
table.createTable(null, null); // theoretically throws IllegalArgumentException

And just check that there is a thrown exception. 并检查是否存在抛出的异常。 No idea how you do that with your @Rule but I don't use that and here how I'd do that: 不知道你是怎么用你的@Rule做到的,但是我不使用它,在这里我是怎么做到的:

final PersonTable table = new PersonTable();
try {
    table.createTable(null, null);
    fail("No exception thrown!");
} catch (IllegalArgumentException e) {
    assertEquals(e.getMessage(), "the expected message");
}

Your issue is that your test doesn't actually call createTable , and therefore there's nothing to throw the exception. 您的问题是您的测试实际上并没有调用createTable ,因此没有什么可以抛出异常。 You could write this. 可以写这个。

@Test
public void testCreateTableWithNullTableName_throwsIOEXception() throws IOException {
    final String tableName = null;
    mockConfig = mock(Configuration.class);
    spyPersonTable = spy(new PersonTable());
    doThrow(new IllegalArgumentException()).when(spyPersonTable).createTable(tableName, mockConfig);
    exception.expect(IllegalArgumentException.class);
    spyPersonTable.createTable(tableName, mockConfig);
}

But I don't really see the point of doing that, because you'd really just be testing Mockito. 但我真的没有意识到这一点,因为你真的只是在测试Mockito。 You're stubbing the method so that it does nothing other than throwing IllegalArgumentException , then checking that your stubbing worked OK - which seems pointless to me. 你正在对方法进行存根,这样它除了抛出IllegalArgumentException之外什么都不做,然后检查你的存根是否正常 - 这对我来说似乎毫无意义。

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

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