Upgrade JUnit from 4.13 to Junit Jupiter 5.6.1 (#61)

* Upgrade JUnit from 4.13 to Junit Jupiter 5.6.1

* Update CHANGELOG
This commit is contained in:
Paul Campbell 2020-03-22 21:49:05 +00:00 committed by GitHub
parent 82f0c25b1d
commit 3cb432a3ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 270 additions and 248 deletions

View file

@ -13,12 +13,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
** Changed ** Changed
- Moved: Node.drawTree to Nodes (#60)
- Pinned pitest-junit5-plugin at 0.9 (#59)
- Replace Jenkins with Github Actions (#57) - Replace Jenkins with Github Actions (#57)
- [checkstyle] suppress npath complexity issues - [checkstyle] suppress npath complexity issues
- [coverage] lower requirements - [coverage] lower requirements
- Clean up changelog and readme, and remove external build dependencies (#38) - Clean up changelog and readme, and remove external build dependencies (#38)
- Pinned pitest-junit5-plugin at 0.9 (#59)
- Moved: Node.drawTree to Nodes (#60)
** Removed ** Removed
@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
** Dependencies ** Dependencies
- Upgraded JUnit from 4.13 to Junit Jupiter 5.6.1
- Bump kemitix-checkstyle-ruleset from 4.0.1 to 5.4.0 (#59) - Bump kemitix-checkstyle-ruleset from 4.0.1 to 5.4.0 (#59)
- Bump kemitix-parent from 5.2.0 to 5.3.0 (#56) - Bump kemitix-parent from 5.2.0 to 5.3.0 (#56)
- Bump lombok from 1.18.10 to 1.18.12 (#55) - Bump lombok from 1.18.10 to 1.18.12 (#55)

View file

@ -17,6 +17,8 @@
<properties> <properties>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<tiles-maven-plugin.version>2.16</tiles-maven-plugin.version> <tiles-maven-plugin.version>2.16</tiles-maven-plugin.version>
<kemitix-maven-tiles.version>2.4.1</kemitix-maven-tiles.version> <kemitix-maven-tiles.version>2.4.1</kemitix-maven-tiles.version>
<pitest-junit5-plugin.version>0.9</pitest-junit5-plugin.version> <pitest-junit5-plugin.version>0.9</pitest-junit5-plugin.version>
@ -24,7 +26,7 @@
<lombok.version>1.18.12</lombok.version> <lombok.version>1.18.12</lombok.version>
<assertj.version>3.15.0</assertj.version> <assertj.version>3.15.0</assertj.version>
<trajano-commons-testing.version>2.1.0</trajano-commons-testing.version> <trajano-commons-testing.version>2.1.0</trajano-commons-testing.version>
<junit.version>4.13</junit.version> <junit.version>5.6.1</junit.version>
<hamcrest.version>2.2</hamcrest.version> <hamcrest.version>2.2</hamcrest.version>
<jacoco-class-instruction-covered-ratio>97%</jacoco-class-instruction-covered-ratio> <jacoco-class-instruction-covered-ratio>97%</jacoco-class-instruction-covered-ratio>
</properties> </properties>
@ -52,8 +54,8 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>junit</artifactId> <artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version> <version>${junit.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>

View file

@ -1,10 +1,9 @@
package net.kemitix.node; package net.kemitix.node;
import lombok.val; import lombok.val;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThat;
/** /**
* Tests for {@link EmptyNodeException}. * Tests for {@link EmptyNodeException}.
@ -23,7 +22,8 @@ public class EmptyNodeExceptionTest {
//when //when
val nodeException = new EmptyNodeException(message); val nodeException = new EmptyNodeException(message);
//then //then
assertThat(nodeException.getMessage(), is(message)); assertThat(nodeException.getMessage())
.isEqualTo(message);
} }
} }

View file

@ -1,6 +1,6 @@
package net.kemitix.node; package net.kemitix.node;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;

View file

@ -2,10 +2,9 @@ package net.kemitix.node;
import lombok.val; import lombok.val;
import org.assertj.core.api.SoftAssertions; import org.assertj.core.api.SoftAssertions;
import org.junit.Rule; import org.junit.jupiter.api.DisplayName;
import org.junit.Test; import org.junit.jupiter.api.Nested;
import org.junit.experimental.categories.Category; import org.junit.jupiter.api.Test;
import org.junit.rules.ExpectedException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -13,7 +12,7 @@ import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.*;
/** /**
* Test for {@link ImmutableNodeItem}. * Test for {@link ImmutableNodeItem}.
@ -22,18 +21,8 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class ImmutableNodeItemTest { public class ImmutableNodeItemTest {
private static final String IMMUTABLE_OBJECT = "Immutable object";
@Rule
public ExpectedException exception = ExpectedException.none();
private Node<String> immutableNode; private Node<String> immutableNode;
private void expectImmutableException() {
exception.expect(UnsupportedOperationException.class);
exception.expectMessage(IMMUTABLE_OBJECT);
}
@Test @Test
public void canCreateAnEmptyAndUnnamedNode() { public void canCreateAnEmptyAndUnnamedNode() {
//when //when
@ -53,9 +42,11 @@ public class ImmutableNodeItemTest {
public void shouldThrowExceptionOnSetName() { public void shouldThrowExceptionOnSetName() {
//given //given
immutableNode = Nodes.asImmutable(Nodes.unnamedRoot(null)); immutableNode = Nodes.asImmutable(Nodes.unnamedRoot(null));
expectImmutableException();
//when //when
immutableNode.setName("named"); assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() ->
immutableNode.setName("named"))
.withMessage("Immutable object");
} }
@Test @Test
@ -104,46 +95,54 @@ public class ImmutableNodeItemTest {
public void shouldNotBeAbleToAddChildToImmutableTree() { public void shouldNotBeAbleToAddChildToImmutableTree() {
//given //given
immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("root")); immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("root"));
expectImmutableException();
//when //when
Nodes.unnamedChild("child", immutableNode); assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() ->
Nodes.unnamedChild("child", immutableNode))
.withMessage("Immutable object");
} }
@Test @Test
public void shouldThrowExceptionWhenSetParent() { public void shouldThrowExceptionWhenSetParent() {
//given //given
immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("subject")); immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("subject"));
expectImmutableException();
//when //when
immutableNode.setParent(Nodes.unnamedRoot("child")); assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() ->
immutableNode.setParent(Nodes.unnamedRoot("child")))
.withMessage("Immutable object");
} }
@Test @Test
public void shouldThrowExceptionWhenAddingChild() { public void shouldThrowExceptionWhenAddingChild() {
//given //given
immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("subject")); immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("subject"));
expectImmutableException();
//when //when
immutableNode.addChild(Nodes.unnamedRoot("child")); assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() ->
immutableNode.addChild(Nodes.unnamedRoot("child")))
.withMessage("Immutable object");
} }
/** @Nested
* Test that we can walk a tree to the target node. @DisplayName("findInPath")
*/ public class FindInPathTests {
@Test
@Category(NodeFindInPathTestsCategory.class) /**
public void shouldWalkTreeToNode() { * Test that we can walk a tree to the target node.
//given */
val root = Nodes.unnamedRoot("root"); @Test
Nodes.namedChild("child", "child", Nodes.unnamedChild("parent", root)); public void shouldWalkTreeToNode() {
immutableNode = Nodes.asImmutable(root); //given
//when val root = Nodes.unnamedRoot("root");
val result = immutableNode.findInPath(Arrays.asList("parent", "child")); Nodes.namedChild("child", "child", Nodes.unnamedChild("parent", root));
//then immutableNode = Nodes.asImmutable(root);
assertThat(result.isPresent()).isTrue(); //when
if (result.isPresent()) { val result = immutableNode.findInPath(Arrays.asList("parent", "child"));
assertThat(result.get() //then
.getName()).isEqualTo("child"); assertThat(result.isPresent()).isTrue();
result.map(value ->
assertThat(value.getName()).isEqualTo("child"));
} }
} }
@ -152,7 +151,6 @@ public class ImmutableNodeItemTest {
* doesn't exist. * doesn't exist.
*/ */
@Test @Test
@Category(NodeFindInPathTestsCategory.class)
public void shouldNotFindNonExistentChildNode() { public void shouldNotFindNonExistentChildNode() {
//given //given
val root = Nodes.unnamedRoot("root"); val root = Nodes.unnamedRoot("root");
@ -168,14 +166,14 @@ public class ImmutableNodeItemTest {
* Test that when we pass null we get an exception. * Test that when we pass null we get an exception.
*/ */
@Test @Test
@Category(NodeFindInPathTestsCategory.class)
public void shouldThrowNEWhenWalkTreeNull() { public void shouldThrowNEWhenWalkTreeNull() {
//given //given
immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("subject")); immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("subject"));
exception.expect(NullPointerException.class);
exception.expectMessage("path");
//when //when
immutableNode.findInPath(null); assertThatNullPointerException()
.isThrownBy(() ->
immutableNode.findInPath(null))
.withMessageContaining("path");
} }
/** /**
@ -183,7 +181,6 @@ public class ImmutableNodeItemTest {
* a result. * a result.
*/ */
@Test @Test
@Category(NodeFindInPathTestsCategory.class)
public void shouldReturnEmptyForEmptyWalkTreePath() { public void shouldReturnEmptyForEmptyWalkTreePath() {
//given //given
immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("subject")); immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("subject"));
@ -218,10 +215,11 @@ public class ImmutableNodeItemTest {
public void getChildShouldThrowNPEWhenThereIsNoChild() { public void getChildShouldThrowNPEWhenThereIsNoChild() {
//given //given
immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("data")); immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("data"));
exception.expect(NullPointerException.class);
exception.expectMessage("child");
//when //when
immutableNode.findChild(null); assertThatNullPointerException()
.isThrownBy(() ->
immutableNode.findChild(null))
.withMessageContaining("child");
} }
@Test @Test
@ -258,19 +256,22 @@ public class ImmutableNodeItemTest {
public void removingParentThrowsException() { public void removingParentThrowsException() {
//given //given
immutableNode = Nodes.asImmutable(Nodes.unnamedRoot(null)); immutableNode = Nodes.asImmutable(Nodes.unnamedRoot(null));
expectImmutableException();
//when //when
immutableNode.removeParent(); assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() ->
immutableNode.removeParent())
.withMessage("Immutable object");
} }
@Test @Test
public void findChildNamedShouldThrowNPEWhenNameIsNull() { public void findChildNamedShouldThrowNPEWhenNameIsNull() {
//given //given
exception.expect(NullPointerException.class);
exception.expectMessage("name");
immutableNode = Nodes.asImmutable(Nodes.unnamedRoot(null)); immutableNode = Nodes.asImmutable(Nodes.unnamedRoot(null));
//when //when
immutableNode.findChildByName(null); assertThatNullPointerException()
.isThrownBy(() ->
immutableNode.findChildByName(null))
.withMessageContaining("name");
} }
@Test @Test
@ -301,27 +302,33 @@ public class ImmutableNodeItemTest {
public void removeChildThrowsExceptions() { public void removeChildThrowsExceptions() {
//given //given
immutableNode = Nodes.asImmutable(Nodes.unnamedRoot(null)); immutableNode = Nodes.asImmutable(Nodes.unnamedRoot(null));
expectImmutableException();
//then //then
immutableNode.removeChild(null); assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() ->
immutableNode.removeChild(null))
.withMessage("Immutable object");
} }
@Test @Test
public void setDataShouldThrowException() { public void setDataShouldThrowException() {
//given //given
immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("initial")); immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("initial"));
expectImmutableException();
//when //when
immutableNode.setData("updated"); assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() ->
immutableNode.setData("updated"))
.withMessage("Immutable object");
} }
@Test @Test
public void createChildThrowsException() { public void createChildThrowsException() {
//given //given
immutableNode = Nodes.asImmutable(Nodes.unnamedRoot(null)); immutableNode = Nodes.asImmutable(Nodes.unnamedRoot(null));
expectImmutableException();
//when //when
immutableNode.createChild("child data", "child name"); assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() ->
immutableNode.createChild("child data", "child name"))
.withMessage("Immutable object");
} }
@Test @Test
@ -338,36 +345,43 @@ public class ImmutableNodeItemTest {
public void createChildShouldThrowException() { public void createChildShouldThrowException() {
//given //given
immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("")); immutableNode = Nodes.asImmutable(Nodes.unnamedRoot(""));
expectImmutableException();
//when //when
immutableNode.createChild("child"); assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() ->
immutableNode.createChild("child"))
.withMessage("Immutable object");
} }
@Test @Test
public void createDescendantLineShouldThrowException() { public void createDescendantLineShouldThrowException() {
//given //given
immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("")); immutableNode = Nodes.asImmutable(Nodes.unnamedRoot(""));
expectImmutableException();
//when //when
immutableNode.createDescendantLine(Arrays.asList("child", "grandchild")); assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() ->
immutableNode.createDescendantLine(Arrays.asList("child", "grandchild")))
.withMessage("Immutable object");
} }
@Test @Test
public void insertInPathShouldThrowException() { public void insertInPathShouldThrowException() {
//given //given
immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("")); immutableNode = Nodes.asImmutable(Nodes.unnamedRoot(""));
expectImmutableException();
//when //when
immutableNode.insertInPath(null, ""); assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() ->
immutableNode.insertInPath(null, ""))
.withMessage("Immutable object");
} }
@Test @Test
public void AsImmutableShouldThrowIAEWhenNotRoot() { public void AsImmutableShouldThrowIAEWhenNotRoot() {
//given assertThatExceptionOfType(IllegalArgumentException.class)
exception.expect(IllegalArgumentException.class); .isThrownBy(() ->
exception.expectMessage("source must be the root node"); Nodes.asImmutable(
//when Nodes.unnamedChild("child",
Nodes.asImmutable(Nodes.unnamedChild("child", Nodes.unnamedRoot("root"))); Nodes.unnamedRoot("root"))))
.withMessage("source must be the root node");
} }
@Test @Test

View file

@ -1,4 +0,0 @@
package net.kemitix.node;
public interface IsNamedCategory {
}

View file

@ -1,10 +1,9 @@
package net.kemitix.node; package net.kemitix.node;
import lombok.val; import lombok.val;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThat;
/** /**
* Tests for {@link NodeException}. * Tests for {@link NodeException}.
@ -23,7 +22,8 @@ public class NodeExceptionTest {
//when //when
val nodeException = new NodeException(message); val nodeException = new NodeException(message);
//then //then
assertThat(nodeException.getMessage(), is(message)); assertThat(nodeException.getMessage())
.isEqualTo(message);
} }
} }

View file

@ -1,9 +0,0 @@
package net.kemitix.node;
/**
* Category marker for tests relating to implementations of Node.findInPath(...).
*
* @author Paul Campbell
*/
public interface NodeFindInPathTestsCategory {
}

View file

@ -2,10 +2,9 @@ package net.kemitix.node;
import lombok.val; import lombok.val;
import org.assertj.core.api.SoftAssertions; import org.assertj.core.api.SoftAssertions;
import org.junit.Rule; import org.junit.jupiter.api.DisplayName;
import org.junit.Test; import org.junit.jupiter.api.Nested;
import org.junit.experimental.categories.Category; import org.junit.jupiter.api.Test;
import org.junit.rules.ExpectedException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -13,7 +12,7 @@ import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.*;
/** /**
* Test for {@link NodeItem}. * Test for {@link NodeItem}.
@ -22,9 +21,6 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class NodeItemTest { public class NodeItemTest {
@Rule
public ExpectedException exception = ExpectedException.none();
private Node<String> node; private Node<String> node;
@Test @Test
@ -140,10 +136,11 @@ public class NodeItemTest {
//given //given
node = Nodes.unnamedRoot("subject"); node = Nodes.unnamedRoot("subject");
val child = Nodes.unnamedChild("child", node); val child = Nodes.unnamedChild("child", node);
exception.expect(NodeException.class);
exception.expectMessage("Parent is a descendant");
//when //when
node.setParent(child); assertThatExceptionOfType(NodeException.class)
.isThrownBy(() ->
node.setParent(child))
.withMessage("Parent is a descendant");
} }
/** /**
@ -185,10 +182,11 @@ public class NodeItemTest {
public void shouldThrowNPEWhenSetParentNull() { public void shouldThrowNPEWhenSetParentNull() {
//given //given
node = Nodes.unnamedRoot("subject"); node = Nodes.unnamedRoot("subject");
exception.expect(NullPointerException.class);
exception.expectMessage("parent");
//when //when
node.setParent(null); assertThatNullPointerException()
.isThrownBy(() ->
node.setParent(null))
.withMessageContaining("parent");
} }
/** /**
@ -199,10 +197,11 @@ public class NodeItemTest {
public void setParentShouldThrowNodeExceptionWhenParentIsSelf() { public void setParentShouldThrowNodeExceptionWhenParentIsSelf() {
//given //given
node = Nodes.unnamedRoot("subject"); node = Nodes.unnamedRoot("subject");
exception.expect(NodeException.class);
exception.expectMessage("Parent is a descendant");
//when //when
node.setParent(node); assertThatExceptionOfType(NodeException.class)
.isThrownBy(() ->
node.setParent(node))
.withMessage("Parent is a descendant");
} }
/** /**
@ -256,10 +255,11 @@ public class NodeItemTest {
public void shouldThrowNPEWhenAddingNullAsChild() { public void shouldThrowNPEWhenAddingNullAsChild() {
//given //given
node = Nodes.unnamedRoot("subject"); node = Nodes.unnamedRoot("subject");
exception.expect(NullPointerException.class);
exception.expectMessage("child");
//when //when
node.addChild(null); assertThatNullPointerException()
.isThrownBy(() ->
node.addChild(null))
.withMessageContaining("child");
} }
/** /**
@ -285,10 +285,11 @@ public class NodeItemTest {
public void addChildShouldThrowNodeExceptionWhenAddingANodeAsOwnChild() { public void addChildShouldThrowNodeExceptionWhenAddingANodeAsOwnChild() {
//given //given
node = Nodes.unnamedRoot("subject"); node = Nodes.unnamedRoot("subject");
exception.expect(NodeException.class);
exception.expectMessage("Child is an ancestor");
//then //then
node.addChild(node); assertThatExceptionOfType(NodeException.class)
.isThrownBy(() ->
node.addChild(node))
.withMessage("Child is an ancestor");
} }
/** /**
@ -298,10 +299,11 @@ public class NodeItemTest {
public void addChildShouldThrowNodeExceptionWhenAddingSelfAsChild() { public void addChildShouldThrowNodeExceptionWhenAddingSelfAsChild() {
//given //given
node = Nodes.unnamedRoot("subject"); node = Nodes.unnamedRoot("subject");
exception.expect(NodeException.class);
exception.expectMessage("Child is an ancestor");
//when //when
node.addChild(node); assertThatExceptionOfType(NodeException.class)
.isThrownBy(() ->
node.addChild(node))
.withMessage("Child is an ancestor");
} }
/** /**
@ -313,10 +315,11 @@ public class NodeItemTest {
//given //given
val parent = Nodes.unnamedRoot("parent"); val parent = Nodes.unnamedRoot("parent");
node = Nodes.unnamedChild("subject", parent); node = Nodes.unnamedChild("subject", parent);
exception.expect(NodeException.class);
exception.expectMessage("Child is an ancestor");
//when //when
node.addChild(parent); assertThatExceptionOfType(NodeException.class)
.isThrownBy(() ->
node.addChild(parent))
.withMessage("Child is an ancestor");
} }
/** /**
@ -329,10 +332,11 @@ public class NodeItemTest {
val grandParent = Nodes.unnamedRoot("grandparent"); val grandParent = Nodes.unnamedRoot("grandparent");
val parent = Nodes.unnamedChild("parent", grandParent); val parent = Nodes.unnamedChild("parent", grandParent);
node = Nodes.unnamedChild("subject", parent); node = Nodes.unnamedChild("subject", parent);
exception.expect(NodeException.class);
exception.expectMessage("Child is an ancestor");
//when //when
node.addChild(grandParent); assertThatExceptionOfType(NodeException.class)
.isThrownBy(() ->
node.addChild(grandParent))
.withMessage("Child is an ancestor");
} }
/** /**
@ -347,82 +351,84 @@ public class NodeItemTest {
node.addChild(child); node.addChild(child);
//then //then
assertThat(child.findParent()).as("when a node is added as a child, the child has the node as " + "its parent") assertThat(child.findParent()).as("when a node is added as a child, the child has the node as " + "its parent")
.contains(node); .contains(node);
} }
/** @Nested
* Test that we can walk a tree to the target node. @DisplayName("findInPath")
*/ public class FindInPathTests {
@Test
@Category(NodeFindInPathTestsCategory.class)
public void shouldWalkTreeToNode() {
//given
val grandparent = "grandparent";
val grandParentNode = Nodes.unnamedRoot(grandparent);
val parent = "parent";
val parentNode = Nodes.unnamedChild(parent, grandParentNode);
val subject = "subject";
node = Nodes.unnamedChild(subject, parentNode);
//when
val result = grandParentNode.findInPath(
Arrays.asList(parent, subject));
//then
assertThat(result.isPresent())
.as("when we walk the tree to a node it is found")
.isTrue();
result.ifPresent(
stringNode ->
assertThat(stringNode)
.as("when we walk the tree to a node we find the correct node")
.isSameAs(node));
}
/** /**
* Test that we get an empty {@link Optional} when walking a path that * Test that we can walk a tree to the target node.
* doesn't exist. */
*/ @Test
@Test public void shouldWalkTreeToNode() {
@Category(NodeFindInPathTestsCategory.class) //given
public void shouldNotFindNonExistentChildNode() { val grandparent = "grandparent";
//given val grandParentNode = Nodes.unnamedRoot(grandparent);
val parent = "parent"; val parent = "parent";
val parentNode = Nodes.unnamedRoot(parent); val parentNode = Nodes.unnamedChild(parent, grandParentNode);
val subject = "subject"; val subject = "subject";
node = Nodes.unnamedChild(subject, parentNode); node = Nodes.unnamedChild(subject, parentNode);
//when //when
val result = parentNode.findInPath(Arrays.asList(subject, "no child")); val result = grandParentNode.findInPath(
//then Arrays.asList(parent, subject));
assertThat(result.isPresent()).as("when we walk the tree to a node that doesn't exists, nothing" + " is found") //then
.isFalse(); assertThat(result.isPresent())
} .as("when we walk the tree to a node it is found")
.isTrue();
result.ifPresent(
stringNode ->
assertThat(stringNode)
.as("when we walk the tree to a node we find the correct node")
.isSameAs(node));
}
/** /**
* Test that when we pass null we get an exception. * Test that we get an empty {@link Optional} when walking a path that
*/ * doesn't exist.
@Test */
@Category(NodeFindInPathTestsCategory.class) @Test
public void shouldThrowNPEWhenWalkTreeNull() { public void shouldNotFindNonExistentChildNode() {
//given //given
node = Nodes.unnamedRoot("subject"); val parent = "parent";
exception.expect(NullPointerException.class); val parentNode = Nodes.unnamedRoot(parent);
exception.expectMessage("path"); val subject = "subject";
//when node = Nodes.unnamedChild(subject, parentNode);
node.findInPath(null); //when
} val result = parentNode.findInPath(Arrays.asList(subject, "no child"));
//then
assertThat(result.isPresent()).as("when we walk the tree to a node that doesn't exists, nothing" + " is found")
.isFalse();
}
/** /**
* Test that when we pass an empty path we get and empty {@link Optional} as * Test that when we pass null we get an exception.
* a result. */
*/ @Test
@Test public void shouldThrowNPEWhenWalkTreeNull() {
@Category(NodeFindInPathTestsCategory.class) //given
public void shouldReturnEmptyForEmptyWalkTreePath() { node = Nodes.unnamedRoot("subject");
//given //when
node = Nodes.unnamedRoot("subject"); assertThatNullPointerException()
//when .isThrownBy(() ->
val result = node.findInPath(Collections.emptyList()); node.findInPath(null))
//then .withMessageContaining("path");
assertThat(result).isEmpty(); }
/**
* Test that when we pass an empty path we get and empty {@link Optional} as
* a result.
*/
@Test
public void shouldReturnEmptyForEmptyWalkTreePath() {
//given
node = Nodes.unnamedRoot("subject");
//when
val result = node.findInPath(Collections.emptyList());
//then
assertThat(result).isEmpty();
}
} }
/** /**
@ -472,10 +478,11 @@ public class NodeItemTest {
public void createDescendantLineShouldThrowNPEWhenDescendantsAreNull() { public void createDescendantLineShouldThrowNPEWhenDescendantsAreNull() {
//given //given
node = Nodes.unnamedRoot("subject"); node = Nodes.unnamedRoot("subject");
exception.expect(NullPointerException.class);
exception.expectMessage("descendants");
//when //when
node.createDescendantLine(null); assertThatNullPointerException()
.isThrownBy(() ->
node.createDescendantLine(null))
.withMessageContaining("descendants");
} }
/** /**
@ -519,10 +526,11 @@ public class NodeItemTest {
public void getChildShouldThrowNPEWhenThereIsNoChild() { public void getChildShouldThrowNPEWhenThereIsNoChild() {
//given //given
node = Nodes.unnamedRoot("data"); node = Nodes.unnamedRoot("data");
exception.expect(NullPointerException.class);
exception.expectMessage("child");
//when //when
node.findChild(null); assertThatNullPointerException()
.isThrownBy(() ->
node.findChild(null))
.withMessageContaining("child");
} }
/** /**
@ -554,10 +562,11 @@ public class NodeItemTest {
public void createChildShouldThrowNPEWhenChildIsNull() { public void createChildShouldThrowNPEWhenChildIsNull() {
//given //given
node = Nodes.unnamedRoot("subject"); node = Nodes.unnamedRoot("subject");
exception.expect(NullPointerException.class);
exception.expectMessage("child");
//when //when
node.createChild(null); assertThatNullPointerException()
.isThrownBy(() ->
node.createChild(null))
.withMessageContaining("child");
} }
@Test @Test
@ -573,10 +582,11 @@ public class NodeItemTest {
val alpha = Nodes.namedRoot("alpha data", "alpha"); val alpha = Nodes.namedRoot("alpha data", "alpha");
node.addChild(alpha); node.addChild(alpha);
val beta = Nodes.namedRoot("beta data", "alpha"); val beta = Nodes.namedRoot("beta data", "alpha");
exception.expect(NodeException.class);
exception.expectMessage("Node with that name already exists here");
//when //when
node.addChild(beta); assertThatExceptionOfType(NodeException.class)
.isThrownBy(() ->
node.addChild(beta))
.withMessage("Node with that name already exists here");
} }
@Test @Test
@ -681,8 +691,6 @@ public class NodeItemTest {
@Test @Test
public void placeNodeInTreeWhereNonEmptyNodeWithSameNameExists() { public void placeNodeInTreeWhereNonEmptyNodeWithSameNameExists() {
//given //given
exception.expect(NodeException.class);
exception.expectMessage("A non-empty node named 'grandchild' already exists here");
node = Nodes.unnamedRoot(null); node = Nodes.unnamedRoot(null);
val child = Nodes.namedChild("child data", "child", node); val child = Nodes.namedChild("child data", "child", node);
Nodes.namedChild("data", "grandchild", child); Nodes.namedChild("data", "grandchild", child);
@ -690,7 +698,14 @@ public class NodeItemTest {
// only grandchild has data // only grandchild has data
//when //when
// attempt to add another node called 'grandchild' to 'child' // attempt to add another node called 'grandchild' to 'child'
node.insertInPath(Nodes.namedRoot("cuckoo", "grandchild"), "child"); assertThatExceptionOfType(NodeException.class)
.isThrownBy(() ->
node.insertInPath(
Nodes.namedRoot(
"cuckoo",
"grandchild"),
"child"))
.withMessage("A non-empty node named 'grandchild' already exists here");
} }
@Test @Test
@ -730,38 +745,41 @@ public class NodeItemTest {
@Test @Test
public void findChildNamedShouldThrowNPEWhenNameIsNull() { public void findChildNamedShouldThrowNPEWhenNameIsNull() {
//given //given
exception.expect(NullPointerException.class);
exception.expectMessage("name");
node = Nodes.unnamedRoot(null); node = Nodes.unnamedRoot(null);
//when //when
node.findChildByName(null); assertThatNullPointerException()
.isThrownBy(() ->
node.findChildByName(null))
.withMessageContaining("name");
} }
@Test @Nested
@Category(IsNamedCategory.class) @DisplayName("isNamed")
public void isNamedNull() { public class IsNamedTests {
//given
node = Nodes.namedRoot(null, null);
//then
assertThat(node.isNamed()).isFalse();
}
@Test @Test
@Category(IsNamedCategory.class) public void isNamedNull() {
public void isNamedEmpty() { //given
//given node = Nodes.namedRoot(null, null);
node = Nodes.namedRoot(null, ""); //then
//then assertThat(node.isNamed()).isFalse();
assertThat(node.isNamed()).isFalse(); }
}
@Test @Test
@Category(IsNamedCategory.class) public void isNamedEmpty() {
public void isNamedNamed() { //given
//given node = Nodes.namedRoot(null, "");
node = Nodes.namedRoot(null, "named"); //then
//then assertThat(node.isNamed()).isFalse();
assertThat(node.isNamed()).isTrue(); }
@Test
public void isNamedNamed() {
//given
node = Nodes.namedRoot(null, "named");
//then
assertThat(node.isNamed()).isTrue();
}
} }
@Test @Test

View file

@ -1,7 +1,7 @@
package net.kemitix.node; package net.kemitix.node;
import lombok.val; import lombok.val;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;

View file

@ -2,7 +2,7 @@ package net.kemitix.node;
import lombok.val; import lombok.val;
import org.assertj.core.api.SoftAssertions; import org.assertj.core.api.SoftAssertions;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static net.trajano.commons.testing.UtilityClassTestUtil import static net.trajano.commons.testing.UtilityClassTestUtil
.assertUtilityClassWellDefined; .assertUtilityClassWellDefined;

View file

@ -1,10 +1,9 @@
package net.kemitix.node; package net.kemitix.node;
import lombok.val; import lombok.val;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThat;
/** /**
* Tests for {@link OrphanedNodeException}. * Tests for {@link OrphanedNodeException}.
@ -23,7 +22,8 @@ public class OrphanedNodeExceptionTest {
//when //when
val nodeException = new OrphanedNodeException(message); val nodeException = new OrphanedNodeException(message);
//then //then
assertThat(nodeException.getMessage(), is(message)); assertThat(nodeException.getMessage())
.isEqualTo(message);
} }
} }