From d5be8f6d2112e19627a363eb593331703858cd78 Mon Sep 17 00:00:00 2001 From: TechPrimers Date: Sun, 4 Jun 2017 12:26:32 +0530 Subject: [PATCH] JUnit 5 in Java 8 using Fizz Buzz example --- .gitignore | 0 README.md | 6 ++ pom.xml | 58 +++++++++++++++++++ .../com/techprimers/testing/FizzBuzz.java | 14 +++++ .../com/techprimers/testing/FizzBuzzTest.java | 48 +++++++++++++++ 5 files changed, 126 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 pom.xml create mode 100644 src/main/java/com/techprimers/testing/FizzBuzz.java create mode 100644 src/test/java/com/techprimers/testing/FizzBuzzTest.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/README.md b/README.md new file mode 100644 index 0000000..e76626a --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +## Fizz Buzz Example in Kotlin with Spek Framework for BDD Style tests + +### Fizz Buzz is a game where +- if the number is divisible by 3, you say Fizz +- if the number is divisible by 5, you say Buzz +- if neither, you say the number \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..9a6cf59 --- /dev/null +++ b/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + com.techprimers.testing + junit5-example + 1.0-SNAPSHOT + + + + UTF-8 + 1.8 + 5.0.0-M3 + 1.0.0-M3 + + + + + + maven-compiler-plugin + 3.1 + + ${java.version} + ${java.version} + + + + maven-surefire-plugin + 2.19 + + + org.junit.platform + junit-platform-surefire-provider + 1.0.0-M3 + + + + + + + + + org.junit.jupiter + junit-jupiter-engine + 5.0.0-M3 + test + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + + + + + \ No newline at end of file diff --git a/src/main/java/com/techprimers/testing/FizzBuzz.java b/src/main/java/com/techprimers/testing/FizzBuzz.java new file mode 100644 index 0000000..752c094 --- /dev/null +++ b/src/main/java/com/techprimers/testing/FizzBuzz.java @@ -0,0 +1,14 @@ +package com.techprimers.testing; + +public class FizzBuzz { + + public String play(int number) { + + if (number == 0) throw new IllegalArgumentException("Number must not be 0"); + if (number % 3 == 0) return "Fizz"; + if (number % 5 == 0) return "Buzz"; + + + return String.valueOf(number); + } +} diff --git a/src/test/java/com/techprimers/testing/FizzBuzzTest.java b/src/test/java/com/techprimers/testing/FizzBuzzTest.java new file mode 100644 index 0000000..bfc506b --- /dev/null +++ b/src/test/java/com/techprimers/testing/FizzBuzzTest.java @@ -0,0 +1,48 @@ +package com.techprimers.testing; + +import org.junit.jupiter.api.*; + +class FizzBuzzTest { + + public FizzBuzz fB; + + @BeforeEach + public void setUp() { + fB = new FizzBuzz(); + } + + @DisplayName("Play FizzBuzz with number = 1") + @Test + public void testNumber() { + String fizzBuzz = fB.play(1); + Assertions.assertEquals(fizzBuzz, "1"); + } + + @DisplayName("Play FizzBuzz with number = 3") + @Test + public void testFizz() { + String fizzBuzz = fB.play(3); + Assertions.assertEquals(fizzBuzz, "Fizz"); + } + + @DisplayName("Play FizzBuzz with number = 5") + @Test + public void testBuzz() { + String fizzBuzz = fB.play(5); + Assertions.assertEquals(fizzBuzz, "Buzz"); + } + + @DisplayName("Don't Play FizzBuzz with number = 0") + @Test + public void testZero() { + + Assertions.assertThrows(IllegalArgumentException.class, + () -> fB.play(0)); + } + + @AfterEach + public void tearDown() { + fB = null; + } + +} \ No newline at end of file