Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9c838f9

Browse files
committed
TDD almost working, build.gradle cleaned up some
1 parent 27fdeac commit 9c838f9

11 files changed

+100
-112
lines changed
File renamed without changes.

build.gradle

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,6 @@ class Tags {
126126
}
127127

128128
ext {
129-
junit4Version = '4.12'
130-
junitVintageVersion = '4.12.0-M2'
131-
junitPlatformVersion = '1.0.0-M2'
132129
junitJupiterVersion = '5.0.0-M2'
133130
}
134131

@@ -148,26 +145,16 @@ subprojects {
148145
}
149146

150147
dependencies {
151-
// compile 'junit:junit:4.12'
148+
// Logging:
152149
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.+'
153150
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.+'
154151
// You can also use the JDK's built-in logging as the back end:
155152
// compile group: 'org.slf4j', name: 'slf4j-jdk14', version: '1.7.5'
156153

157-
// JUnit Jupiter API and TestEngine implementation
158-
/* testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}")
159-
testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}")
160-
161-
// If you also want to support JUnit 3 and JUnit 4 tests
162-
testCompile("junit:junit:${junit4Version}")
163-
testRuntime("org.junit.vintage:junit-vintage-engine:${junitVintageVersion}")*/
164-
compile "org.junit.jupiter:junit-jupiter-api:5.0.0-M2"
165-
testCompile "org.junit.jupiter:junit-jupiter-api:5.0.0-M2"
166-
testRuntime "org.junit.jupiter:junit-jupiter-engine:5.0.0-M2"
167-
/* testCompile "junit:junit:4.12"
168-
testRuntime "org.junit.vintage:junit-vintage-engine:4.12.0-M2"
169-
*//* compile "org.junit.jupiter:junit-jupiter-api:5.0.0-M2"
170-
compile "org.junit.vintage:junit-vintage-engine:4.12.0-M2"*/
154+
// JUnit testing:
155+
compile "org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}"
156+
testCompile "org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}"
157+
testRuntime "org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}"
171158
}
172159

