Tt
Click this widget to change the font size.
CC
Click this widget to change contrast.

Home Page 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | Links | Search | Bio |

Guide to Object-oriented Programming With Java (Buffalo State University Version)


Chapter 13: JUnit Testing and Using the Debug Tool

Testing an Application Using Junit

The eclipse IDE allows you to use Junit to create unit tests to test individual sections of yourr Java code.

A DistanceConverter.java Example

Before you can write any unit tests you need to create the application to be tested. For example, DistanceConverter.java

package edu.buffalostate.cis425.sp24.put-your-username-here;

public class DistanceConverter {

  public static double convertFeettoMeters(double feet) {
      return feet * 0.3048;
  }

  public static double convertMeterstoFeet(double meters) {
      return meters * 3.2808;
  }

  public static void main(String[] args) {
    System.out.println("1 foot = " + convertFeettoMeters(1) + " meters\n");
  }
}

Figure 13-1: DistanceConverter.java Code
Console
1 foot = 0.3048 meters

Figure 13-2: DistanceConverter.java Output

Creating the Junit Test Template

Use the following steps to create a template Junit test for your DistanceConverter.java file.

  1. Right click on your DistanceConverter.java File
  2. Choose: New -> Junit Test Case
  3. In the Name: field, type: DistanceConverterTest.java
  4. Click the Finish button
    Note: The first time you use Junit you may need to click the link to setup the Junit 4 library in your project properties.
  5. You should now see the template in the edtor window


Figure 13-3: Using java.io.util.Scanner to Read a File Code
package edu.buffalostate.cis425.sp24.put-your-username-here;

import static org.junit.Assert.*;
import org.junit.Test;

public class DistanceConverterTestEmpty {
  @Test
  public void test() {
    fail("Not yet implemented");
  }
}

Figure 13-4: Using java.io.util.BufferedReader to Read a File Output

Adding Test Cases

Now you are ready to add some test cases to the DistanceConverterTest.java file:

package edu.buffalostate.cis425.sp24.put-your-username-here;

import static org.junit.Assert.*;
import org.junit.Test;

public class DistanceConverterTestEmpty {

  double feet, meters;

  @Test
  public void testConvertFeettoMeters() {
    feet = 2;
    meters = DistanceConverter.convertFeettoMeters(meters);
    assertEquals(meters, 0.3048, 0.001);
  }

  @Test
  public void testConvertMeterstoFeet() {
    meters = 2;
    meters = DistanceConverter.convertMeterstoFeet(feet);
    assertEquals(3.281,feet, 0.001);
  }
}

Figure 13-5: Adding Test Cases to DistanceConverterTest.java Code

Now run your DistanceConverterTest.java file to produce the following window.

Console
ConsoleConsole edu.buffalostate.cis425.sp24.put-your-username-here.DistanceConverterTest
    ConsoletestConvertFeettoMeters(edu.buffalostate.cis425.sp24.put-your-username-here.DistanceConverterTest)
    ConsoletestConvertFeettoMeters(edu.buffalostate.cis425.sp24.put-your-username-here.DistanceConverterTest)

Figure 13-6: Test Cases DistanceConverterTest.java Output

Note: the above example has a test failure because the test case for convertFeettoMeters() is testing for feet to be a 2 but we assert (expects) it to be a 1.

Using the Eclipse Debug Tool

Eclipse has a built-in debug tool that provides a way to walk through your code and observe values for your variables and objects. You can set breakpoints so that when you run your code Eclipse will stop at the next breakpoint. This is a handly tool to determine if your variables have a value that you expect them to have and to help figure out why you may be getting unexpected results.

  1. Set a breakpoint:
    1. Right-click on the line number in your code
    2. Choose: Toggle Breakpoint
  2. Run your program in debug mode:
    1. In the Toolbar, click the debug Debug icon
      The Debug Perspective (see Figure 13-8) will open

Figure 13-7: Steps for using the Debug Tool
  1. The Debug View (top left window) shows your code structure (call stack)
  2. The Variables/Expressions View (top right window) shows your variables and expressions values
  3. The Breakpoints View (top right window) shows your breakpoints
  4. The Display View (middle left window) shows your code
  5. The Outline View (middle right window) shows your program structure
  6. The Console View (bottom window) shows your program output
debug


Figure 13-8: Debug Perspective
debug
skip The Skip Breakpoints icon (Ctrl-Alt-B) allows you to skip over all breakpoints
resume The Resume icon (F8) allows you to skip over all breakpoints
suspend The Suspend icon allows you to skip over all breakpoints
terminate The Terminate icon (F2) allows you to skip over all breakpoints
disconnect The Disconnect icon allows you to skip over all breakpoints
step into The Step Into icon (F5) allows you to skip over all breakpoints
step over The Step Over icon (F6) allows you to skip over all breakpoints
step return The Step Return icon (F7) allows you to skip over all breakpoints


Figure 13-9: The Debug Toolbar

Help contribute to my OER Resources. Donate with PayPal button via my PayPal account.
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2016-2024 Jim Gerland