私は、スキャナオブジェクトとユーザによるキーボードからの動的入力を使用するメソッドをテストしようとしています。私はそれをテストする方法を理解できませんでした。私が使用したいメソッドは別のメソッドのループでも使用されています。私はMockito
がこの特定の状況で便利になると思います、残念ながら、私はそれをテストする方法を見つけることができませんでした。その特定のメソッドが例外をスローする間違った値を返そうとしている場合さて、コードはより深い説明を提供します(私は願っています)。JAVA JUnitテストスキャナ/ダイナミック入力
/**
* This method asks user to insert cash
* It gets the property text from properties file by key insert.cash
* Checks if coin is in proper format
* Checks if coin exists in available coins array
* Otherwise throws exception
* @throws InvalidCoinException
* @return cash - how much money user inserted
*/
@Override
public double insertCash() throws InvalidCoinException {
double cash = 0;
double temp; // temporary variable which goes through all if statems if all conditions are satisfied it gets assigned to cash variable
boolean coinExists = false;
System.out.println(prop.getProperty("insert.cash"));
if(!sc.hasNextDouble()) {
throw new InvalidCoinException(MessageFormat.format(prop.getProperty("invalid.coin"), sc.next()));
}
else {
temp = sc.nextDouble();
for(int i = 0; i < availableCoins.length; i++) {
if(temp == availableCoins[i] || temp == 0) {
coinExists = true;
}
}
if(coinExists == true) {
cash = temp;
}
else {
throw new InvalidCoinException(MessageFormat.format(prop.getProperty("invalid.coin"), temp));
}
}
return cash;
}
JUNIT:(だけではなく、一つの方法は、スキャナを使用しているため)
@org.junit.Before
public void initializeTest() throws IOException {
machine = new CoffeeMachineImplementation();
machineSpy = Mockito.spy(CoffeeMachineImplementation.class);
}
@org.junit.Test
public void testInsertCash() throws InvalidCoinException {
System.out.println("---------------- Insert cash -----------------");
Double input2 = 0.06;
Double input3 = 200.0;
Double input4 = 0.02;
try {
when(machineSpy.insertCash()).thenReturn(input2)
.thenThrow(new InvalidCoinException("..."));
fail("Should have thrown an exception " + input2);
}
catch (InvalidCoinException e) {
String exception = "Invalid coin: " + input2 + " euro is not existing coin";
System.out.println(e.getMessage());
assertEquals(exception, e.getMessage());
}
が私のスキャナオブジェクトはコンストラクタで宣言されています。
public CoffeeMachineImplementation() throws IOException {
prop = new Properties();
input = new FileInputStream("src/localization.properties");
maWaterCap = 5;
maCoffeeCap = 3;
prop.load(input);
sc = new Scanner(System.in);
}
私はgettin nullポインタ例外です – user2204367
私はそのコンストラクタでロードされているプロパティについて疑問に思っています..私の更新をチェックしてください –
よく修正しました。とにかく助けてくれてありがとう。ほとんどそれは、その方法について多くの助けとなったスキャナセッター注入についてでした:) – user2204367