Home > Software engineering >  Is there a way to test with JUnit this project?
Is there a way to test with JUnit this project?

Time:04-19

I created this rock scissors paper project

public class Rocksciccorspaper {
    private static final int R = 1;
    private static final int S = 2;
    private static final int P = 3;
    private int rounds;

    public Rocksciccorspaper(int rounds) {
        this.rounds = rounds;
    }

    private String winner(int userSelect, int botSelect) {
        if (userSelect == botSelect) {
            return "draw";
        } else if (botSelect - userSelect == 1 || botSelect - userSelect == -2) {
            return "Congrats, you won.";
        } else {
            return "You lose.";
        }
    }

    public void start() {
        Scanner sc = new Scanner(System.in);
        int playedRounds = 0;
        while (playedRounds < rounds) {
            System.out.print("choose Rock(1), Sciccors(2), Paper(3): ");
            int userSelect = sc.nextInt();
            int botSelect = new Random().nextInt(3)   1;
            System.out.println("You Choose: "
                      selectionDetect(userSelect));
            System.out.println("Bot Choose: "
                      selectionDetect(botSelect));
            System.out.println(winner(userSelect, botSelect));
            System.out.println();
            playedRounds  ;
        }
        sc.close();
    }

    private String selectionDetect(int selection) {
        if (selection == R) {
            return "Rock (1)";
        } else if (selection == S) {
            return "Sciccors (2)";
        } else if (selection == P) {
            return "Paper (3)";
        } else {
            return "Invalid Selection ("   selection   ")";
        }
    }

    public static void main(String[] args) {
        Rocksciccorspaper rocksciccorsPaper = new Rocksciccorspaper(3);
        rocksciccorsPaper.start();
    }

}

I want to test it. The first function which I want to test is the winner function. I think that's one of the important functions.

I tried something but it doesn't work well. I'm not sure how to test the selection in my if condition.

import static org.junit.Assert.*;

import org.junit.Test;

public class RocksciccorspaperTest {
    @Test
    public void test() {
        fail("Not yet implemented");
    }
}

CodePudding user response:

If it is difficult to think of a test, then a Test First approach is often useful. Put your existing code aside. Write a test. Write enough code to satisfy that test (perhaps copy and paste your old code; perhaps not). Refactor if you wish. Repeat until done.

Test First aside:

The method winner is probably going to be your first target to test. It needs to be made accessible to the test. In this case that is easy to do by making it static and public and moving it to a separate class. Including UI text makes that test fragile (say, you decide to capitalise "draw"), so consider using an enum instead of String.

The actual code for testing winner will be something like:

import static mypackage.Decision.*;
// ...
    assertEquals("draw", winner(R, R));

There's only nine possible combinations, so exhaustive testing is trvial.

As well as UI, global state makes testing (and everything else) hacky. Consider replacing System.in and System.out with parameters. The globals themselves only need to be referenced right up in main.

  • Related