简体   繁体   English

捕获块的 Junit 测试在捕获后不抛出任何异常

[英]Junit test for catch block not throwing any exception after catching

I have to write Junit test for the catch block.我必须为 catch 块编写 Junit 测试。 But I am not able to identify what should I assert here.但我无法确定我应该在这里断言什么。 Since the func() is only catching the exception and not throwing anything I cannot assert using Assertions.assertThatExceptionOfType().由于 func() 仅捕获异常而不抛出任何我无法使用 Assertions.assertThatExceptionOfType() 断言的内容。 I am new to Junit testing so cannot think of anything else.我是 Junit 测试的新手,所以想不出别的了。 Any possible way to test the type of exception received by catch block.任何可能的方法来测试 catch 块接收到的异常类型。

Method方法

public void func() {
    try {
        int x = solve();
    } catch(Exception1 e) {
        log.warn("error", e);
    } catch(Exception2 e) {
        log.warn("error", e);
    }
}

private int solve() throws ExceptionName {
    //do something...
    throws new Exception("error occured");
    ...
}

You can change the visibility of solve() method and test it with all exception cases.您可以更改solve()方法的可见性并在所有异常情况下对其进行测试。 For example change it to default例如将其更改为默认值

int solve() throws ExceptionName {

Put tests in the same package as class with this method so that it could be access from test.使用此方法将测试与类放在同一个包中,以便它可以从测试访问。

UPDATE更新

The best way would be to change the code to be more testable as it was shown above.最好的方法是将代码更改为更易于测试,如上所示。 In order to not change the code you can use the way from this answer .为了不更改代码,您可以使用此答案中的方式。 It can be tricky.这可能很棘手。 With Mockito and PowerMockito you can control when Exception1 or Exception2 are creating.使用 Mockito 和 PowerMockito,您可以控制何时创建Exception1Exception2 Based on this you will know which catch statement was executed.基于此,您将知道执行了哪个 catch 语句。

In test code it could be like this:在测试代​​码中,它可能是这样的:

@RunWith(PowerMockRunner.class)
@PrepareForTest({ Exception1.class, Exception2.class, MyClass.class })
public class TestClass {

    @Before
    public void setup() {
        Exception1 cutMock = Mockito.mock(Exception1.class);
        PowerMockito.whenNew(Exception1.class)
                .withArguments(Matchers.anyString())
                .thenReturn(cutMock);
    }

    @Test
    public void testMethod() {
        // prepare
        MyClasss myClass = new MyClass();

        // execute
        myClass.func();

        // checks if the constructor has been called once and with the expected argument values:
        String value = "abc";
        PowerMockito.verifyNew(Exception1.class).withArguments(value);
    }
}

I have to write Junit test for the catch block.我必须为catch块编写Junit测试。 But I am not able to identify what should I assert here.但是我无法确定我在这里应该断言什么。 Since the func() is only catching the exception and not throwing anything I cannot assert using Assertions.assertThatExceptionOfType().由于func()仅捕获异常而不抛出任何内容,因此我无法使用Assertions.assertThatExceptionOfType()进行断言。 I am new to Junit testing so cannot think of anything else.我是Junit测试的新手,因此无法想到其他任何东西。 Any possible way to test the type of exception received by catch block.测试catch块接收到的异常类型的任何可能方法。

Method方法

public void func() {
    try {
        int x = solve();
    } catch(Exception1 e) {
        log.warn("error", e);
    } catch(Exception2 e) {
        log.warn("error", e);
    }
}

private int solve() throws ExceptionName {
    //do something...
    throws new Exception("error occured");
    ...
}

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

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