committed by
GitHub
parent
179201f367
commit
5fc64d1662
40
junit/junit5/junit5/pom.xml
Normal file
40
junit/junit5/junit5/pom.xml
Normal file
@@ -0,0 +1,40 @@
|
||||
<?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>org.example</groupId>
|
||||
<artifactId>junit5</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>5.9.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-params</artifactId>
|
||||
<version>5.9.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-all</artifactId>
|
||||
<version>1.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
14
junit/junit5/junit5/src/main/java/Cat.java
Normal file
14
junit/junit5/junit5/src/main/java/Cat.java
Normal file
@@ -0,0 +1,14 @@
|
||||
public class Cat {
|
||||
private String name;
|
||||
|
||||
public Cat(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Cat() {
|
||||
}
|
||||
}
|
||||
24
junit/junit5/junit5/src/main/java/GoldFish.java
Normal file
24
junit/junit5/junit5/src/main/java/GoldFish.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
71
junit/junit5/junit5/src/test/java/CatTest.java
Normal file
71
junit/junit5/junit5/src/test/java/CatTest.java
Normal file
@@ -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<String> catNames = asList("Phibi", "Monica", "Stilla");
|
||||
|
||||
assertThat(catNames, hasItems("Monica", "Phibi"));
|
||||
assertThat(catNames, not(hasItems("Melih")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCollectionSize() {
|
||||
List<String> 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"));
|
||||
}
|
||||
|
||||
}
|
||||
82
junit/junit5/junit5/src/test/java/DogTest.java
Normal file
82
junit/junit5/junit5/src/test/java/DogTest.java
Normal file
@@ -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 :(");
|
||||
}
|
||||
}
|
||||
51
junit/junit5/junit5/src/test/java/GoldFishTest.java
Normal file
51
junit/junit5/junit5/src/test/java/GoldFishTest.java
Normal file
@@ -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<Arguments> provideFishes() {
|
||||
return Stream.of(
|
||||
Arguments.of(new GoldFish("Browny", 1)),
|
||||
Arguments.of(new GoldFish("Greeny", 2))
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user