mercredi 7 avril 2021

Spoof user input via dataflowinout stream for multiple cases

In an assignment I am being asked to provide a test case for my MVC which test both the controller and the view class functionality. Apart from the view class for a test case I created a testView class which extended the view class. Using the same methods along with helper methods I am trying to achieve spoofing user input at different points. An example of this spoofing may be that it works as print prompt, take input, print print, take input. Both the print prompt and the take input are methods which are defined to work as the only interaction with the user in the view class but when creating test cases I can't seem to inject the autonomous code I want to test my class functionality.

public class TestView extends View {
    private Controller newGame;
    private int lineIncrement = 0;

    @Override
    public void sysOutput(String prompt) {
        System.out.println(prompt);
    }

    @Override
    public void showCard(String suit, String value) {
        System.out.println("Card: " + suit + value);
    }

    /**
     *
     * @return sends the input from the user back to the controller for processing
     */
    @Override
    public String sysInput() {
        Scanner myObj = new Scanner(System.in);  // Create a Scanner object
        String input = myObj.nextLine();  // Read user input
        return input;
    }

    //used to get stream of input from the test cases
    public void userInputHelper(String testInput){
        ByteArrayInputStream in = new ByteArrayInputStream(testInput.getBytes());
        System.setIn(in);
        lineIncrement++;
    }

Above is the helper method and user input retrieval method, below is the usage of these methods in a test case

    /**
     *  test the ability to interact with the getting the chip amount
     */
    public void testGetChips() {
        String[] actions = {"500", "50"};
        userInputHelper(actions[0]);
        userInputHelper(actions[1]);
        new Controller(this);
        newGame.
        assertEquals("500", newGame.getPlayer().getAllocateBoughtChips());
        lineIncrement = 0;
    }

Aucun commentaire:

Enregistrer un commentaire