-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JUnit 5 in Java 8 using Fizz Buzz example
- Loading branch information
TechPrimers
committed
Jun 4, 2017
0 parents
commit d5be8f6
Showing
5 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |