使用Junit5的Mockito.spy

时间:2019-07-18 14:13:36

标签: java junit5 spy

在Junit5 Mockito.dothrow中带有间谍对象,没有给出doThrow中指定的真实异常,而是给出了另一种RealTimeProxy异常

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

public class SampleTest {

    @Test
    public void test(){
        B b = Mockito.mock(B.class);
       A a = new A(b);
       A aSpy = Mockito.spy(a);
       Mockito.doThrow(C.class).when(b).someMethod();
        Assertions.assertThrows(C.class,()->aSpy.testMethod());


    }

    public  class A{
        B b;
        public A(B b){
            this.b = b;
        }

        public void testMethod(){
            b.someMethod();
        }
    }

    public  class B{
        public void someMethod(){

        }
    }
public class C extends RuntimeException {
    }
}

我没有得到类型为C类型的异常,而是得到了另一种异常(org.powermock.api.mockito.internal.mockcreation.RuntimeExceptionProxy),并且测试失败了。

有人能让我知道如何在Junit5中从事间谍活动

0 个答案:

没有答案