Objectives

On completion of this lab you should be able to:

  • Write JUnit Test Cases for the DVD app.

  • Run and interpret the results of JUnit test classes.

Testing the DVD class

In this practical, you will create a test class for DVD.java.

Creating the New Project

  • Create a new project called DVDLibraryV3.1 in Eclipse.

  • Copy the following classes into the project:

  • Create a new folder called lib and copy the XStream jar file into it. Add this jar file to your build path:

Creating DVDTest.java

  • In this section, you may need to refer to your lecture slides for support (i.e. the last slide deck).

  • Create a new source folder called test.

  • Within this folder, create a new JUnit Test Case called DVDTest.

  • You should be prompted to add JUnit4 to your build path. Do this.

  • A DVDTest.java file should be generated for you. Run this file and JUnit should report that you have 1 failure. Delete this generated test:

   @Test
   public void test()
   {
         fail("Not yet implemented");
   }
  • Run DVDTest.java again and JUnit should report no errors.

Adding tests to DVDTest.java

  • Referring to your lecture slides for support, write the following tests:

    • testGetTitle()
    • testConstructors()
    • testSetTitle()
    • testToString()
  • Make sure that you use the setUp() method to setup your test data for each test method.

  • Are all your tests resulting in a green bar? If so, move onto the next step, otherwise revisit the tests and fix them.

Solution for DVDTest.java

Testing the Library class

In this practical, you will create a test class for Library.java (which will also test the XML loading and storing).

Creating LibraryTest.java

  • In this section, you may need to refer to your lecture slides for support (i.e. the fifth slide deck).

  • Within your DVDLibrary3.1 project, inside the test folder, create a new JUnit Test Case called LibraryTest.

  • A LibraryTest.java file should be generated for you. When you run this file, JUnit should report that you have 1 failure. Delete this generated test:

   @Test
   public void test()
   {
         fail("Not yet implemented");
   }
  • Run LibraryTest.java again and JUnit should report no errors.

Adding tests to LibraryTest.java

  • Referring to your lecture slides for support, write the following tests:

    • testConstructor()
    • testAdd()
    • testGetDVDs()
    • testListDVDs()
    • testSaveAndLoad()
    • testIsValidIndex()
  • Make sure that you use the setUp() method to setup your test data for each test method.

  • Are all your tests resulting in a green bar? If so, move onto the next step, otherwise revisit the tests and fix them.

Solution for LibraryTest.java

Solutions

The solution for the overall project can be found here.