From 5fc64d16625a5672525b6b1ccd019f9f08ac8304 Mon Sep 17 00:00:00 2001 From: Abdulcelil Cercenazi Date: Tue, 31 Jan 2023 02:46:51 +0100 Subject: [PATCH] add JUnit5 code (#217) * add JUnit5 code * modify JUnit5 code --- junit/junit5/junit5/pom.xml | 40 +++++++++ junit/junit5/junit5/src/main/java/Cat.java | 14 ++++ .../junit5/junit5/src/main/java/GoldFish.java | 24 ++++++ .../junit5/junit5/src/test/java/CatTest.java | 71 ++++++++++++++++ .../junit5/junit5/src/test/java/DogTest.java | 82 +++++++++++++++++++ .../junit5/src/test/java/GoldFishTest.java | 51 ++++++++++++ 6 files changed, 282 insertions(+) create mode 100644 junit/junit5/junit5/pom.xml create mode 100644 junit/junit5/junit5/src/main/java/Cat.java create mode 100644 junit/junit5/junit5/src/main/java/GoldFish.java create mode 100644 junit/junit5/junit5/src/test/java/CatTest.java create mode 100644 junit/junit5/junit5/src/test/java/DogTest.java create mode 100644 junit/junit5/junit5/src/test/java/GoldFishTest.java diff --git a/junit/junit5/junit5/pom.xml b/junit/junit5/junit5/pom.xml new file mode 100644 index 0000000..b89c204 --- /dev/null +++ b/junit/junit5/junit5/pom.xml @@ -0,0 +1,40 @@ + + + 4.0.0 + + org.example + junit5 + 1.0-SNAPSHOT + + + 11 + 11 + + + + + org.junit.jupiter + junit-jupiter-api + 5.9.0 + test + + + + org.junit.jupiter + junit-jupiter-params + 5.9.0 + test + + + + org.hamcrest + hamcrest-all + 1.3 + test + + + + + \ No newline at end of file diff --git a/junit/junit5/junit5/src/main/java/Cat.java b/junit/junit5/junit5/src/main/java/Cat.java new file mode 100644 index 0000000..9b96295 --- /dev/null +++ b/junit/junit5/junit5/src/main/java/Cat.java @@ -0,0 +1,14 @@ +public class Cat { + private String name; + + public Cat(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public Cat() { + } +} diff --git a/junit/junit5/junit5/src/main/java/GoldFish.java b/junit/junit5/junit5/src/main/java/GoldFish.java new file mode 100644 index 0000000..ff7c07f --- /dev/null +++ b/junit/junit5/junit5/src/main/java/GoldFish.java @@ -0,0 +1,24 @@ + +public class GoldFish { + private String name; + private int age; + + public GoldFish(String name, int age) { + this.name = name; + this.age = age; + } + public int calculateSpeed() { + if (age == 0){ + throw new RuntimeException("This will fail :(("); + } + return 10 / age; + } + + public String getName() { + return name; + } + + public int getAge() { + return age; + } +} diff --git a/junit/junit5/junit5/src/test/java/CatTest.java b/junit/junit5/junit5/src/test/java/CatTest.java new file mode 100644 index 0000000..d0f4852 --- /dev/null +++ b/junit/junit5/junit5/src/test/java/CatTest.java @@ -0,0 +1,71 @@ +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static java.util.Arrays.asList; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +public class CatTest { + @Test + public void testMeow() { + String catName = "Stilla"; + int catAge = 3; + boolean isNice = false; + + assertThat(catName, equalTo("Stilla")); + assertThat(catAge, lessThan(5)); + assertThat(isNice, is(false)); + } + + @Test + public void testCatInstance() { + Cat cat = new Cat(); + + assertThat(cat, instanceOf(Cat.class)); + } + + @Test + public void testSameCatInstance() { + Cat cat = new Cat(); + + assertThat(cat, sameInstance(cat)); + } + + @Test + public void testCollectionContaining() { + List catNames = asList("Phibi", "Monica", "Stilla"); + + assertThat(catNames, hasItems("Monica", "Phibi")); + assertThat(catNames, not(hasItems("Melih"))); + } + + @Test + public void testCollectionSize() { + List catNames = asList("Phibi", "Monica"); + + assertThat(catNames, hasSize(2)); + } + + @Test + public void testBean() { + Cat cat = new Cat("Mimi"); + + assertThat(cat, hasProperty("name", equalTo("Mimi"))); + } + + @Test + public void testStringEquality() { + String catNameInCaps = "RACHEL"; + + assertThat(catNameInCaps, equalToIgnoringCase("rachel")); + } + + @Test + public void testStringContains() { + String catName = "Joey The Cute"; + + assertThat(catName, containsString("Cute")); + } + +} diff --git a/junit/junit5/junit5/src/test/java/DogTest.java b/junit/junit5/junit5/src/test/java/DogTest.java new file mode 100644 index 0000000..9cdf178 --- /dev/null +++ b/junit/junit5/junit5/src/test/java/DogTest.java @@ -0,0 +1,82 @@ +import org.junit.jupiter.api.*; + +import static org.junit.jupiter.api.Assertions.*; + +public class DogTest { + + @BeforeAll + public static void init() { + System.out.println("Doing stuff"); + } + + @BeforeEach + public void doEach() { + System.out.println("Hey Doggo"); + } + + @AfterAll + public static void finish() { + System.out.println("Finishing stuff"); + } + + @AfterEach + public void doAfterEach() { + System.out.println("Bye Doggo"); + } + + @Test + public void barkFailure() { + String expectedString = "Meow"; + assertEquals(expectedString, "Woof"); + } + + @Disabled("Dog 1 please don't woof") + @Test + public void testBark1() { + String expectedString = "woof1"; + assertEquals(expectedString, "woof1"); + System.out.println("WOOF => 1"); + } + + @Test + public void testBark2() { + String expectedString = "woof2"; + assertEquals(expectedString, "woof2"); + System.out.println("WOOF => 2"); + } + + @Test + public void testNotBark() { + String unexpectedString = ""; + assertNotEquals(unexpectedString, "woof"); + System.out.println("Didn't woof!!"); + } + + @Test + public void nullCheck() { + Object dog = null; + assertNull(dog); + System.out.println("Null dog :("); + } + + @Test + public void nonNullCheck() { + String dog = "Max"; + assertNotNull(dog); + System.out.println("Hey I am " + dog); + } + + @Test + public void trueCheck() { + int dogAge = 2; + assertTrue(dogAge < 5); + System.out.println("I am young :)"); + } + + @Test + public void falseCheck() { + int dogAge = 7; + assertFalse(dogAge < 5); + System.out.println("I am old :("); + } +} diff --git a/junit/junit5/junit5/src/test/java/GoldFishTest.java b/junit/junit5/junit5/src/test/java/GoldFishTest.java new file mode 100644 index 0000000..cdcc0e3 --- /dev/null +++ b/junit/junit5/junit5/src/test/java/GoldFishTest.java @@ -0,0 +1,51 @@ +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import java.util.stream.Stream; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalToIgnoringCase; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +public class GoldFishTest { + @Test + public void testBooleanAssumption() { + GoldFish goldFish = new GoldFish("Windows Jelly", 1); + + assumeTrue(System.getProperty("os.name").contains("Windows")); + assertThat(goldFish.getName(), equalToIgnoringCase("Windows Jelly")); + } + + @Test + public void testBooleanAssert() { + GoldFish goldFish = new GoldFish("Windows Jelly", 1); + + assert(System.getProperty("os.name").contains("Windows")); + assertThat(goldFish.getName(), equalToIgnoringCase("Windows Jelly")); + } + + @Test + public void testException() { + GoldFish goldFish = new GoldFish("Goldy", 0); + + RuntimeException exception = assertThrows(RuntimeException.class, goldFish::calculateSpeed); + + assertThat(exception.getMessage(), equalToIgnoringCase("This will fail :((")); + } + + @ParameterizedTest + @MethodSource("provideFishes") + public void parameterizedTest(GoldFish goldFish) { + assertTrue(goldFish.getAge() >= 1); + } + + private static Stream provideFishes() { + return Stream.of( + Arguments.of(new GoldFish("Browny", 1)), + Arguments.of(new GoldFish("Greeny", 2)) + ); + } +}