Friday, August 14, 2020

JUnit & DOS

When I tried coding this in VS Code, the plug-in did not recognize the system JDK so I reverted to DOS just because.

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
view raw Commands hosted with ❤ by GitHub
package boss;
@FunctionalInterface
public interface Scoreable {
int getScore();
}
view raw Scoreable.java hosted with ❤ by GitHub
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());
}
}
view raw TestRunner.java hosted with ❤ by GitHub