Close
Register
Close Window

Masters of Engineering Bridge Course

Chapter 7 Testing and Exceptions

Show Source |    | About   «  6.6. Static, Main, and Exceptions   ::   Contents   ::   7.2. More on Exceptions  »

7.1. Revisit Java Unit Testing

7.1.1. Objectives

Upon completion of this module, students will be able to:

  • Review the basics of a java class including fields, constructors, methods, parameters, and use of the keyword this

  • Review debugging code and code coverage

  • Variations for JUnit assert statements

7.1.2. Introduction to Hokie Class

In this discussion we will be revisiting good testing practices with an example class called “Hoakie Class”.

7.1.2.1. [6:07] Introduction to Hokie Class Video

7.1.2.2. Code Example

Try It Yourself

In Eclipse, use the Project > Download Assignment… menu command to download the lab project named “ex7.1.2.2-BuggyHokie”.

Refer to 01.02: Lab: LightBot for Beginners if you need to review the instructions for downloading Eclipse projects.

7.1.3. Checkpoint 1

7.1.4. Intro to Hokie Class JUnit Testing

7.1.4.1. A Note about Assert Statements

So far in the course when we want to test that a piece of code acted the way we wanted, we’d run a statement like:

assertThat(<something we want to check>).isEqualTo(<expected value>);

This is a more modern style that’s intended to be more readable. However, there is a different form of syntax you can use to create assertions:

assertEquals(<expected value>, <something we want to check>);

This second kind of assert statement is more commonly used today, but it can be tricky to use correctly. When using asserEquals, it can be easy to put the value we want to check first and the expected value second.

For example, say we wanted to check that a variable x was equal to 5.

int x = 4;
assertEquals(x, 5);

Writing like this would be syntactically correct, but potentially confusing because the failure message would read “Expected [4] but got [5]”. In reality, we were expecting 5 but got 4.

Videos in the second half of the course will be using this second, more commonly used syntax. You can continue to use either version. Below, is a table of assertions in both styles.

Assertions

Task

Newer Assertion Style

Older Assertion Style

Notes

Checking that x is equal to 5

assertThat(x).isEqualTo(5);

assertEquals(5, x);

While the new style has a .isNotEqualTo(), there is no assertNotEquals() in the old style

Check that a double x is equal to double y

assertThat(x).isEqualTo(y, within(0.01));

assertEquals(y, x, 0.01);

Checking that x is true

assertThat(x).isTrue();

assertTrue(x);

Checking that x is false

assertThat(x).isFalse();

assertTrue(x);

Checking that x is null

assertThat(x).isNull();

assertNull(x);

Checking that x is not null

assertThat(x).isNotNull();

assertNotNull(x);

Checking two object variables refer to the same space in memory

assertThat(obj1).isSameAs(obj2);

assertSame(obj2, ob1);

7.1.4.2. [19:37] Hokie Class JUnit Testing Video

Video Slides 7.1.4.1-JavaUnitTesting.pdf

7.1.5. Checkpoint 2

7.1.6. Review of Writing JUnit Tests with student.TestCase

7.1.6.1. Additional reference for writing Junit Tests:

Writing Junit Tests With Student TestCase

A Whirlwind Introduction to JUnit

7.1.7. Checkpoint 3

   «  6.6. Static, Main, and Exceptions   ::   Contents   ::   7.2. More on Exceptions  »

nsf
Close Window