Merge pull request #11413 from Trixi-Turny/BAEL-5209-assert-type-of-object
BAEL-5209 example with JUnit5, Hamcrest and AssertJ
This commit is contained in:
commit
ffacd9b9ae
2
pom.xml
2
pom.xml
|
@ -1315,6 +1315,7 @@
|
||||||
<module>persistence-modules/sirix</module>
|
<module>persistence-modules/sirix</module>
|
||||||
<module>quarkus-vs-springboot</module>
|
<module>quarkus-vs-springboot</module>
|
||||||
<module>spring-boot-modules/spring-boot-cassandre</module>
|
<module>spring-boot-modules/spring-boot-cassandre</module>
|
||||||
|
<module>testing-modules/testing-assertions</module>
|
||||||
</modules>
|
</modules>
|
||||||
</profile>
|
</profile>
|
||||||
|
|
||||||
|
@ -1367,6 +1368,7 @@
|
||||||
<module>persistence-modules/sirix</module>
|
<module>persistence-modules/sirix</module>
|
||||||
<module>quarkus-vs-springboot</module>
|
<module>quarkus-vs-springboot</module>
|
||||||
<module>spring-boot-modules/spring-boot-cassandre</module>
|
<module>spring-boot-modules/spring-boot-cassandre</module>
|
||||||
|
<module>testing-modules/testing-assertions</module>
|
||||||
</modules>
|
</modules>
|
||||||
</profile>
|
</profile>
|
||||||
</profiles>
|
</profiles>
|
||||||
|
|
|
@ -42,7 +42,6 @@
|
||||||
<module>spring-testing-2</module>
|
<module>spring-testing-2</module>
|
||||||
<module>spring-testing</module>
|
<module>spring-testing</module>
|
||||||
<module>test-containers</module>
|
<module>test-containers</module>
|
||||||
<module>testing-assertions</module>
|
|
||||||
<module>testing-libraries-2</module>
|
<module>testing-libraries-2</module>
|
||||||
<module>testing-libraries</module>
|
<module>testing-libraries</module>
|
||||||
<module>testng</module>
|
<module>testng</module>
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.baeldung.object.type;
|
||||||
|
|
||||||
|
public class Deciduous implements Tree {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Deciduous(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEvergreen() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.baeldung.object.type;
|
||||||
|
|
||||||
|
public class Evergreen implements Tree {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Evergreen(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEvergreen() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
package com.baeldung.object.type;
|
||||||
|
|
||||||
|
public interface Tree {
|
||||||
|
|
||||||
|
public boolean isEvergreen();
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.baeldung.object.type;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class TreeSorter {
|
||||||
|
protected Tree sortTree(String name) {
|
||||||
|
|
||||||
|
final List<String> deciduous = List.of("Beech", "Birch", "Ash", "Whitebeam", "Hornbeam", "Hazel & Willow");
|
||||||
|
final List<String> evergreen = List.of("Cedar", "Holly", "Laurel", "Olive", "Pine");
|
||||||
|
|
||||||
|
if (deciduous.contains(name)) {
|
||||||
|
return new Deciduous(name);
|
||||||
|
} else if (evergreen.contains(name)) {
|
||||||
|
return new Evergreen(name);
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("Tree could not be classified");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.baeldung.object.type;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
class TreeSorterAssertJUnitTest {
|
||||||
|
private final TreeSorter tested = new TreeSorter();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sortTreeShouldReturnEvergreen_WhenPineIsPassed() {
|
||||||
|
Tree tree = tested.sortTree("Pine");
|
||||||
|
assertThat(tree).isExactlyInstanceOf(Evergreen.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sortTreeShouldReturnDecidious_WhenBirchIsPassed() {
|
||||||
|
Tree tree = tested.sortTree("Birch");
|
||||||
|
assertThat(tree).hasSameClassAs(new Deciduous("Birch"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package com.baeldung.object.type;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
class TreeSorterHamcrestUnitTest {
|
||||||
|
private final TreeSorter tested = new TreeSorter();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sortTreeShouldReturnEvergreen_WhenPineIsPassed() {
|
||||||
|
Tree tree = tested.sortTree("Pine");
|
||||||
|
//with JUnit assertEquals:
|
||||||
|
assertEquals(tree.getClass(), Evergreen.class);
|
||||||
|
//with Hamcrest instanceOf:
|
||||||
|
assertThat(tree, instanceOf(Evergreen.class));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sortTreeShouldReturnDecidious_WhenBirchIsPassed() {
|
||||||
|
Tree tree = tested.sortTree("Birch");
|
||||||
|
assertThat(tree, instanceOf(Deciduous.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue