Back to home

Gradle setupSetting up the Testssrc/main/java/FizzBuzz.javaRunning tests
Java Fizzbuzz main image

Java Fizzbuzz

Gradle setup

For our build.gradle file:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 apply plugin: "java" apply plugin: "eclipse" apply plugin: "idea" repositories { mavenCentral() } dependencies { testCompile "junit:junit:4.12" } test { testLogging { exceptionFormat = 'full' events = ["passed", "failed", "skipped"] } }

Setting up the Tests

Create file src/test/java/FizzBuzzTest.java:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 import org.junit.Ignore; import org.junit.Test; import static org.junit.Assert.assertEquals; public class FizzBuzzTest { @Test public void testReturnsIntAsString() { assertEquals("2", new FizzBuzz().run(2)); } // @Ignore("Remove to run test") @Test public void testReturnsFizz() { assertEquals("Fizz", new FizzBuzz().run(3)); } @Test public void testReturnsBuzz() { assertEquals("Buzz", new FizzBuzz().run(5)); } @Test public void testReturnsFizzBuzz() { assertEquals("FizzBuzz", new FizzBuzz().run(15)); } }

src/main/java/FizzBuzz.java

For our main Java file running FizzBuzz:

1 2 3 4 5 6 7 8 9 10 11 12 13 class FizzBuzz { String run(Integer input) { if (input % 15 == 0) { return "FizzBuzz"; } else if (input % 3 == 0) { return "Fizz"; } else if (input % 5 == 0) { return "Buzz"; } else { return Integer.toString(input); } } }

Running tests

Run gradle test to compile and test our FizzBuzz class.

Dennis O'Keeffe

@dennisokeeffe92
  • Melbourne, Australia

Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
I am currently working on workingoutloud.dev, Den Dribbles and LandPad .

Related articles


1,200+ PEOPLE ALREADY JOINED ❤️️

Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.