0% found this document useful (0 votes)
47 views2 pages

JUNIT Testing

This document describes how to create and write JUnit tests in Java. It explains that to create a JUnit test file, you select "File → New → JUnit Test Case" and give it a class name that extends TestCase. This sets up test inheritance. To write tests, define public void methods starting with "test" that use assertions like assertTrue and assertEquals to validate outcomes. The tests can call other private methods for support.

Uploaded by

Allan Song
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views2 pages

JUNIT Testing

This document describes how to create and write JUnit tests in Java. It explains that to create a JUnit test file, you select "File → New → JUnit Test Case" and give it a class name that extends TestCase. This sets up test inheritance. To write tests, define public void methods starting with "test" that use assertions like assertTrue and assertEquals to validate outcomes. The tests can call other private methods for support.

Uploaded by

Allan Song
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

C Cr re ea at ti in ng g J JU Un ni it t T Te es st ts s

So far you have run JUnit tests. Lets know describe how to write them.

Create a JUnit .java test file as follows:

FileNew JUnit Test Case

If the JUnit library is not part of the project then select Click Here at
the bottom of the screen

Pick a class name (e.g., Myclass or any name you prefer)

At this point you will see a class declaration that looks as follows:
public class Myclass extends TestCase { }

The class declaration defined when we create a JUnit test file represents an
inheritance example. TestCase is a class that defines methods supporting the
implementation of JUnit tests, and those methods are now available in Myclass.

W Wr ri it ti in ng g J JU Un ni it t T Te es st ts s

To create tests define public void methods that start with the word test (e.g.,
testEqualsMethod, testNumberOne, testNumberTwo, etc.). Each of these methods
represents a test.

Inside of a test method you can have typical Java code where assertions are used to
specify the expected results from the test.

The following are common assertions we use in JUnit tests:
assertTrue verifies that the argument expression is true. If the argument
expression is false the test fails. Otherwise execution is successful.
assertEquals takes two arguments (expected value and actual value). If
the values are not equal the test fails. Otherwise execution is successful.
In Eclipse type this.assert (and then wait) and you will see list of the assert
methods available

You can define auxiliary methods (e.g. private methods) that support the set of
tests you are developing.

You can have multiple assertions in a JUnit test. Once the first one fails the whole
test is considered to had failed.

Pick descriptive names for your test methods.
W Wr ri it ti in ng g J JU Un ni it t T Te es st ts s ( (E Ex xa am mp pl le e) )



public class AuxMath {
public static int maximum(int x, int y) {
if (x > y)
return x;
return y;
}

public static int minimum(int x, int y) {
if (x < y)
return x;
return y;
}
}

import junit.framework.TestCase;

public class JUnitTestExample extends TestCase {
public void testOneMaximum() {
int expectedResults = 20;
assertEquals(expectedResults, AuxMath.maximum(10, 20));
}

public void testTwoMinimum() {
int expectedResults = 5;
assertEquals(expectedResults, AuxMath.minimum(30, 5));
}
}

You might also like