Docsity

JUnit: A Testing Framework for Java - Setting Up, Basics, and Special Cases, Exams of Computer Science

Partial preview of the text

Download JUnit: A Testing Framework for Java - Setting Up, Basics, and Special Cases and more Exams Computer Science in PDF only on Docsity!

1

CS162: Introduction to

Computer Science II

JUnit

2

JUnit Version

NOTE: We are using JUnit 4.1. If you are

using JUnit 3.8, the material on these

slides will not work.

3

Outline

1. Setting up JUnit

2. The basics: @Test, asserts, test

fixtures

3. Special tags: Exceptions, @Ignore

4. Running Tests

4

1. Setting up JUnit

What is JUnit?

• A tool to help design and organize test

cases

• Design a companion test class for each

class you develop

• If your class is called MyClass, you call

the companion test class MyClassTest

7

1. Setting up JUnit

  • If you are having problems using JUnit from Eclipse,

you probably need to include it in your project’s build

path

  • How to do this:
    • Right click on your project in the Package Explorer
    • Select properties
    • Select Java Build Path
    • Click on the libraries tab
    • Click on Add External Jars
    • Go to your Eclipse directory
    • Go to the plugins subdirectory
    • Go to org.junit4_4.1.0.1 (or something similar)
    • Select junit-4.1.jar

8

1. Setting up JUnit

1. Create a JUnit test case:

• Click on the package you want to add the

test case to

• Go to File → New → JUnit Test Case

• Give it a name, hit finish

2. Notice that it looks just like a java class.

3. Add the line import org.junit.*

4. Add the line import static org.junit.Assert.*

9

2. The Basics: @Test

To add test functions, all you have to do is put a @Test

in front of the method. Test functions are special

as they are run automatically by JUnit.

import org.junit.Test;

public class TreasureChestTest { @Test public void addGold() { ... } }

10

2. The Basics: Asserts

The recipe:

1. Write the code which runs the function

you are testing

2. Check the results using the asserts

provided by JUnit

13

2. The Basics: Asserts

import org.junit.Test;

public class TreasureChestTest { @Test public void addGold() { TreasureChest tc = new TreasureChest(10); tc.addGold(10); assertEquals(20,tc.checkGold()); } }

A simple test case example:

14

2. The Basics: Test Fixtures

@Test public void addGold() { TreasureChest tc = new TreasureChest(10); tc.addGold(10); assertEquals(20,tc.checkGold()); }

@Test public void removeGold() { TreasureChest tc = new TreasureChest(10); tc.removeGold(10); assertEquals (0,tc.checkGold()); }

Notice that we need to set up a TreasureChest object in

each test. You could repeat the code as above but there

is another alternative...

15

2. The Basics: Test Fixtures

• Sometimes, you may have 2 or more test

functions that use the same set of objects

• This set of objects is called a test fixture

• To use a test fixture:

1. Create a member variable for each object in the

test fixture

2. Write a method to set up the test fixture member

variables and use the @Before tag

3. Write a method to clean up the test fixture

member variables (if necessary) and use the

@After tag

16

2. The Basics: Test Fixtures

Note:

• Methods tagged with @Before are

called before every test method

• Methods tagged with @After are called

after every test method

• Why? Ensure no side effects between

test function runs

19

3. Special Cases: Exceptions

@Test (expected=RuntimeException. class )

public void addGoldException() {

TreasureChest tc = new TreasureChest();

tc.addGold(-10);

assertEquals (10,tc.checkGold());

Notice that we declare what exception we expect to be thrown

20

3. Special Cases: Ignore

@Ignore(“Not running because ”) @Test (expected=RuntimeException. class ) public void addGoldException() { TreasureChest tc = new TreasureChest(); tc.addGold(-10); assertEquals (10,tc.checkGold()); }

Sometimes, you need to ignore a test.

  • To do so, use the @Ignore flag.
  • You will need to import org.junit.Ignore.
  • If you ignore a test, make sure you include a message to say

why you are ignoring it.

21

4. Running Tests

  • Right-click on your

TestCase in Eclipse’s

Package Explorer

  • Select Run As →

JUnit Test

  • After running it, you

will see a result like

that shown on the left

  • All green means all

your tests passed

22

4. Running Tests: Test Suites

• You can combine TestCases into

TestSuites

• You can also combine TestSuites into

other TestSuites to get a hierarchy

• Lets you run all your tests or you can

run a subset of them

Note: There is an option in Eclipse’s to

create a Test Suite file. Do not use it. It

is from the old version of JUnit.

25

4. Running Tests: Test Suites

  • You run a test suite just like

you would run a regular JUnit

test

  • Notice that all the tests in

TreasureChestTest and

PirateTest have been run