├── .gitignore ├── README.md ├── src ├── test │ ├── java │ │ └── com │ │ │ └── techprimers │ │ │ └── testing │ │ │ ├── CucumberTest.java │ │ │ ├── FizzBuzzStepdefs.java │ │ │ └── FizzBuzzTest.java │ └── resources │ │ └── FizzBuzz.feature └── main │ └── java │ └── com │ └── techprimers │ └── testing │ └── FizzBuzz.java ├── Jenkinsfile └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.iml 3 | target -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Fizz Buzz Example in Java 8 using Jenkins Pipeline to publish Cucumber Reports 2 | 3 | ### Fizz Buzz is a game where 4 | - if the number is divisible by 3, you say Fizz 5 | - if the number is divisible by 5, you say Buzz 6 | - if the number is divisible by 3 & 5, you say FizzBuzz 7 | - if neither, you say the number 8 | 9 | ### Pre-requisite 10 | - Add `Cucumber Reports` plugin in Jenkins to use `cucumber` DSL as shown 11 | in the `Jenkinsfile` -------------------------------------------------------------------------------- /src/test/java/com/techprimers/testing/CucumberTest.java: -------------------------------------------------------------------------------- 1 | package com.techprimers.testing; 2 | 3 | import cucumber.api.CucumberOptions; 4 | import cucumber.api.junit.Cucumber; 5 | import org.junit.runner.RunWith; 6 | 7 | @RunWith(Cucumber.class) 8 | @CucumberOptions( 9 | features = "src/test/resources", 10 | glue = "com.techprimers.testing", 11 | format = {"json:target/cucumber.json"} 12 | ) 13 | public class CucumberTest { 14 | } 15 | -------------------------------------------------------------------------------- /src/test/resources/FizzBuzz.feature: -------------------------------------------------------------------------------- 1 | Feature: FizzBuzz Game play 2 | 3 | Scenario: Play FizzBuzz to get Fizz 4 | Given Create a FizzBuzz game play 5 | When I play with number 3 6 | Then The result is "Fizz" 7 | 8 | Scenario: Play FizzBuzz to get Buzz 9 | Given Create a FizzBuzz game play 10 | When I play with number 5 11 | Then The result is "Buzz" 12 | 13 | Scenario: Play FizzBuzz to get FizzBuzz 14 | Given Create a FizzBuzz game play 15 | When I play with number 15 16 | Then The result is "FizzBuzz" -------------------------------------------------------------------------------- /src/main/java/com/techprimers/testing/FizzBuzz.java: -------------------------------------------------------------------------------- 1 | package com.techprimers.testing; 2 | 3 | public class FizzBuzz { 4 | 5 | public String play(int number) { 6 | 7 | if (number == 0) throw new IllegalArgumentException("Number must not be 0"); 8 | 9 | if (isMultipleOf(number, 3) && isMultipleOf(number, 5)) return "FizzBuzz"; 10 | if (isMultipleOf(number, 3)) return "Fizz"; 11 | if (isMultipleOf(number, 5)) return "Buzz"; 12 | 13 | 14 | return String.valueOf(number); 15 | } 16 | 17 | private boolean isMultipleOf(int number, int i) { 18 | return number % i == 0; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Jenkinsfile: -------------------------------------------------------------------------------- 1 | pipeline{ 2 | 3 | agent any 4 | 5 | stages { 6 | 7 | stage ('Compile Stage') { 8 | 9 | steps { 10 | 11 | withMaven(maven: 'maven_3_5_0') { 12 | sh 'mvn clean install' 13 | 14 | } 15 | 16 | } 17 | } 18 | stage ('Test Stage') { 19 | 20 | steps { 21 | 22 | withMaven(maven: 'maven_3_5_0') { 23 | sh 'mvn test' 24 | 25 | } 26 | 27 | } 28 | } 29 | 30 | 31 | stage ('Cucumber Reports') { 32 | 33 | steps { 34 | cucumber buildStatus: "UNSTABLE", 35 | fileIncludePattern: "**/cucumber.json", 36 | jsonReportDirectory: 'target' 37 | 38 | } 39 | 40 | } 41 | 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /src/test/java/com/techprimers/testing/FizzBuzzStepdefs.java: -------------------------------------------------------------------------------- 1 | package com.techprimers.testing; 2 | 3 | import cucumber.api.java.en.Given; 4 | import cucumber.api.java.en.Then; 5 | import cucumber.api.java.en.When; 6 | import org.junit.Assert; 7 | public class FizzBuzzStepdefs { 8 | 9 | FizzBuzz fizzBuzz; 10 | String result; 11 | 12 | @Given("^Create a FizzBuzz game play$") 13 | public void createAFizzBuzzGamePlay() throws Throwable { 14 | fizzBuzz = new FizzBuzz(); 15 | } 16 | 17 | @When("^I play with number (\\d+)$") 18 | public void iPlayWithNumber(int number) throws Throwable { 19 | result = fizzBuzz.play(number); 20 | } 21 | 22 | @Then("^The result is \"([^\"]*)\"$") 23 | public void theResultIs(String resultString) throws Throwable { 24 | Assert.assertEquals(result, resultString); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/test/java/com/techprimers/testing/FizzBuzzTest.java: -------------------------------------------------------------------------------- 1 | package com.techprimers.testing; 2 | 3 | import org.junit.After; 4 | import org.junit.Assert; 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | 8 | public class FizzBuzzTest { 9 | 10 | public FizzBuzz fB; 11 | 12 | @Before 13 | public void setUp() { 14 | fB = new FizzBuzz(); 15 | } 16 | 17 | @Test 18 | public void testNumber() { 19 | String fizzBuzz = fB.play(1); 20 | Assert.assertEquals(fizzBuzz, "1"); 21 | } 22 | 23 | @Test 24 | public void testFizz() { 25 | String fizzBuzz = fB.play(3); 26 | Assert.assertEquals(fizzBuzz, "Fizz"); 27 | } 28 | 29 | @Test 30 | public void testBuzz() { 31 | String fizzBuzz = fB.play(5); 32 | Assert.assertEquals(fizzBuzz, "Buzz"); 33 | } 34 | 35 | @After 36 | public void tearDown() { 37 | fB = null; 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.techprimers.testing 8 | jenkins-example 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | UTF-8 14 | 1.8 15 | 16 | 17 | 18 | 19 | 20 | maven-compiler-plugin 21 | 3.1 22 | 23 | ${java.version} 24 | ${java.version} 25 | 26 | 27 | 28 | maven-surefire-plugin 29 | 2.19 30 | 31 | 32 | junit 33 | junit 34 | 4.12 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | info.cukes 45 | cucumber-java 46 | 1.2.5 47 | 48 | 49 | info.cukes 50 | cucumber-junit 51 | 1.2.5 52 | 53 | 54 | 55 | junit 56 | junit 57 | 4.12 58 | 59 | 60 | 61 | 62 | --------------------------------------------------------------------------------