⌛⌛ JUnit tests for course attainment

Place your code into the subdirectory Round11/junitattainment/src/test/java/fi/tuni/prog3/junitattainment. Fetch task material from student_template_project.

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

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, ie. the course code, student number and grade.

  • A public member function String toString().

    • Returns a string of form “courseCode studentNumber grade” (note that now there is no newline).

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 toString return a string with the expected form?

  • Does compareTo work as expected?

Note that this task focuses mainly on implementing tests. You may implement also the class Attainment (it might simplify creating the tests), but it is not necessary due to the provided Attainment class implementations (see below).


You may use the Attainment class implementations given in the test material subdirectory testfiles. Theese 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 some readily provided Attainment class file into the subdirectory target/classes/fi/tuni/prog3/junitattainment, your JUnit tests can be compiled e.g. 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 e.g. as java -jar junit-platform-console-standalone-1.8.2.jar -cp target/classes/:target/test-classes -p fi.tuni.prog3.junitattainment. 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...