BAEL-1861 - Running JUnit tests from a Java application (#4526)

* BAEL-1562 - Thymeleaf sample working

* BAEL-1562 Code added for Fragments sample

* BAEL-1562 - Last correction for the test

* BAEL-1562 - Thymeleaf sample working

* BAEL-1562 Code added for Fragments sample

* BAEL-1562 - Last correction for the test

* Updates Thymeleaf version to 3.0.9.RELEASE

* Added msf4j projects

* updated msf4j project folder

* fixed issue with spring-thymeleaf/pom.xml

* Removed depedency-reduced-pom.xml

* Whitespacing fix

* Strange git issue with README.MD, wouldn't revert the file

* Added jupiter api

* Corrected junit test

* Added test engine to plugin

* Removed extra tag

* Little fixes to junit4 and junit4 run from java

* Removed scope from pom.xml

* Removed bin file from testing

* Slight changes for PMD

* Slight changes for PMD

* ok, moved code to another folder

* Renamed and fixed runjunitfromjava

* moved test classes to test folder

* moved main to src/java

* BAEL-1861 Moved test running classes to src/test/java

* Added changes to runjunitfromjava

* Added changes to runjunitfromjava

* BAEL-1861 Changed test execution code examples

* BAEL-1861 Changed test execution code examples; formatting
This commit is contained in:
Pello Altadill 2018-08-01 11:58:35 +02:00 committed by Predrag Maric
parent a8288f98a7
commit c765acd1da
19 changed files with 688 additions and 0 deletions

View File

@ -0,0 +1,54 @@
<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>com.baeldung.junit</groupId>
<artifactId>applicationtesting</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>applicationtesting</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<junit-jupiter.version>5.2.0</junit-jupiter.version>
<junit-launcher.version>1.2.0</junit-launcher.version>
<junit4.version>4.12</junit4.version>
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${junit-launcher.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit4.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,41 @@
package com.baeldung.junit.runfromjava.listnode;
public class ListNode {
private int value;
private ListNode next;
public ListNode(int v) {
value = v;
}
public ListNode(int v, ListNode next) {
value = v;
this.next = next;
}
public int getValue() {
return value;
}
public ListNode getNext() {
return next;
}
public void setNext(ListNode next) {
this.next = next;
}
public String toString() {
String result = "";
ListNode tmp = this;
while (tmp.next != null) {
result += tmp.value + "->";
tmp = tmp.next;
}
result += tmp.value;
return result.toString();
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.junit.runfromjava.listnode;
public class MergeLists {
public ListNode merge(ListNode list1, ListNode list2) {
if (list1 == null) {
return list2;
}
if (list2 == null) {
return list1;
}
if (list1.getValue() <= list2.getValue()) {
list1.setNext(merge(list1.getNext(), list2));
return list1;
} else {
list2.setNext(merge(list2.getNext(), list1));
return list2;
}
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.junit.runfromjava.listnode;
public class RemovedNthElement {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode start = new ListNode(0);
start.setNext(head);
ListNode fast = start;
ListNode slow = start;
for (int i = 0; i < n + 1 && fast != null; i++) {
fast = fast.getNext();
}
while (fast != null) {
fast = fast.getNext();
slow = slow.getNext();
}
slow.setNext(slow.getNext()
.getNext());
return start.getNext();
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.junit.runfromjava.listnode;
public class RotateList {
public ListNode rotateRight(ListNode list, int n) {
if (list == null || list.getNext() == null) {
return list;
}
ListNode tmpList = new ListNode(0);
tmpList.setNext(list);
ListNode fast = tmpList;
ListNode slow = tmpList;
int listLength;
for (listLength = 0; fast.getNext() != null; listLength++) {
fast = fast.getNext();
}
for (int j = listLength - n % listLength; j > 0; j--) {
slow = slow.getNext();
}
fast.setNext(tmpList.getNext());
tmpList.setNext(slow.getNext());
slow.setNext(null);
return tmpList.getNext();
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.junit.runfromjava.listnode;
public class SwapNodes {
public ListNode swapPairs(ListNode listHead) {
ListNode result = new ListNode(0);
result.setNext(listHead);
ListNode current = result;
while (current.getNext() != null && current
.getNext()
.getNext() != null) {
ListNode first = current.getNext();
ListNode second = current
.getNext()
.getNext();
first.setNext(second.getNext());
current.setNext(second);
current
.getNext()
.setNext(first);
current = current
.getNext()
.getNext();
}
return result.getNext();
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.junit4.runfromjava;
import org.junit.Test;
import com.baeldung.junit.runfromjava.listnode.ListNode;
import static org.junit.Assert.*;
public class ListNodeUnitTest {
@Test
public void whenListHasOneElement_thenGetExpectedValue() {
ListNode listNode = new ListNode(42);
assertEquals(listNode.getValue(), 42);
}
@Test
public void whenInitSimpleList_thenGettersGiveExpectedValues() {
ListNode listNode = new ListNode(42, new ListNode(666, null));
assertEquals(listNode.getValue(), 42);
assertEquals(listNode.getNext()
.getValue(), 666);
}
@Test
public void whenConvertingListToString_thenGetExpectedValue() {
ListNode listNode = new ListNode(42, new ListNode(666, new ListNode(15, null)));
assertEquals(listNode.toString(), "42->666->15");
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.junit4.runfromjava;
import org.junit.Test;
import com.baeldung.junit.runfromjava.listnode.ListNode;
import com.baeldung.junit.runfromjava.listnode.MergeLists;
import static org.junit.Assert.*;
import org.junit.Before;
public class MergeListsUnitTest {
private ListNode listNode1;
private ListNode listNode2;
private MergeLists mergeLists;
@Before
public void setUp() throws Exception {
mergeLists = new MergeLists();
listNode1 = new ListNode(2, new ListNode(4, new ListNode(6, new ListNode(8, null))));
listNode2 = new ListNode(1, new ListNode(3, new ListNode(5, new ListNode(7, null))));
}
@Test
public void whenMergingNormalLists_thenGetExpectedString() {
assertEquals(mergeLists.merge(listNode1, listNode2)
.toString(), "1->2->3->4->5->6->7->8");
}
@Test
public void whenMergingNullLists_thenGetNull() {
listNode1 = null;
listNode2 = null;
assertNull(mergeLists.merge(listNode1, listNode2));
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.junit4.runfromjava;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({ ListNodeUnitTest.class, MergeListsUnitTest.class })
public class MyTestSuite {
}

View File

@ -0,0 +1,33 @@
package com.baeldung.junit4.runfromjava;
import org.junit.Test;
import com.baeldung.junit.runfromjava.listnode.ListNode;
import com.baeldung.junit.runfromjava.listnode.RemovedNthElement;
import static org.junit.Assert.*;
import org.junit.Before;
public class RemovedNthElementUnitTest {
private ListNode listNode;
private RemovedNthElement removedNthElement;
@Before
public void setUp() throws Exception {
removedNthElement = new RemovedNthElement();
listNode = new ListNode(42, new ListNode(666, new ListNode(15, new ListNode(3, null))));
}
@Test
public void whenRemovingSecondElement_thenReturnExpectedList() {
assertEquals(removedNthElement.removeNthFromEnd(listNode, 2)
.toString(), "42->666->3");
}
@Test
public void whenRemovingThirdElement_thenReturnExpectedList() {
assertEquals(removedNthElement.removeNthFromEnd(listNode, 3)
.toString(), "42->15->3");
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.junit4.runfromjava;
import org.junit.Test;
import com.baeldung.junit.runfromjava.listnode.ListNode;
import com.baeldung.junit.runfromjava.listnode.RotateList;
import static org.junit.Assert.*;
import org.junit.Before;
public class RotateListUnitTest {
private RotateList rotateList;
private ListNode listNode;
@Before
public void setUp() throws Exception {
rotateList = new RotateList();
listNode = new ListNode(42, new ListNode(666, new ListNode(15, new ListNode(3, null))));
}
@Test
public void whenRotatingListTwice_thenReturnExpectedList() {
assertEquals(rotateList.rotateRight(listNode, 2)
.toString(), "15->3->42->666");
}
@Test
public void whenRotatingListThreeTimes_thenReturnExpectedList() {
assertEquals(rotateList.rotateRight(listNode, 3)
.toString(), "666->15->3->42");
}
}

View File

@ -0,0 +1,93 @@
package com.baeldung.junit4.runfromjava;
import junit.extensions.ActiveTestSuite;
import junit.extensions.RepeatedTest;
import junit.framework.JUnit4TestAdapter;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.junit.internal.TextListener;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class RunJUnit4Tests {
public static void runOne() {
JUnitCore junit = new JUnitCore();
junit.addListener(new TextListener(System.out));
junit.run(MergeListsUnitTest.class);
}
public static void runAllClasses() {
JUnitCore junit = new JUnitCore();
junit.addListener(new TextListener(System.out));
Result result = junit.run(ListNodeUnitTest.class, MergeListsUnitTest.class, RemovedNthElementUnitTest.class, RotateListUnitTest.class, SwapNodesUnitTest.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
resultReport(result);
}
public static void runSuiteOfClasses() {
JUnitCore junit = new JUnitCore();
junit.addListener(new TextListener(System.out));
Result result = junit.run(MyTestSuite.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
resultReport(result);
}
public static void runRepeated() {
Test test = new JUnit4TestAdapter(MergeListsUnitTest.class);
RepeatedTest repeatedTest = new RepeatedTest(test, 5);
JUnitCore junit = new JUnitCore();
junit.addListener(new TextListener(System.out));
junit.run(repeatedTest);
}
public static void runRepeatedSuite() {
TestSuite mySuite = new ActiveTestSuite();
JUnitCore junit = new JUnitCore();
junit.addListener(new TextListener(System.out));
mySuite.addTest(new RepeatedTest(new JUnit4TestAdapter(MergeListsUnitTest.class), 5));
mySuite.addTest(new RepeatedTest(new JUnit4TestAdapter(RemovedNthElementUnitTest.class), 3));
junit.run(mySuite);
}
public static void resultReport(Result result) {
System.out.println("Finished. Result: Failures: " +
result.getFailureCount() + ". Ignored: " +
result.getIgnoreCount() + ". Tests run: " +
result.getRunCount() + ". Time: " +
result.getRunTime() + "ms.");
}
public static void main(String[] args) {
System.out.println("\nRunning one test class:");
runOne();
System.out.println("\nRunning all test classes:");
runAllClasses();
System.out.println("\nRunning a suite of test classes:");
runSuiteOfClasses();
System.out.println("\nRunning repeated tests:");
runRepeated();
System.out.println("\nRunning repeated suite tests:");
runRepeatedSuite();
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.junit4.runfromjava;
import org.junit.Test;
import com.baeldung.junit.runfromjava.listnode.ListNode;
import com.baeldung.junit.runfromjava.listnode.SwapNodes;
import static org.junit.Assert.*;
import org.junit.Before;
public class SwapNodesUnitTest {
private SwapNodes swapNodes;
private ListNode listNode;
@Before
public void setUp() throws Exception {
swapNodes = new SwapNodes();
listNode = new ListNode(42, new ListNode(666, new ListNode(15, new ListNode(3, null))));
}
@Test
public void whenSwappingPairs_thenReturnExpectedList() {
assertEquals(swapNodes.swapPairs(listNode)
.toString(), "666->42->3->15");
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.junit5.runfromjava;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import com.baeldung.junit.runfromjava.listnode.ListNode;
class ListNodeUnitTest {
@Test
void whenListHasOneElement_thenGetExpectedValue() {
ListNode listNode = new ListNode(42);
assertEquals(listNode.getValue(), 42);
}
@Test
void whenInitSimpleList_thenGettersGiveExpectedValues() {
ListNode listNode = new ListNode(42, new ListNode(666, null));
assertEquals(listNode.getValue(), 42);
assertEquals(listNode.getNext()
.getValue(), 666);
}
@Test
void whenConvertingListToString_thenGetExpectedValue() {
ListNode listNode = new ListNode(42, new ListNode(666, new ListNode(15, null)));
assertEquals(listNode.toString(), "42->666->15");
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.junit5.runfromjava;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import com.baeldung.junit.runfromjava.listnode.ListNode;
import com.baeldung.junit.runfromjava.listnode.MergeLists;
class MergeListsUnitTest {
private ListNode listNode1;
private ListNode listNode2;
private MergeLists mergeLists;
@BeforeEach
void setUp() throws Exception {
mergeLists = new MergeLists();
listNode1 = new ListNode(2, new ListNode(4, new ListNode(6, new ListNode(8, null))));
listNode2 = new ListNode(1, new ListNode(3, new ListNode(5, new ListNode(7, null))));
}
@RepeatedTest(10)
void whenMergingNormalLists_thenGetExpectedString() {
assertEquals(mergeLists
.merge(listNode1, listNode2)
.toString(), "1->2->3->4->5->6->7->8");
}
@RepeatedTest(5)
void whenMergingNullLists_thenGetNull() {
listNode1 = null;
listNode2 = null;
assertNull(mergeLists.merge(listNode1, listNode2));
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.junit5.runfromjava;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.baeldung.junit.runfromjava.listnode.ListNode;
import com.baeldung.junit.runfromjava.listnode.RemovedNthElement;
class RemovedNthElementUnitTest {
private ListNode listNode;
private RemovedNthElement removedNthElement;
@BeforeEach
void setUp() throws Exception {
removedNthElement = new RemovedNthElement();
listNode = new ListNode(42, new ListNode(666, new ListNode(15, new ListNode(3, null))));
}
@Test
void whenRemovingSecondElement_thenReturnExpectedList() {
assertEquals(removedNthElement.removeNthFromEnd(listNode, 2)
.toString(), "42->666->3");
}
@Test
void whenRemovingThirdElement_thenReturnExpectedList() {
assertEquals(removedNthElement.removeNthFromEnd(listNode, 3)
.toString(), "42->15->3");
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.junit5.runfromjava;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.baeldung.junit.runfromjava.listnode.ListNode;
import com.baeldung.junit.runfromjava.listnode.RotateList;
class RotateListUnitTest {
private RotateList rotateList;
private ListNode listNode;
@BeforeEach
void setUp() throws Exception {
rotateList = new RotateList();
listNode = new ListNode(42, new ListNode(666, new ListNode(15, new ListNode(3, null))));
}
@Test
void whenRotatingListTwice_thenReturnExpectedList() {
assertEquals(rotateList.rotateRight(listNode, 2)
.toString(), "15->3->42->666");
}
@Test
void whenRotatingListThreeTimes_thenReturnExpectedList() {
assertEquals(rotateList.rotateRight(listNode, 3)
.toString(), "666->15->3->42");
}
}

View File

@ -0,0 +1,59 @@
package com.baeldung.junit5.runfromjava;
import static org.junit.platform.engine.discovery.ClassNameFilter.includeClassNamePatterns;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectPackage;
import java.io.PrintWriter;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.TestPlan;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
import org.junit.platform.launcher.listeners.TestExecutionSummary;
public class RunJUnit5Tests {
SummaryGeneratingListener listener = new SummaryGeneratingListener();
public void runOne() {
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder
.request()
.selectors(selectClass(RotateListUnitTest.class))
.build();
Launcher launcher = LauncherFactory.create();
TestPlan testPlan = launcher.discover(request);
launcher.registerTestExecutionListeners(listener);
launcher.execute(request);
}
public void runAll() {
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder
.request()
.selectors(selectPackage("com.baeldung.junit5.runfromjava"))
.filters(includeClassNamePatterns(".*Test"))
.build();
Launcher launcher = LauncherFactory.create();
TestPlan testPlan = launcher.discover(request);
launcher.registerTestExecutionListeners(listener);
launcher.execute(request);
}
public static void main(String[] args) {
RunJUnit5Tests runner = new RunJUnit5Tests();
runner.runAll();
TestExecutionSummary summary = runner.listener.getSummary();
summary.printTo(new PrintWriter(System.out));
runner.runOne();
summary = runner.listener.getSummary();
summary.printTo(new PrintWriter(System.out));
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.junit5.runfromjava;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.baeldung.junit.runfromjava.listnode.ListNode;
import com.baeldung.junit.runfromjava.listnode.SwapNodes;
class SwapNodesUnitTest {
private SwapNodes swapNodes;
private ListNode listNode;
@BeforeEach
void setUp() throws Exception {
swapNodes = new SwapNodes();
listNode = new ListNode(42, new ListNode(666, new ListNode(15, new ListNode(3, null))));
}
@Test
void whenSwappingPairs_thenReturnExpectedList() {
assertEquals(swapNodes.swapPairs(listNode)
.toString(), "666->42->3->15");
}
}