lundi 4 mai 2020

How to input Null and followed by any other string Value in Junit Test Case

I have a scenario in which I am trying to write a test case. In this scenario, I have to input null first and followed by a string value. Basically there are two console inputs in the function. So I need to give null in the first input and string value in 2nd input.

I know how to give both separately but didn't know how I can do it in a single go.

Input using string value

@Test
public void TestAmountWithChangeEqualZero() {
    InputStream sysInBackup = System.in; // backup System.in to restore it later
    ByteArrayInputStream in = new ByteArrayInputStream("TE".getBytes());
    System.setIn(in);


    var result = vendingMachine.captureMoney(2.0);
    assertTrue(result);

    System.setIn(sysInBackup);
}

Input for Null

    @Test
public void TestAmountWithNullAndCancelInput() {


    InputStream  empty = new InputStream() {

        @Override
        public int read() /*throws IOException */{
            // TODO Auto-generated method stub
            return -1;
        }
    };

    System.setIn(empty);
    var result = vendingMachine.captureMoney(2.0);
    assertTrue(result);

    System.setIn(System.in);
}

Can you give me any suggestions? I am new in Java

Aucun commentaire:

Enregistrer un commentaire