0% found this document useful (0 votes)
41 views4 pages

SE Tools Lab 4 JUnit Testing

This document provides instructions for setting up and running a JUnit test of a Calculator class using Eclipse. It describes creating a new project called JunitwithAnt with a src package and test source folder. A Calculator class is created to add two numbers. A JUnit test case is generated for this class to test the add method, asserting that 10 + 5 equals 15. The test is run and results viewed in the JUnit view.

Uploaded by

Hiziki Tare
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)
41 views4 pages

SE Tools Lab 4 JUnit Testing

This document provides instructions for setting up and running a JUnit test of a Calculator class using Eclipse. It describes creating a new project called JunitwithAnt with a src package and test source folder. A Calculator class is created to add two numbers. A JUnit test case is generated for this class to test the add method, asserting that 10 + 5 equals 15. The test is run and results viewed in the JUnit view.

Uploaded by

Hiziki Tare
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/ 4

(SE TOOLS AND PRACTICE) LAB 4, JUNIT TESTING

JUnit Testing
1. Project preparation
 Create a new project called JunitwithAnt.

 Create a new source folder test.


 For this right-click on your project, select Properties and choose Java Build Path. 
Select the Source tab.

 Press the button.

 Afterwards, press the button. Enter test as folder name.

2. Create a Java class


 in the src folder, create the mypackage package and the following class.

1|Page
(SE TOOLS AND PRACTICE) LAB 4, JUNIT TESTING

package myPackage; public

class Calculator {

public int add(int x, int

y) { return x +

y;

1. Create a JUnit test


 Right-click on your new class in the Package Explorer view and select New
JUnit Test Case.
 In the following wizard ensure that the New JUnit 4 test flag is selected and
set the source folder to test, so that your test class gets created in this folder.

 Press the button and select the methods that you want to test.

2|Page
(SE TOOLS AND PRACTICE) LAB 4, JUNIT TESTING

 If the JUnit library is not part of the classpath of your project, Eclipse will
prompt you to add it. Use this to add JUnit to your project.

2. Create a test with the following code.


package mypackage;
import static
org.junit.Assert.*;

import org.junit.Test;

public class CalculatorTest {


Calculator tester=new Calculator();
@Test
public void testAdd() {
assertEquals("10 + 5 must be 15", 15,tester.add(10, 5));
}

3|Page
(SE TOOLS AND PRACTICE) LAB 4, JUNIT TESTING

Run your test in Eclipse


 Right-click on your new test class and select Run-As JUnit Test.

 The results of the tests are displayed in the JUnit view.

4|Page

You might also like