Skip to content

Commit

Permalink
JUnit 5 in Java 8 using Fizz Buzz example
Browse files Browse the repository at this point in the history
  • Loading branch information
TechPrimers committed Jun 4, 2017
0 parents commit d5be8f6
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
Empty file added .gitignore
Empty file.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
58 changes: 58 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.techprimers.testing</groupId>
<artifactId>junit5-example</artifactId>
<version>1.0-SNAPSHOT</version>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<junit.jupiter.version>5.0.0-M3</junit.jupiter.version>
<junit.platform.version>1.0.0-M3</junit.platform.version>
</properties>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.0-M3</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.0-M3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
</dependency>

</dependencies>

</project>
14 changes: 14 additions & 0 deletions src/main/java/com/techprimers/testing/FizzBuzz.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
48 changes: 48 additions & 0 deletions src/test/java/com/techprimers/testing/FizzBuzzTest.java
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit d5be8f6

Please sign in to comment.