我是 Java 新手,我一直在努力解決這個問題,其中覆寫率測驗在接受輸入的方法上失敗(通過掃描儀)。奇怪的是,如果我只從 intellij 運行特定的測驗方法,完全相同的測驗就會通過。
我已經能夠在下面這個簡單的例子中在某種程度上復制它 -
InputName.java
import java.util.Scanner;
public class InputName{
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
String name = inputName();
System.out.println("You entered - " name);
}
public static String inputName() throws IllegalArgumentException {
System.out.println("Please enter your name with up to 8 characters: ");
String name = scanner.nextLine();
Integer nameLength = name.length();
if (nameLength > 8){
throw new IllegalArgumentException("Name length is more than 8 characters long");
}
else {
return name;
}
}
}
輸入名稱測驗.java
import org.junit.Test;
import java.io.ByteArrayInputStream;
import static org.junit.Assert.assertEquals;
public class InputNameTest{
@Test
public void testInputName() {
// Scanner scan = new Scanner(System.in);
// InputStream sysInBackup = System.in;
System.setIn(new ByteArrayInputStream("JohnDoe\n".getBytes()));
assertEquals("JohnDoe", InputName.inputName());
// System.setIn(sysInBackup);
}
@Test (expected = IllegalArgumentException.class)
public void testIllegalArgumentException() {
// InputStream sysInBackup = System.in;
System.setIn(new ByteArrayInputStream("JohnnyDoe\n".getBytes()));
InputName.inputName();
// System.setIn(sysInBackup);
}
}
目錄結構-
Check
|-- src
|- main
| |- InputName.java
|- tests
|- InputNameTest.java
測驗使用 Junit4。
在這里,只有第二個測驗可能會失敗。但是在我的實際專案中,當作為覆寫率運行時,這兩個測驗都失敗了(可能是因為該測驗在此方法之前測驗了其他使用 Scanner 的方法)。但是當單獨運行時兩者都會通過。
在覆寫范圍內運行整個測驗時失敗 -
僅運行該測驗方法時通過 -

uj5u.com熱心網友回復:
由于設計不佳,您的 UnitTest 很脆弱。您正在測驗的課程是STUPID code。
“實用程式類”中的方法應該是static. 事實上,它們不應該,特別是如果這些方法不是純函式,而是像您的代碼那樣需要一些依賴項。
該問題的正確解決方案是static從方法中洗掉關鍵字并將 Scanner 類的實體作為建構式引數傳遞,而不是在類InputName本身內部實體化它:
import java.util.Scanner;
public class InputName{
private final Scanner scanner;
InputName(Scanner scanner){
this.scanner = scanner;
}
public static void main(String[] args) {
String name = new InputName(new Scanner(System.in)).inputName();
System.out.println("You entered - " name);
}
public String inputName() throws IllegalArgumentException {
System.out.println("Please enter your name with up to 8 characters: ");
String name = scanner.nextLine();
Integer nameLength = name.length();
if (nameLength > 8){
throw new IllegalArgumentException("Name length is more than 8 characters long");
}
else {
return name;
}
}
}
您的測驗將更改為:
import org.junit.Test;
import java.io.ByteArrayInputStream;
import static org.junit.Assert.assertEquals;
public class InputNameTest{
@Test
public void testInputName() {
assertEquals("JohnDoe",
new InputName(new Scanner(new ByteArrayInputStream(
"JohnDoe\n".getBytes()))).inputName());
}
@Test (expected = IllegalArgumentException.class)
public void testIllegalArgumentException() {
assertEquals("JohnnyDoe",
new InputName(new Scanner(new ByteArrayInputStream(
"JohnnyDoe\n".getBytes()))).inputName());
}
}
沒必要糾結 System.in
當然,使用模擬框架(如 Mockito)來創建類的測驗替身Scanner會更好。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/389710.html
上一篇:類中的模擬背景關系管理器
