public class mock { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入一个整数:"); int num = scanner.nextInt(); System.out.println("你输入的整数是:" + num); } }生成单元测试
时间: 2023-05-28 11:03:37 浏览: 54
@Test
public void testMain() {
ByteArrayInputStream in = new ByteArrayInputStream("10".getBytes());
System.setIn(in);
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
mock.main(null);
assertEquals("请输入一个整数:你输入的整数是:10\n", out.toString());
}
相关问题
public class mock { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入一个整数:"); int num = scanner.nextInt(); System.out.println("你输入的整数是:" + num); } }
该程序有语法错误,缺少加号连接字符串。
正确代码:
public class mock {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个整数:");
int num = scanner.nextInt();
System.out.println("你输入的整数是:" + num);
}
}
mockito.mockstatic 返回为void方法
Mockito.mockStatic 返回为 void 方法时,可以使用 Mockito.doNothing() 来模拟该方法的行为。具体来说,可以使用以下代码:
Mockito.mockStatic(YourClass.class);
Mockito.doNothing().when(YourClass::yourVoidMethod);
其中,YourClass 是包含 void 方法的类名,yourVoidMethod 是该类中的 void 方法名。这样,当调用该方法时,就会执行 doNothing() 方法,即不做任何操作。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)