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(); }); }