Chapter:Classes - Testing your classes - Exercises

From Juneday education
Jump to: navigation, search

Exercises on testing your classes

This exercise aims to make you familiar with how to write test code to test your classes.

You should Write test code to test the Passport class below

package org.police.passportsystem;

public class Passport {

  private String  name;
  private String  birth;

  public Passport(String name, String birth) {
    this.name  = name;
    this.birth = birth;
  }

  public String toString() {
    return name + " " + birth;
  }

  public String name() {
    return name;
  }

  public String birth() {
    return birth;
  }
}


Q1

Create a class PassportTest in the package org.police.passportsystem.test The class should have a main method.

Expand using link to the right to see a possible solution/answer.

Suggested solution

1 package org.police.passportsystem.test;
2 
3 public class PassportTest {
4 
5     public static void main(String[] args) {
6 
7 
8     }
9 }

To compile and execute the tests: javac org/police/passportsystem/test/PassportTest.java && java -ea org.police.passportsystem.test.PassportTest

Q2

Add, to the main method in the test class above, code to create a passport object with the following arguments "Ada Lovelace" "1815-12-10". It really seems a bit odd to create a passport for someone who died many years ago but we couldn't resist to give a bit of history lesson. Make sure that the name and birth is stored correctly in the object.

Hint: you could check that the name in the Passport object has the same content as the text you passed as argument. Do the same with birth. It is easier to do this if you save the arguments in two String reference variables. You may use assert to do this.

Expand using link to the right to see a possible solution/answer.

Suggested solution

 1 package org.police.passportsystem.test;
 2 
 3 import org.police.passportsystem.Passport;
 4 
 5 public class PassportTest {
 6 
 7   public static void main(String[] args) {
 8 
 9     String name  = "Ada Lovelace";
10     String birth = "1815-12-10";
11     Passport p        = new Passport(name, birth);
12 
13     assert p.name().equals(name)   : "Wrong name";
14     assert p.birth().equals(birth) : "Wrong birth date string";
15   }
16 }

To compile and execute the tests: javac org/police/passportsystem/test/PassportTest.java && java -ea org.police.passportsystem.test.PassportTest

Q3

Verify (or test) that the toString method returns the correct string.

Hint: create a string yourself with the expected result and compare that string with the actual string returned from the method.

Expand using link to the right to see a possible solution/answer.

Suggested solution

 1 package org.police.passportsystem.test;
 2 
 3 import org.police.passportsystem.Passport;
 4 
 5 public class PassportTest {
 6 
 7   public static void main(String[] args) {
 8 
 9     String name  = "Ada Lovelace";
10     String birth = "1815-12-10";
11     Passport p        = new Passport(name, birth);
12     String expected   = name + " " + birth;
13 
14     assert p.name().equals(name)   : "Wrong name";
15     assert p.birth().equals(birth) : "Wrong birth date string";
16     assert p.toString().equals(expected) : "Wrong toString() format";
17   }
18 }

To compile and execute the tests: javac org/police/passportsystem/test/PassportTest.java && java -ea org.police.passportsystem.test.PassportTest

Links

Further reading

Where to go next

Next page is: Classes_-_Using_your_classes

« PreviousBook TOCNext »