Untitled

<aside> <img src="/icons/preview_gray.svg" alt="/icons/preview_gray.svg" width="40px" /> Our past TA has recorded a super helpful walkthrough if you prefer a video of these steps! You may need to login with your USF username and password for access.

</aside>

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


Understanding Tests

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.