Skip to content

Instantly share code, notes, and snippets.

@greghelton
Last active August 15, 2020 00:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greghelton/993da5f5a7f653b06059a4d4dd4e80f7 to your computer and use it in GitHub Desktop.
Save greghelton/993da5f5a7f653b06059a4d4dd4e80f7 to your computer and use it in GitHub Desktop.
I got out the old JUnit book and I tried using VS Code and its Java plugin and I had a myriad of problems. I reverted back to the old DOS command line just to verify that the problem was them, not me.
javac -d bin src/boss/ScoreCollection.java
javac -cp lib\junit-4.13.jar -d bin test/boss/ScoreCollectionTest.java
javac -cp lib\junit-4.13.jar;bin -d bin test/boss/TestRunner.java
java -cp bin;lib/junit-4.13.jar;lib/hamcrest-core-1.3.jar boss.TestRunner
Result:
test(boss.ScoreCollectionTest): Not yet implemented
false
For version 2:
javac -cp bin;lib\junit-4.13.jar;lib\hamcrest-core-1.3.jar -d bin test/boss/ScoreCollectionTest.java
java -cp bin;lib/junit-4.13.jar;lib/hamcrest-core-1.3.jar boss.TestRunner
package boss;
@FunctionalInterface
public interface Scoreable {
int getScore();
}
package boss;
import java.util.*;
public class ScoreCollection {
private List<Scoreable> scores = new ArrayList<>();
public void add(Scoreable scoreable) {
scores.add(scoreable);
}
public int arithmeticMean() {
int total = scores.stream().mapToInt(Scoreable::getScore).sum();
return total / scores.size();
}
}
package boss;
import static org.junit.Assert.*;
import org.junit.*;
public class ScoreCollectionTest {
@Test
public void test() {
fail("Not yet implemented");
}
}
package boss;
import static org.junit.Assert.*;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.*;
public class ScoreCollectionTest {
@Test
public void answersArithmeticMeanOfTwoNumbers() {
ScoreCollection coll = new ScoreCollection();
coll.add(() -> 5);
coll.add(() -> 7);
int actualResult = coll.arithmeticMean();
assertThat(actualResult, equalTo(6));
}
}
package boss;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(ScoreCollectionTest.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment