Every homework comes with a src
» test
» java
directory with JUnit 5 test code. You must pass all of these tests remotely to earn full credit, but should learn how to run and understand the tests locally first.
TABLE OF CONTENTS
Each homework will have a class called HomeworkTest.java
where Homework
is the name of the homework assignment. For example, for the ArgumentParser
homework, its associated test class is ArgumentParserTest.java
.
The homework tests use the Junit 5 testing framework, which has built-in support in Eclipse. The tests themselves are Java code. There are always several parts of a test: the test case (method being tested and on which input), the actual result of running that method, and the expected result.
For example, consider this test:
/**
* Tests values that should be considered valid flags.
*
* @param flag valid flag value
*/
@Order(1)
@ParameterizedTest(name = "[{index}: \\"{0}\\"]")
@ValueSource(strings = {
"-a", "-hello", "-hello world", "-trailing ", "-résumé", "-über",
"-abc123", "-with-dash", "-with_underscore", "-@debug", "-#admin",
"--quiet", })
public void testValidFlags(String flag) {
boolean actual = ArgumentParser.isFlag(flag);
Assertions.assertTrue(actual, flag);
}
The method being tested is the isFlag
method in ArgumentParser
, given by this line:
boolean actual = ArgumentParser.isFlag(flag);
The method is being tested multiple times, once for each input in the @ValueSource
here:
@ValueSource(strings = {
"-a", "-hello", "-hello world", "-trailing ", "-résumé", "-über",
"-abc123", "-with-dash", "-with_underscore", "-@debug", "-#admin",
"--quiet", })
For each of these input values for the isFlag
method, the test is expecting the method to return true
each time:
Assertions.assertTrue(actual, flag);
If we consider the first input value, then the code asserts ArgumentParser.isFlag("-a")
should return true
. If it does, the test passes. If not, an exception is thrown.
Each method might look slightly different, but it will always have those same three components: what is being tested, what is the actual value, and what is the expected value. If you ever have trouble understanding a test, reach out on CampusWire!
You should try passing the tests locally first. Before you can do that, you need to follow the Homework Setup guide to setup the your repository using GitHub classroom and then import that repository as a Java Project in Eclipse.