173160
junitPlatform {
@@ -191,11 +178,8 @@ subprojects {
191178
main {
192179
java {
193180
srcDir projectDir
194-
// exclude "*Test.java"
195-
exclude "*Tests.java"
196-
exclude "*JUnit.java"
197-
exclude "StringInverter*.java"
198-
// exclude "Queue.java"
181+
// Remove this when it's working:
182+
exclude "DynamicStringInverterTests.java"
199183
}
200184
resources {
201185
srcDir projectDir
@@ -234,7 +218,7 @@ subprojects {
234218

235219
// Exclude java sources that will not compile
236220
if (tags.compileTimeError) {
237-
println ">>> Excluding " + file.name
221+
// println ">>> Excluding " + file.name
238222
sourceSets.main.java.excludes.add(file.name)
239223
} else {
240224
JavaExec javaTask = null
@@ -362,9 +346,6 @@ configure(subprojects - project(':onjava')) {
362346
compile group: 'com.google.guava', name: 'guava', version: '19.0'
363347
compile "org.openjdk.jmh:jmh-core:${jmh.jmhVersion}"
364348
compile 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M2'
365-
366-
//compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.6.2'
367-
//compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.6.2'
368349
}
369350
}
370351

verifying/CountedListTest.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// Simple use of JUnit to test CountedList.
66
package verifying;
77
import java.util.*;
8-
//import verifying.CountedList;
98
import org.junit.jupiter.api.*;
109
import static org.junit.jupiter.api.Assertions.*;
1110

@@ -43,8 +42,8 @@ public void replace() {
4342
// as it isn't annotated with @Test, it will not
4443
// be automatically executed by JUnit.
4544
private
46-
void compare(ArrayList<String> lst, String[] strs) {
47-
String[] array = (String[])lst.toArray();
45+
void compare(List<String> lst, String[] strs) {
46+
String[] array = lst.toArray(new String[0]);
4847
assertTrue(array.length == strs.length,
4948
"Arrays not the same length");
5049
for(int i = 0; i < array.length; i++)
@@ -72,11 +71,6 @@ public void addAll() {
7271
compare(list, new String[] { "0", "1", "2",
7372
"An", "African", "Swallow" });
7473
}
75-
/* public static void main(String[] args) {
76-
// Invoke JUnit on the class:
77-
org.junit.runner.JUnitCore.runClasses(
78-
SimpleJUnit.class);
79-
} */
8074
}
8175
/* Output:
8276
CountedList #0
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// verifying/DynamicStringInverterTests.java
2+
// (c)2016 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5+
package verifying;
6+
import java.util.*;
7+
import java.util.function.*;
8+
import java.util.stream.*;
9+
import org.junit.jupiter.api.*;
10+
import static org.junit.jupiter.api.Assertions.*;
11+
import static org.junit.jupiter.api.DynamicTest.*;
12+
13+
class DynamicStringInverterTests {
14+
Stream<DynamicTest>
15+
multiple_version_test(Consumer<StringInverter> test) {
16+
List<StringInverter> versions = Arrays.asList(
17+
new StringInverter1(), new StringInverter2(),
18+
new StringInverter3(), new StringInverter4());
19+
return DynamicTest.stream(versions.iterator(),
20+
(version) -> version.getClass().getSimpleName(),
21+
test
22+
);
23+
}
24+
25+
@TestFactory
26+
Stream<DynamicTest> basicInversion_Succeed() {
27+
return multiple_version_test( (version) -> {
28+
String in = "Exit, Pursued by a Bear.";
29+
String out = "eXIT, pURSUED BY A bEAR.";
30+
assertEquals(version.invert(in), out);
31+
});
32+
}
33+
34+
@TestFactory
35+
Stream<DynamicTest> basicInversion_Fail() {
36+
return multiple_version_test( (version) -> {
37+
expectThrows(RuntimeException.class, () -> {
38+
assertEquals(version.invert("X"), "X");
39+
});
40+
});
41+
}
42+
43+
@TestFactory
44+
Stream<DynamicTest> allowedCharacters_Fail() {
45+
return multiple_version_test( (version) -> {
46+
expectThrows(RuntimeException.class, () -> {
47+
version.invert(";-_()*&^%$#@!~`");
48+
version.invert("0123456789");
49+
});
50+
});
51+
}
52+
53+
@TestFactory
54+
Stream<DynamicTest> allowedCharacters_Succeed() {
55+
return multiple_version_test( (version) -> {
56+
version.invert("abcdefghijklmnopqrstuvwxyz ,.");
57+
version.invert("ABCDEFGHIJKLMNOPQRSTUVWXYZ ,.");
58+
});
59+
}
60+
61+
@TestFactory
62+
Stream<DynamicTest> lengthLessThan26_Fail() {
63+
return multiple_version_test( (version) -> {
64+
String str = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
65+
assertTrue(str.length() > 25);
66+
expectThrows(RuntimeException.class, () -> {
67+
version.invert(str);
68+
});
69+
});
70+
}
71+
72+
@TestFactory
73+
Stream<DynamicTest> lengthLessThan26_Succeed() {
74+
return multiple_version_test( (version) -> {
75+
String str = "xxxxxxxxxxxxxxxxxxxxxxxxx";
76+
assertTrue(str.length() < 26);
77+
version.invert(str);
78+
});
79+
}
80+
}

verifying/FirstJUnit5Tests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
import static org.junit.jupiter.api.Assertions.*;
88

99
class FirstJUnit5Tests {
10-
@Test
11-
void myFirstTest() {
12-
assertEquals(2, 1 + 1);
13-
}
10+
@Test
11+
void myFirstTest() {
12+
System.out.println("FirstJUnit5Tests");
13+
assertEquals(2, 1 + 1);
14+
}
1415
}

verifying/StringInverter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5+
package verifying;
56

67
interface StringInverter {
78
public String invert(String str);

verifying/StringInverter1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5-
// {java StringInverterTest StringInverter1}
5+
package verifying;
66

77
public class StringInverter1 implements StringInverter {
88
public String invert(String str) { return ""; }

verifying/StringInverter2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5-
// {java StringInverterTest StringInverter2}
5+
package verifying;
66
import static java.lang.Character.*;
77

88
public class StringInverter2 implements StringInverter {

verifying/StringInverter3.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5-
// {java StringInverterTest StringInverter3}
5+
package verifying;
66
import static java.lang.Character.*;
77

88
public class StringInverter3 implements StringInverter {

verifying/StringInverter4.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5-
// {java StringInverterTest StringInverter4}
5+
package verifying;
66
import static java.lang.Character.*;
77

88
public class StringInverter4 implements StringInverter {

verifying/StringInverterTest.java

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)