JUnit 5 in Java 8 using Fizz Buzz example
This commit is contained in:
0
.gitignore
vendored
Normal file
0
.gitignore
vendored
Normal file
6
README.md
Normal file
6
README.md
Normal file
@@ -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
pom.xml
Normal file
58
pom.xml
Normal file
@@ -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
src/main/java/com/techprimers/testing/FizzBuzz.java
Normal file
14
src/main/java/com/techprimers/testing/FizzBuzz.java
Normal file
@@ -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
src/test/java/com/techprimers/testing/FizzBuzzTest.java
Normal file
48
src/test/java/com/techprimers/testing/FizzBuzzTest.java
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user