Jnit Mock & Assert

java - Mockito: When any instance invokes method, thenReturn something - Stack Overflow

 

 

Mock一个void 返回值类型的方法

//代码文件
public class Demo {
    public void voidMethod(String param) {
        System.out.println(param);
    }
}
//测试文件
@Test
void voidMethodTest(){
    Demo demo = mock(Demo.class);
    doNothing().when(demo).voidMethod("test");
}

  

  

AssertThrow Exception

//代码文件
public class Demo {
    public void assertMethod() throws Exception {
        throw new Exception("");
    }
}


   //测试文件
    @Test
    void assertMethodTest(){
        Demo demo =new Demo();
        assertThrows(Exception.class,()->{
            demo.assertMethod();
        });
    }

  

 

posted @ 2022-03-02 18:11  今天起个早  阅读(87)  评论(0编辑  收藏  举报