diff --git a/README.md b/README.md new file mode 100644 index 0000000..4b18f79 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# This repro is part of the Serenity Mentoring Programme that I am going through. This programme is run by John Smart https://johnfergusonsmart.com/serenity-bdd-mentoring/. His programme aims to turn manual testers into world class automation engineers. Using Java, Serenity, BDD, Cucumber and Gerkin diff --git a/pom.xml b/pom.xml index 8171c1a..acbb56a 100644 --- a/pom.xml +++ b/pom.xml @@ -24,6 +24,21 @@ 4.13 test + + net.serenity-bdd + serenity-core + 2.2.9 + + + net.serenity-bdd + serenity-junit + 2.2.9 + + + org.assertj + assertj-core + 3.16.1 + @@ -70,5 +85,16 @@ + + + org.apache.maven.plugins + maven-compiler-plugin + + 14 + 14 + --enable-preview + + + diff --git a/src/main/java/com/serenitydojo/Cat.java b/src/main/java/com/serenitydojo/Cat.java new file mode 100644 index 0000000..84af9eb --- /dev/null +++ b/src/main/java/com/serenitydojo/Cat.java @@ -0,0 +1,68 @@ +package com.serenitydojo; + +/** + * A feline creature. + */ +public class Cat extends Pet { + + private String favoriteFood; + private int age; + + // A very useful field + public static final String CAT_NOISE = "Meow"; + + public static String usualFood() { + return "Tuna"; + } + + public Cat(String name, int age) { + super(name); + this.age = age; + this.favoriteFood = usualFood(); + } + + public Cat(String name, String favoriteFood, int age) { + super(name); + this.favoriteFood = favoriteFood; + this.age = age; + } + + public void setFavoriteFood(String favoriteFood) { + this.favoriteFood = favoriteFood; + } + + public String getFavoriteFood() { + return favoriteFood; + } + + public int getAge() { + return age; + } + + @Override + public String makeNoise() { + return CAT_NOISE; + } + + @Override + public String play() { + return "plays with string"; + } + + public void feed(String food) { + System.out.println(getName() + " eats some " + food); + } + + public void groom() { + lickPaws(); + cleanFur(); + } + + private void cleanFur() { + System.out.println(getName() + " cleans his fur"); + } + + private void lickPaws() { + System.out.println(getName() + " licks his paws"); + } +} diff --git a/src/main/java/com/serenitydojo/Dog.java b/src/main/java/com/serenitydojo/Dog.java new file mode 100644 index 0000000..a12fc6f --- /dev/null +++ b/src/main/java/com/serenitydojo/Dog.java @@ -0,0 +1,29 @@ +package com.serenitydojo; + +public class Dog extends Pet { + private String favoriteToy; + private int age; + + public Dog(String name, String favoriteToy, int age) { + super(name); + this.favoriteToy = favoriteToy; + this.age = age; + } + + @Override + public String play() { + return "plays with bone"; + } + + public String getFavoriteToy() { + return favoriteToy; + } + + public int getAge() { + return age; + } + + public String makeNoise() { + return "Woof"; + } +} diff --git a/src/main/java/com/serenitydojo/Hampster.java b/src/main/java/com/serenitydojo/Hampster.java new file mode 100644 index 0000000..445d15a --- /dev/null +++ b/src/main/java/com/serenitydojo/Hampster.java @@ -0,0 +1,30 @@ +package com.serenitydojo; + +public class Hampster extends Pet { + private String favoriteGame; + private int age; + + public Hampster(String name, String favoriteGame, int age) { + super(name); + this.favoriteGame = favoriteGame; + this.age = age; + } + + public String getFavoriteGame() { + return favoriteGame; + } + + public int getAge() { + return age; + } + + @Override + public String play() { + return "runs in wheel"; + } + + @Override + public String makeNoise() { + return "Squeak"; + } +} diff --git a/src/main/java/com/serenitydojo/HelloWorldWriter.java b/src/main/java/com/serenitydojo/HelloWorldWriter.java new file mode 100644 index 0000000..f026f83 --- /dev/null +++ b/src/main/java/com/serenitydojo/HelloWorldWriter.java @@ -0,0 +1,8 @@ +package com.serenitydojo; + +public class HelloWorldWriter { + + public void writeHelloWorld(){ + System.out.println("Hello World"); + } +} diff --git a/src/main/java/com/serenitydojo/Pet.java b/src/main/java/com/serenitydojo/Pet.java new file mode 100644 index 0000000..eb6c450 --- /dev/null +++ b/src/main/java/com/serenitydojo/Pet.java @@ -0,0 +1,18 @@ +package com.serenitydojo; + +public abstract class Pet { + private String name; + private int age; + + + public Pet(String name) { + this.name = name; + } + public String getName() { + return name; + } + + public String goForWalks() { return "walk walk walk"; } + public abstract String makeNoise(); + public abstract String play(); +} diff --git a/src/main/java/com/serenitydojo/exceptions/FileLoader.java b/src/main/java/com/serenitydojo/exceptions/FileLoader.java new file mode 100644 index 0000000..98f7b84 --- /dev/null +++ b/src/main/java/com/serenitydojo/exceptions/FileLoader.java @@ -0,0 +1,19 @@ +package com.serenitydojo.exceptions; + +import java.io.IOException; + +public class FileLoader { + public String readHelloWorld() throws IOException { + return "";//Files.readString(Paths.get("src/main/resources/hello.txt")); + } + + public Boolean fileContainsText(String filename, String expectedText) { + String path = "src/main/resources/" + filename; + return null;// (Files.readString(Paths.get(path)).contains(expectedText)); + } + + public Boolean fileHasText(String filename, String expectedText) { + String path = "src/main/resources/" + filename; + return null;// (Files.readString(Paths.get(path)).contains(expectedText)); + } +} diff --git a/src/main/java/com/serenitydojo/exceptions/MissingWelcomeFileException.java b/src/main/java/com/serenitydojo/exceptions/MissingWelcomeFileException.java new file mode 100644 index 0000000..883700b --- /dev/null +++ b/src/main/java/com/serenitydojo/exceptions/MissingWelcomeFileException.java @@ -0,0 +1,7 @@ +package com.serenitydojo.exceptions; + +public class MissingWelcomeFileException extends RuntimeException { + public MissingWelcomeFileException(String message, Throwable e) { + super(message, e); + } +} diff --git a/src/main/java/com/serenitydojo/exceptions/StringProcessor.java b/src/main/java/com/serenitydojo/exceptions/StringProcessor.java new file mode 100644 index 0000000..13e702a --- /dev/null +++ b/src/main/java/com/serenitydojo/exceptions/StringProcessor.java @@ -0,0 +1,26 @@ +package com.serenitydojo.exceptions; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URL; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class StringProcessor { + + public String showLengthOf(String input) { + int length = (input == null) ? 0 : input.length(); + return input + " has a length of " + length; + } + + + public int getPortOf(String urlAsAString) { + try { + URL url = new URL(urlAsAString); + return url.getDefaultPort(); + } catch(MalformedURLException badlyWrittenException) { + throw new TestEnvironmentUnavailableException(); + } + } +} diff --git a/src/main/java/com/serenitydojo/exceptions/TestEnvironmentUnavailableException.java b/src/main/java/com/serenitydojo/exceptions/TestEnvironmentUnavailableException.java new file mode 100644 index 0000000..567710c --- /dev/null +++ b/src/main/java/com/serenitydojo/exceptions/TestEnvironmentUnavailableException.java @@ -0,0 +1,5 @@ +package com.serenitydojo.exceptions; + +public class TestEnvironmentUnavailableException extends RuntimeException { + +} diff --git a/src/main/java/com/serenitydojo/model/AnimalType.java b/src/main/java/com/serenitydojo/model/AnimalType.java new file mode 100644 index 0000000..792bfc3 --- /dev/null +++ b/src/main/java/com/serenitydojo/model/AnimalType.java @@ -0,0 +1,5 @@ +package com.serenitydojo.model; + +public enum AnimalType { + CAT, DOG, HAMSTER, FISH, LLAMA +} diff --git a/src/main/java/com/serenitydojo/model/Feeder.java b/src/main/java/com/serenitydojo/model/Feeder.java new file mode 100644 index 0000000..d037ff4 --- /dev/null +++ b/src/main/java/com/serenitydojo/model/Feeder.java @@ -0,0 +1,17 @@ +package com.serenitydojo.model; + +public class Feeder { + public FoodType feeds(AnimalType animal, boolean isPremium) { + + switch (animal) { + case CAT: + return (isPremium) ? FoodType.SALMON : FoodType.TUNA; + case DOG: + return (isPremium) ? FoodType.DELUXE_DOG_FOOD : FoodType.DOG_FOOD; + case HAMSTER: + return (isPremium) ? FoodType.LETTUCE : FoodType.CABBAGE; + default: + return FoodType.UNKNOWN; + } + } +} diff --git a/src/main/java/com/serenitydojo/model/FoodType.java b/src/main/java/com/serenitydojo/model/FoodType.java new file mode 100644 index 0000000..c1e4501 --- /dev/null +++ b/src/main/java/com/serenitydojo/model/FoodType.java @@ -0,0 +1,5 @@ +package com.serenitydojo.model; + +public enum FoodType { + TUNA, CABBAGE, LETTUCE, SALMON, DOG_FOOD, DELUXE_DOG_FOOD, UNKNOWN +} diff --git a/src/main/resources/hello.txt b/src/main/resources/hello.txt new file mode 100644 index 0000000..5e1c309 --- /dev/null +++ b/src/main/resources/hello.txt @@ -0,0 +1 @@ +Hello World \ No newline at end of file diff --git a/src/test/java/com/serenitydojo/HelloWorldWriterTest.java b/src/test/java/com/serenitydojo/HelloWorldWriterTest.java new file mode 100644 index 0000000..b9f0167 --- /dev/null +++ b/src/test/java/com/serenitydojo/HelloWorldWriterTest.java @@ -0,0 +1,12 @@ +package com.serenitydojo; + +import org.junit.Test; + +public class HelloWorldWriterTest { + @Test + public void shouldWriteHelloWorldToTheConsole(){ + HelloWorldWriter writer = new HelloWorldWriter(); + writer.writeHelloWorld(); + + } +} diff --git a/src/test/java/com/serenitydojo/exceptions/ExceptionHandlingExercises.java b/src/test/java/com/serenitydojo/exceptions/ExceptionHandlingExercises.java new file mode 100644 index 0000000..8724a1f --- /dev/null +++ b/src/test/java/com/serenitydojo/exceptions/ExceptionHandlingExercises.java @@ -0,0 +1,55 @@ +package com.serenitydojo.exceptions; + +import org.junit.Test; + +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ExceptionHandlingExercises { + + // - Handle a declared exception + // - Catch a declared exception and return a value + // - Catch a declared exception and throw a custom exception + // - Custom logic that throws a custom exception when no result is found + // - Using the Finally block to clean up + + /** + * Exercise 1 - Handling exceptions + * Uncomment the code in this test and make it work. + * You will need to modify the FileLoader class so that it correctly handles an IOException + */ + @Test + public void workingWithDeclaredExceptions() throws IOException { + FileLoader fileLoader = new FileLoader(); + assertThat(fileLoader.readHelloWorld()).isEqualTo("Hello World"); + } + + /** + * Exercise 2 - Catching exceptions + * Update the fileContainsText() method in the FileLoader class so that it returns false if the file + * does not contain the excepted text, or if the file does not exist. + */ + @Test + public void catchingExceptions() { + FileLoader fileLoader = new FileLoader(); + assertThat(fileLoader.fileContainsText("hello.txt","Hello World")).isTrue(); + } + + @Test + public void catchingExceptionsWhenTheFileDoesNotExist() { + FileLoader fileLoader = new FileLoader(); + assertThat(fileLoader.fileContainsText("does-not-exist.txt","Hello World")).isFalse(); + } + + /** + * Exercise 3 - Throwing custom exceptions + * Create a custom runtime exception called MissingWelcomeFileException, + * and update the fileHasText() method to throw this exception if no matching file is found. + */ + @Test(expected = MissingWelcomeFileException.class) + public void catchingCustomExceptionsWhenTheFileDoesNotExist() { + FileLoader fileLoader = new FileLoader(); + assertThat(fileLoader.fileHasText("does-not-exist.txt","Hello World")).isFalse(); + } +} diff --git a/src/test/java/com/serenitydojo/exceptions/WhenWorkingWithExceptions.java b/src/test/java/com/serenitydojo/exceptions/WhenWorkingWithExceptions.java new file mode 100644 index 0000000..4395705 --- /dev/null +++ b/src/test/java/com/serenitydojo/exceptions/WhenWorkingWithExceptions.java @@ -0,0 +1,34 @@ +package com.serenitydojo.exceptions; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class WhenWorkingWithExceptions { + + @Test + public void shouldShowTheLengthOfAString() { + StringProcessor stringProcessor = new StringProcessor(); + + String result = stringProcessor.showLengthOf("some string"); + + assertThat(result).isEqualTo("some string has a length of 11"); + } + + @Test + public void shouldShowZeroForNullStrings() { + + StringProcessor stringProcessor = new StringProcessor(); + + String result = stringProcessor.showLengthOf(null); + + assertThat(result).isEqualTo("null has a length of 0"); + } + + @Test(expected = TestEnvironmentUnavailableException.class) + public void shouldFindThePort() { + StringProcessor stringProcessor = new StringProcessor(); + + stringProcessor.getPortOf("A:https://www.google.com"); + } +} diff --git a/src/test/java/com/serenitydojo/readme.md b/src/test/java/com/serenitydojo/readme.md deleted file mode 100644 index 44f08e7..0000000 --- a/src/test/java/com/serenitydojo/readme.md +++ /dev/null @@ -1 +0,0 @@ -Unit tests go here \ No newline at end of file