This course has already ended.

⌛⌛ JUnit tests for course attainment

This task concerns implementing JUnit tests for the Attainment class that represents course attainments. The class is quite similar to that what has been used already in some previous tasks.

Place your JUnit test class into the subdirectory round11\junitattainment\src\test\java\fi\tuni\prog3\junitattainment. You may name your test class by yourself or use the name provided by, for example, NetBeans.

The round11\junitattainment directory of the remote material repository has some test material that is discussed in detail below.

The class Attainment is expected to have the following properties:

  • Belongs into the package fi.tuni.prog3.junitattainment.

  • Implements the interface Comparable<Attainment>.

    • The interface member function compareTo compares primarily student numbers and secondarily course codes. These comparisons are done with the compareTo function of the String class.

  • A public constructor Attainment(String courseCode, String studentNumber, int grade).

    • Initializes an Attainment object with the given course code, student number and grade.

    • Throws an exception of type IllegalArgumentException, if courseCode or studentNumber is null or if grade does not belong to the interval 0–5.

  • Public member functions String getCourseCode(), String getStudentNumber() and int getGrade().

    • Return the values corresponding to their names, that is, the course code, student number and grade.

  • A public member function String toString().

    • Returns a string of form "courseCode studentNumber grade".

Implement JUnit tests that test the following aspects of the class Attainment:

  • When an Attainment object has been constructed, are the values returned by getCourseCode, getStudentNumber and getGrade equal to the values that were provided to the constructor?

  • Does the constructor throw an exception of type IllegalArgumentException, if it receives an illegal parameter?

  • Does the toString function return a string with the expected form?

  • Does the compareTo function work as expected?

Note that this task focuses mainly on implementing tests. You may implement also the class Attainment, which may simplify the creating the tests, but it is not necessary due to the provided Attainment class bytecode (see below), if you test your program from the command window.

Testing

You may use the Attainment class implementations given in the test material subdirectory testfiles. These implementations are provided in the form of readily-compiled class files. The subdirectory testfiles\0 contains a working version, and the other subdirectories contain versions that fail the JUnit tests described above.

Although you need to place your code in a Maven directory structure, you do not need to use Maven for compiling and testing the code. The task material provides a JUnit JAR library junit-platform-console-standalone-1.8.2.jar. If you copy it into your task root directory and one of the readily-provided Attainment class files into the subdirectory target\classes\fi\tuni\prog3\junitattainment, your JUnit tests can be compiled, for example, as javac -cp target/classes;junit-platform-console-standalone-1.8.2.jar -d target/test-classes -sourcepath src/test/java src/test/java/fi/tuni/prog3/junitattainment/*.java. The JUnit tests can then be run, for example, as java -jar junit-platform-console-standalone-1.8.2.jar -cp target/classes/;target/test-classes -p fi.tuni.prog3.junitattainment. (Mac and Linux users: Use colon : instead of the semicolon ; in the previous commands.) You may switch the tested Attainment class version by simply copying a different class file into the subdirectory target\classes\fi\tuni\prog3\junitattainment.

The automated tests are based on the same readily provided Attainment class implementations. You will receive full points, if your JUnit tests find no errors from the implementation in testfiles\0, but find errors from all other provided implementations.

A+ presents the exercise submission form here.

Posting submission...