TDD almost working, build.gradle cleaned up some

This commit is contained in:
Bruce Eckel 2016-08-27 10:12:56 -06:00
parent 27fdeacf4c
commit 9c838f918b
11 changed files with 100 additions and 112 deletions

View File

@ -126,9 +126,6 @@ class Tags {
} }
ext { ext {
junit4Version = '4.12'
junitVintageVersion = '4.12.0-M2'
junitPlatformVersion = '1.0.0-M2'
junitJupiterVersion = '5.0.0-M2' junitJupiterVersion = '5.0.0-M2'
} }
@ -148,26 +145,16 @@ subprojects {
} }
dependencies { dependencies {
// compile 'junit:junit:4.12' // Logging:
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.+' compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.+'
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.+' compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.+'
// You can also use the JDK's built-in logging as the back end: // You can also use the JDK's built-in logging as the back end:
// compile group: 'org.slf4j', name: 'slf4j-jdk14', version: '1.7.5' // compile group: 'org.slf4j', name: 'slf4j-jdk14', version: '1.7.5'
// JUnit Jupiter API and TestEngine implementation // JUnit testing:
/* testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}") compile "org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}"
testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}") testCompile "org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}"
testRuntime "org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}"
// If you also want to support JUnit 3 and JUnit 4 tests
testCompile("junit:junit:${junit4Version}")
testRuntime("org.junit.vintage:junit-vintage-engine:${junitVintageVersion}")*/
compile "org.junit.jupiter:junit-jupiter-api:5.0.0-M2"
testCompile "org.junit.jupiter:junit-jupiter-api:5.0.0-M2"
testRuntime "org.junit.jupiter:junit-jupiter-engine:5.0.0-M2"
/* testCompile "junit:junit:4.12"
testRuntime "org.junit.vintage:junit-vintage-engine:4.12.0-M2"
*//* compile "org.junit.jupiter:junit-jupiter-api:5.0.0-M2"
compile "org.junit.vintage:junit-vintage-engine:4.12.0-M2"*/
} }
junitPlatform { junitPlatform {
@ -191,11 +178,8 @@ subprojects {
main { main {
java { java {
srcDir projectDir srcDir projectDir
// exclude "*Test.java" // Remove this when it's working:
exclude "*Tests.java" exclude "DynamicStringInverterTests.java"
exclude "*JUnit.java"
exclude "StringInverter*.java"
// exclude "Queue.java"
} }
resources { resources {
srcDir projectDir srcDir projectDir
@ -234,7 +218,7 @@ subprojects {
// Exclude java sources that will not compile // Exclude java sources that will not compile
if (tags.compileTimeError) { if (tags.compileTimeError) {
println ">>> Excluding " + file.name // println ">>> Excluding " + file.name
sourceSets.main.java.excludes.add(file.name) sourceSets.main.java.excludes.add(file.name)
} else { } else {
JavaExec javaTask = null JavaExec javaTask = null
@ -362,9 +346,6 @@ configure(subprojects - project(':onjava')) {
compile group: 'com.google.guava', name: 'guava', version: '19.0' compile group: 'com.google.guava', name: 'guava', version: '19.0'
compile "org.openjdk.jmh:jmh-core:${jmh.jmhVersion}" compile "org.openjdk.jmh:jmh-core:${jmh.jmhVersion}"
compile 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M2' compile 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M2'
//compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.6.2'
//compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.6.2'
} }
} }

View File

@ -5,7 +5,6 @@
// Simple use of JUnit to test CountedList. // Simple use of JUnit to test CountedList.
package verifying; package verifying;
import java.util.*; import java.util.*;
//import verifying.CountedList;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@ -43,8 +42,8 @@ public class CountedListTest {
// as it isn't annotated with @Test, it will not // as it isn't annotated with @Test, it will not
// be automatically executed by JUnit. // be automatically executed by JUnit.
private private
void compare(ArrayList<String> lst, String[] strs) { void compare(List<String> lst, String[] strs) {
String[] array = (String[])lst.toArray(); String[] array = lst.toArray(new String[0]);
assertTrue(array.length == strs.length, assertTrue(array.length == strs.length,
"Arrays not the same length"); "Arrays not the same length");
for(int i = 0; i < array.length; i++) for(int i = 0; i < array.length; i++)
@ -72,11 +71,6 @@ public class CountedListTest {
compare(list, new String[] { "0", "1", "2", compare(list, new String[] { "0", "1", "2",
"An", "African", "Swallow" }); "An", "African", "Swallow" });
} }
/* public static void main(String[] args) {
// Invoke JUnit on the class:
org.junit.runner.JUnitCore.runClasses(
SimpleJUnit.class);
} */
} }
/* Output: /* Output:
CountedList #0 CountedList #0

View File

@ -0,0 +1,80 @@
// verifying/DynamicStringInverterTests.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
package verifying;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.DynamicTest.*;
class DynamicStringInverterTests {
Stream<DynamicTest>
multiple_version_test(Consumer<StringInverter> test) {
List<StringInverter> versions = Arrays.asList(
new StringInverter1(), new StringInverter2(),
new StringInverter3(), new StringInverter4());
return DynamicTest.stream(versions.iterator(),
(version) -> version.getClass().getSimpleName(),
test
);
}
@TestFactory
Stream<DynamicTest> basicInversion_Succeed() {
return multiple_version_test( (version) -> {
String in = "Exit, Pursued by a Bear.";
String out = "eXIT, pURSUED BY A bEAR.";
assertEquals(version.invert(in), out);
});
}
@TestFactory
Stream<DynamicTest> basicInversion_Fail() {
return multiple_version_test( (version) -> {
expectThrows(RuntimeException.class, () -> {
assertEquals(version.invert("X"), "X");
});
});
}
@TestFactory
Stream<DynamicTest> allowedCharacters_Fail() {
return multiple_version_test( (version) -> {
expectThrows(RuntimeException.class, () -> {
version.invert(";-_()*&^%$#@!~`");
version.invert("0123456789");
});
});
}
@TestFactory
Stream<DynamicTest> allowedCharacters_Succeed() {
return multiple_version_test( (version) -> {
version.invert("abcdefghijklmnopqrstuvwxyz ,.");
version.invert("ABCDEFGHIJKLMNOPQRSTUVWXYZ ,.");
});
}
@TestFactory
Stream<DynamicTest> lengthLessThan26_Fail() {
return multiple_version_test( (version) -> {
String str = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
assertTrue(str.length() > 25);
expectThrows(RuntimeException.class, () -> {
version.invert(str);
});
});
}
@TestFactory
Stream<DynamicTest> lengthLessThan26_Succeed() {
return multiple_version_test( (version) -> {
String str = "xxxxxxxxxxxxxxxxxxxxxxxxx";
assertTrue(str.length() < 26);
version.invert(str);
});
}
}

View File

@ -7,8 +7,9 @@ import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class FirstJUnit5Tests { class FirstJUnit5Tests {
@Test @Test
void myFirstTest() { void myFirstTest() {
assertEquals(2, 1 + 1); System.out.println("FirstJUnit5Tests");
} assertEquals(2, 1 + 1);
}
} }

View File

@ -2,6 +2,7 @@
// (c)2016 MindView LLC: see Copyright.txt // (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information. // Visit http://mindviewinc.com/Books/OnJava/ for more book information.
package verifying;
interface StringInverter { interface StringInverter {
public String invert(String str); public String invert(String str);

View File

@ -2,7 +2,7 @@
// (c)2016 MindView LLC: see Copyright.txt // (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information. // Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// {java StringInverterTest StringInverter1} package verifying;
public class StringInverter1 implements StringInverter { public class StringInverter1 implements StringInverter {
public String invert(String str) { return ""; } public String invert(String str) { return ""; }

View File

@ -2,7 +2,7 @@
// (c)2016 MindView LLC: see Copyright.txt // (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information. // Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// {java StringInverterTest StringInverter2} package verifying;
import static java.lang.Character.*; import static java.lang.Character.*;
public class StringInverter2 implements StringInverter { public class StringInverter2 implements StringInverter {

View File

@ -2,7 +2,7 @@
// (c)2016 MindView LLC: see Copyright.txt // (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information. // Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// {java StringInverterTest StringInverter3} package verifying;
import static java.lang.Character.*; import static java.lang.Character.*;
public class StringInverter3 implements StringInverter { public class StringInverter3 implements StringInverter {

View File

@ -2,7 +2,7 @@
// (c)2016 MindView LLC: see Copyright.txt // (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information. // Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// {java StringInverterTest StringInverter4} package verifying;
import static java.lang.Character.*; import static java.lang.Character.*;
public class StringInverter4 implements StringInverter { public class StringInverter4 implements StringInverter {

View File

@ -1,69 +0,0 @@
// verifying/StringInverterTest.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// {ValidateByHand} // Don't run by itself
import java.util.*;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
// import org.junit.platform.runner.JUnitPlatform;
public class StringInverterTest {
static StringInverter inverter;
@Test
public final void basicInversion_Succeed() {
String in = "Exit, Pursued by a Bear.";
String out = "eXIT, pURSUED BY A bEAR.";
assertEquals(inverter.invert(in), out);
}
@Test
public final void basicInversion_Fail() {
expectThrows(RuntimeException.class, () -> {
assertEquals(inverter.invert("X"), "X");
});
}
@Test
public final void allowedCharacters_Fail() {
expectThrows(RuntimeException.class, () -> {
inverter.invert(";-_()*&^%$#@!~`");
inverter.invert("0123456789");
});
}
@Test
public final void allowedCharacters_Succeed() {
inverter.invert("abcdefghijklmnopqrstuvwxyz ,.");
inverter.invert("ABCDEFGHIJKLMNOPQRSTUVWXYZ ,.");
}
@Test
public final void lengthLessThan26_Fail() {
String str = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
assertTrue(str.length() > 25);
expectThrows(RuntimeException.class, () -> {
inverter.invert(str);
});
}
@Test
public final void lengthLessThan26_Succeed() {
String str = "xxxxxxxxxxxxxxxxxxxxxxxxx";
assertTrue(str.length() < 26);
inverter.invert(str);
}
/*
public static void main(String[] args) throws Exception{
assertEquals(args.length, 1);
inverter = (StringInverter)
Class.forName(args[0]).newInstance();
Result result = org.junit.runner.JUnitCore.runClasses(
StringInverterTest.class);
List<Failure> failures = result.getFailures();
System.out.printf("%s has %d FAILURES:\n",
args[0], failures.size());
int count = 1;
for(Failure f : failures) {
System.out.printf("Failure %d:\n", count++);
System.out.println(f.getDescription());
System.out.println(f.getMessage());
}
}
*/
}