Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

WRITING METHODS;

RUNNING UNIT TESTS

CS 1301 – Computer Science I


Announcement

◦Project 1 due tonight


This week
◦ Today: Starting Unit 2 of CS1301
◦ Constructors with parameters
◦ Testing overview / terminology and JUnit
◦ Setter / Mutator methods
◦ Methods that return a value
◦ Activity 04

◦ Next time:
◦ Exceptions, review of preconditions
◦ JUnit tests with Exceptions
◦ Activity 05
◦ Lab 05 goes out

◦ We do not have SI this week


Items for today
◦ Activity 4
◦ Writing Parameterized Constructors
◦ Testing
◦ Writing Methods with Parameters
◦ Writing Methods with Return Values
◦ Writing Methods with Both
WRITING
PARAMETERIZED
CONSTRUCTORS
A Default Constructor
/**
* Creates a default person.
*
* @precondition none
* @postcondition getFirstName().isEmpty() &&
getLastName().isEmpty() && getAge()==0
*/
public Person() {
this.firstName = "";
this.lastName = "";
this.age = 0;
}
Parameterized Constructor
/**
* Creates a Person with the given first and last name, and an age of zero.
*
* @precondition firstName != null && !firstName.isEmpty() && lastName != null
• && !lastName.isEmpty()

* @postcondition getFirstName().equals(firstName) &&


* getLastName.equals(lastName) && getAge()==0
*
* @param firstName
* the person's first name
* @param lastName
* the person's last name
*/
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.age = 0;
}
Using the Parameterized
Constructor defined on the
previous slide…

Write 3 examples:
Testing the parameterized
constructor
TESTING
Testing Terminology
◦ Bug – an error in the code
◦ Debugging – the process of localizing and correcting bugs in
code
◦ Testing – the process of checking that a program is correct

◦ Test-Driven Development (TDD) – write tests before the code to be tested

Create
[test Write Run [no]
New fails] Tests Pass? Debug
Code Tests
Test
[yes]
Testing Terminology
● test: program code whose single responsibility is to
make sure other program code works correctly
○ unit testing is a best practice for software developers

● production code: code implementing the behavior


of the system
○ distributed to clients (as a desktop program, or website,
etc.)
○ is separate from test code

● JUnit: unit testing framework for Java


○ built into Eclipse
○ tests are written in Java
Unit Tests
Unit Testing
◦ Design a set of small tests to collectively test the system
◦ Typically automated

Unit Test
◦ Test the smallest unit of code feasible in a specific use case

Test Suite
◦ Collection of unit tests.

Assertion
◦ Common testing construct (not just Unit Testing)
◦ Condition that is asserted to always be true
◦ Generates an error when the condition fails
◦ Typically provide a descriptive error message
Interpreting a Passing Test

"Green Check": passing test


● Indicates the production
code functions correctly for
that specific test

● Is not an indicator of overall


correctness!

● Remember,
“set of small tests to collectively test the
system”
WRITING
METHODS WITH
PARAMETERS
Setter
A setter is a simple method that sets the value of a field.

/**
* Sets the person's age
*
* @precondition age >= 0
* @postcondition getAge() == age
*
* @param age @param: describes the parameter
* the person's new age required for all parameters
*/
public void setAge(int age) {
this.age = age; body: stores the value in the
} setter's parameter into the field
associated with the setter
Mutator Method
Method that alters the value stored in a field

/**
* Ages the Person by the given number of years.
*
* @precondition years >= 0
* @postcondition getAge() == getAge()@prev + years
*
* @param years
* the number of years to age by
*/
public void growOlderBy(int years) {
this.age = this.age + years;
}
Testing growOlderBy()

● run the TestGrowOlderBy test


● this time it ran several tests
○ each one looks at a different test case:
■ when years=0
■ when years=1
■ when years > 1
■ when called several times in
succession
● The x indicates a failing test
○ more info on how it failed can be found
in the failure trace
○ we'll discuss the failure trace in the
next slide
Interpreting the Failure Trace

Test
Code

expected value vs actual value


Error
Message
File name and line # where
the error came from
Debugging a Method

Why did growOlderBy() give 3 when we


expected 8?
1. look at the code for growOlderBy()
and see if you can figure it out
2. fix the code
3. re-run the test
a. if it passes, we're done
b. if it fails, repeat from step 1
Practice writing a mutator
In the Person class of the Eclipse project, write a method:

● named changeName
● with two parameters:
○ firstName
○ lastName
● that changes the Person's name based on the parameters’
values
● don’t forget about pre- and postconditions

Then uncomment and run the tests in TestChangeName.

Fix any errors that cause failing tests.


WRITING METHODS WITH RETURN
VALUE
Getter Method
◦ Method that provides access to (gets) the value stored in a field
◦ Does not alter the state of the object
◦ i.e., returns the value of the field, but does not change it!

◦ @return Describes the value returned by the method

/**
* Gets the person's last name
*
* @return the person's last name
*/
public String getLastName() {
return this.lastName;
}

/**
* Gets the person's first name
*
* @return the person's first name
*/
public String getFirstName() {
return this.firstName;
}
Example: method that
calculates and returns a value

/**
* Gets the person's initials, as a String.
*
* @return the person's initials, as a String.
*/
public String getInitials() {

return this.firstName.charAt(0) + "." + this.lastName.charAt(0) + ".";

}
Practice writing a method
with a return value
In the Eclipse project on Moodle:

● In the Person class, write a method named getFullName that:


○ returns a String of the person's full name

● Example: if the first name is "Ada" and the last name is


"Lovelace", getFullName should return "Ada Lovelace" (note
the space!)

● Uncomment the tests in the TestGetFullName class

● Your completed method should pass all the tests in


TestGetFullName
WRITING
METHODS WITH
BOTH
i.e., parameters and return value
Example: finding a Person's
birth year
/**
* Finds the person's birth year.
*
* @precondition currentYear > 1970
* @postcondition none
* @return the person's birth year.
*/
public int findBirthYear(int currentYear) {

return currentYear - this.age;

}
Practice writing a method
with parameters and return
In the Person class, write a method:
◦ Name the method getFormalName

◦ Method should accept one parameter, the title

◦ Method should return the title, followed by a space,


followed by the person's last name

Example:
◦ If the title is "Dr." and the Person's full name is ”Doogie Howser", then
this method will return "Dr. Howser”
◦ Uncomment the tests in TestGetFormalName and ensure they pass.

You might also like