Fizz Buzz Cucumber Java

292 Views Asked by At

I'm having a problem here - I'm working on Fizz-Buzz-None. If divisible by 3 - Fizz, by 5 - Buzz, by 3 and 5 - FizzBuzz, any other case - None. I'm getting an error in Terminal of IntliiJ says "Values should be different. Actual: None". Please tell me what did I do wrong :) Here's my code;

public class FizzBuzzNoneChecker {

    public String checkIfFizz(int number) {
        return "Fizz";
    }

    public String checkBuzz(int number) {
        return "Buzz";
    }

    public String checkFizzBuzz(int number) {
        return "FizzBuzz";
    }

    public String checkNone (int number) {
        return "None";
    }
}




import io.cucumber.java8.En;
import org.junit.Assert;

public class IsItFizzBuzzNoneSteps implements En{
        private String answer;
        private int number;

    public IsItFizzBuzzNoneSteps () {
        Given("Given number is 3", () -> {

            // Write code here that turns the phrase above into concrete actions
            this.number = 3;

        });

        Given("Given number is 6", () -> {

            // Write code here that turns the phrase above into concrete actions

            this.number = 6;

        });

        Given("Given number is 5", () -> {

            // Write code here that turns the phrase above into concrete actions

            this.number = 5;

        });

        Given("Given number is 10", () -> {

            // Write code here that turns the phrase above into concrete actions

            this.number = 10;

        });

        Given("Given number is 15", () -> {

            // Write code here that turns the phrase above into concrete actions

            this.number = 15;

        });

        Given("Given number is 30", () -> {

            // Write code here that turns the phrase above into concrete actions

            this.number = 30;

        });

        Given("Given number is 1", () -> {

            // Write code here that turns the phrase above into concrete actions

            this.number = 1;

        });



        Given("Given number is 91", () -> {

            // Write code here that turns the phrase above into concrete actions

            this.number = 91;
        });



        When("I ask about the password for the given number", () -> {

            // Write code here that turns the phrase above into concrete actions
            FizzBuzzNoneChecker fizzBuzzNoneChecker = new FizzBuzzNoneChecker();
            this.answer = fizzBuzzNoneChecker.checkIfFizz(this.number);
            this.answer = fizzBuzzNoneChecker.checkBuzz(this.number);
            this.answer = fizzBuzzNoneChecker.checkFizzBuzz(this.number);
            this.answer = fizzBuzzNoneChecker.checkNone(this.number);

        });

        Then("I should be told {string}", (String string) -> {

           
            Assert.assertNotEquals(string, this.answer);
         


        });

    }
}



Feature: This is a game "Fizz-Buzz-None"
  if a number is divisible by 5 & 3 - say "FizzBuzz", if by 3 - say "Fizz", if by 5 - say "Buzz"
  in another case - say "None"

  Scenario Outline: Answer the right word as a reaction to a given number
    Given Given number is <number>
    When I ask about the password for the given number
    Then I should be told <answer>
    Examples:
      | number | answer     |
      | 3      | "Fizz"     |
      | 6      | "Fizz"     |
      | 5      | "Buzz"     |
      | 10     | "Buzz"     |
      | 15     | "FizzBuzz" |
      | 30     | "FizzBuzz" |
      | 1      | "None"     |
      | 91     | "None"     |

So when I put Assert.assertEquals(string, this.answer); none of test's passing, with AssertNotEquals - 6 of them and 2 are as failed.

1

There are 1 best solutions below

3
On
  1. Your FizzBuzzNoneChecker methods are not actually checking anything. Each method always returns its fixed string regardless of which number you provide. You need some logic in here to check if the number is divisible by 3 or 5 to determine what string to return, e.g.
public String checkIfFizz(int number) {
  if (number % 3 == 0) {
    return "Fizz";
  } else {
    return "";
  }
}

I would actually do this in a ternary operation, which is much shorter, but I have no idea if you have encountered those yet.

  1. As your code stands, this.answer is always going to end up being the result of checkNone(). You need to combine your four check methods into one, using a sequence of if() statements to figure out which string to return. Take my code from point 1 as your start, and expand the logic until it covers the four possible situations in the right order to give the right response.

  2. For extracting the numbers from the "Given... " inputs, since the input will apparently always follow that format, take a look at substrings in https://docs.oracle.com/javase/8/docs/api/index.html.

  3. Will your inputs only ever be one of those values, or do you need to consider the possibility that the user might want to know about 42?

  4. Writing test code is fantastic. Showing us your test code would be even better, it gives us more information to be able to help you :)

Look into the above points and let me know how you get on. Feel free to ask more questions if you need to!