Overview of Eclipse Lectures - Nc State...

45
Overview of Eclipse Lectures 1. Overview 2. Installing and Running 3. Building and Running Java Classes 4. Debugging “Lecture 0” 1 CSC/ECE 517 Fall 2009 Lec. 2 4. Debugging 5. Testing with JUnit 6. Refactoring 7. Version Control with CVS Lecture 2 Lecture 3

Transcript of Overview of Eclipse Lectures - Nc State...

Page 1: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Overview of Eclipse Lectures

1. Overview

2. Installing and Running

3. Building and Running Java Classes

4. Debugging

“Lecture 0”

1CSC/ECE 517 Fall 2009 Lec. 2

4. Debugging

5. Testing with JUnit

6. Refactoring

7. Version Control with CVS

Lecture 2

Lecture 3

Page 2: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Module Road Map

1. Overview2. Installing and Running

3. Building and Running Java Classes

4. Debugging� Debug Perspective� Debug Session

2CSC/ECE 517 Fall 2009 Lec. 2

� Debug Session� Breakpoint� Debug Views� Breakpoint Types� Evaluating and Displaying Expressions

5. Testing with JUnit6. Refactoring7. Version Control with CVS

Page 3: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Debugging » Debugging in Eclipse

� The Java Debugger� Part of Eclipse Java Development Tools (JDT)

� More than System.out.printn( error

)

� Detects errors as code executes

� Correct errors as code executes

3CSC/ECE 517 Fall 2009 Lec. 2

� Correct errors as code executes

� Actions you can perform debugging include:� Control Execution

� Set simple breakpoints

� Set conditional breakpoints

� Review and change variable values

� Hot code replace (feature new to JRE 1.4)

Page 4: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Debugging » Debug Perspective

Threads and Monitor View Variable View

4CSC/ECE 517 Fall 2009 Lec. 2

Console View

Outline View

Editor View

Tasks View

Page 5: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Debugging » Simple Breakpoint

� Breakpoint� Stops the execution of a

program at the point

� Thread suspends at the location where the breakpoint is set

5CSC/ECE 517 Fall 2009 Lec. 2

breakpoint is set

� Setting a breakpoint� CTRL+Shift+B at

current point in editor line

� Double click in editor’s marker bar at current line

Page 6: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Debugging » Starting a Debugging Session

� Select Java class

containing the following:

� main() method

� Resulting execution will

pass breakpoint

6CSC/ECE 517 Fall 2009 Lec. 2

� Select Run » Debug As »

Java Application

� Or Select Debug As »

Java Application from the

drop-down menu on the

Debug tool bar.

Page 7: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Debugging » Debug Session

� Execution suspends

prior to the line with a

breakpoint

� You can set multiple

breakpoints

7CSC/ECE 517 Fall 2009 Lec. 2

breakpoints

Page 8: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Debugging » Deleting Breakpoints

� Double-click on the breakpoint in the editor

8CSC/ECE 517 Fall 2009 Lec. 2

Page 9: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Debugging » Control Execution From Breakpoint…

� Step Into or F5: � For methods, execute

method and suspend on first statement in the method

� For assignments, similar to Step Over

� For conditionals, similar to Step Over

9CSC/ECE 517 Fall 2009 Lec. 2

Step Over

� Step Over or F6� Execute next statement

� Step Return or F7� Resume execution to the end

of the method on the next line after it was invoked

Page 10: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Debugging » Control Execution From Breakpoint

� Resume or F8� Continue execution

until program ends or another breakpoint is reached

� Terminate� Stops the current

10CSC/ECE 517 Fall 2009 Lec. 2

� Stops the current execution thread

Page 11: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Debugging » Variables and Fields

� To see the values

bound to fields:

� Use Variables View

� Select variable in editor

and select Inspect

11CSC/ECE 517 Fall 2009 Lec. 2

Page 12: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

public class Debug {

private int something = 0;

private Vector list = new Vector();

public void firstMethod(){

thirdMethod(something);

something = something + 1;

}

public void secondMethod(){

thirdMethod(something);

Debugging » Code Debugging in this Module

12CSC/ECE 517 Fall 2009 Lec. 2

thirdMethod(something);

something = something + 2;

}

public void thirdMethod(int value){

something = something + value;

}

public static void main(String[] args) {

Debug debug = new Debug();

debug.firstMethod();

debug.secondMethod();}

}

Page 13: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Debugging » Variables View

� Shows all fields of instance where breakpoint

occurred

� Select this to see all fields

� Select any field to see value

� If field is bound to an object, you can select Inspect

from the menu to view its fields and values

13CSC/ECE 517 Fall 2009 Lec. 2

from the menu to view its fields and values

Page 14: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Debugging » Changing Field Values

� To change field value:

� Select field in Variables view

� Select Change Variable Value from the menu

� Enter new value into Set Variable Value window

Click OK

14CSC/ECE 517 Fall 2009 Lec. 2

� Click OK

Page 15: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Debugging » Expressions View

� Remembers all objects you have inspected

� Displays the fields of the object� You can see the values

of the fields

15CSC/ECE 517 Fall 2009 Lec. 2

of the fields

� You can Inspect the fields

� Opens when:� You Inspect an object

� You click on the Expressions tab

Page 16: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Debugging » Breakpoint View

� Lists all available

breakpoints

� Can be used for

manipulating breakpoints

(through the views menu):

� Enabling

16CSC/ECE 517 Fall 2009 Lec. 2

� Enabling

� Disabling

� Removing

� Also displays breakpoints

properties

� Accessed like other

debugging views

Page 17: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Debugging » Debug View

� Shows:

� Active threads

� Current stack frame

when execution has

stopped

Previous stack frames

17CSC/ECE 517 Fall 2009 Lec. 2

� Previous stack frames

� Method and variables

are shown in the editor

for the selected frame

� Update in the editor

updates the source

Page 18: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Debugging » Breakpoint Types

� Breakpoints can be set for the following Java entities:

� Line (simple breakpoint)

� Method

Field (watchpoint)

18CSC/ECE 517 Fall 2009 Lec. 2

� Field (watchpoint)

� Java Exception

� Each breakpoint is set a different way and has different properties

Page 19: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Debugging » Method Breakpoints

� To set method breakpoint:

� Select method in the Outline View

� From context menu select Toggle Method Breakpoint

� To set breakpoint’s properties:

� Select breakpoint in editor. From the context menu, select Breakpoint Properties.. from the context menu

� In the Properties dialog, Check Enabled to enable the breakpoint

19CSC/ECE 517 Fall 2009 Lec. 2

enable the breakpoint� Check Hit Count to enable hit count.

Breakpoint suspends execution of a thread the nth time it is hit, but never again, until it is re-enabled or the hit count is changed or disabled.

� Choose Enable condition to have breakpoint enabled only if the specified condition occurs.

� condition is 'true' option: Breakpoint stops if the condition evaluates to true. The expres-sion provided must be a boolean expression.

� value of condition changes option: Breakpoint stops if result of the condition changes.

Page 20: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Debugging » Field Breakpoints

� Also known as watchpoint

� To set the watchpoint:

� Select field in the Outline View

� From context menu select Toggle Watchpoint

� To set watchpoint’s properties:

20CSC/ECE 517 Fall 2009 Lec. 2

� To set watchpoint’s properties:

� Select breakpoint in editor

� Select Breakpoint Properties.. from context menu

� Set properties as desired

� Access/modification, enable

� Execution suspended on

access/modification of field

Page 21: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Debugging » Java Exception Breakpoint

� To Add Java Exception

Point:

� Select Run » Add Java

Exception Breakpoint

from main menu

Enter exception type

21CSC/ECE 517 Fall 2009 Lec. 2

� Enter exception type

� Specify what triggers a

breakpoint:

� Caught exception

� Uncaught exception

� Both

Page 22: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Debugging » How To Debug

� Here are simple steps for debugging in Eclipse:

� Set your breakpoints

� Hit a breakpoint during execution

Walk/step through code to other breakpoints

22CSC/ECE 517 Fall 2009 Lec. 2

� Walk/step through code to other breakpoints

� Follow along in editor

� Inspect/Display interesting fields

� Watch the Console for things to happen

Page 23: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Debugging » Summary

� You have learned:

� The views in the Debug Perspective

� Typical debug session

� How to use the Inspector

About the different types of breakpoints

23CSC/ECE 517 Fall 2009 Lec. 2

� About the different types of breakpoints

� How to set breakpoints

� How step around your code doing debugging

Page 24: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Debugging » Exercise 2

� Set a breakpoint in the printField method of the

class NewClass that was created in Exercise 1.

� Start a debug session.

24CSC/ECE 517 Fall 2009 Lec. 2

� What happens to the program execution?

� What do you see in the debug windows?

Page 25: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Module Road Map

1. Overview2. Installing and Running

3. Building and Running Java Classes

4. Debugging5. Testing with JUnit

� What is JUnit?

25CSC/ECE 517 Fall 2009 Lec. 2

� What is JUnit?

� Where Does it Come From?

� Working with Test Cases

� Working with Test Suites

� JUnit Window

6. Refactoring7. Version Control with CVS

Page 26: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

What is JUnit?

� Regression testing framework

� Written by Erich Gamma and Kent Beck

� Used for unit testing in Java

� Open Source

26CSC/ECE 517 Fall 2009 Lec. 2

� Open Source

� Released under IBM's CPL

Page 27: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Testing » Where Does JUnit Come From?

� JUnit’s web site: http://junit.org/index.htm

� Eclipse includes JUnit

� Eclipse provides new GUI to run JUnit test cases and

suites

� JDT tools include a plug-in that integrates JUnit into the

27CSC/ECE 517 Fall 2009 Lec. 2

JDT tools include a plug-in that integrates JUnit into the

Java IDE

� Allows you to define regression tests for your code and

run them from the Java IDE.

� You can run your unit tests outside of Eclipse

� If you wish using TestRunner

� Using JUnit’s Window

Page 28: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Testing » Including JUnit in a Project

� In the Package window, right-click on the name of the project

� Choose “Properties”; then select “Java Build Path”

� Click on the “Libraries” tab, and then choose the “Add Library” button on the right.

28CSC/ECE 517 Fall 2009 Lec. 2

button on the right.

� Select “Junit” and click “Finish”.

Page 29: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Testing » Eclipse JUnit Setup

� Eclipse preferences can be set in the JUnit Preferences window (Window » Preferences from the main menu. Expand Java in the Preferences window)

For the most part you can leave

29CSC/ECE 517 Fall 2009 Lec. 2

� For the most part you can leave the preferences alone

� Filters needed to identify packages, classes, or patterns that should not be shown in the stack trace of a test failure

Page 30: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Testing » JUnit Test Cases

� JUnit Test Cases� Test case

� Runs multiple tests

� Implemented as subclass of TestCase

� Define instance variables that store the state of

30CSC/ECE 517 Fall 2009 Lec. 2

� Define instance variables that store the state of the tests in the class

� Initialize TestCase by overriding setUp method

� Clean-up after test case is done by overriding tearDown method

� The Test framework will invoke the setUp and tearDown methods.

Page 31: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Testing » Creating JUnit Test Cases in Eclipse

� Create a new package

to contain your test

case classes

31CSC/ECE 517 Fall 2009 Lec. 2

� Add the JUnit JAR file

to the project’s

buildpath

Page 32: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Testing » Creating JUnit Test Cases in Eclipse

� Select your testing package

� From the context menu select New » JUnit Test Case. This opens the New JUnit Test Case Wizard.

� Fill in the name of your test case in the Name field.

32CSC/ECE 517 Fall 2009 Lec. 2

Fill in the name of your test case in the Name field.

� Select the method stubs that you want Eclipse to generate

� This will create the corresponding class in your testing package

Page 33: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Testing » JUnit TestCase Template

public class NewTestCase extends TestCase {public class NewTestCase extends TestCase {public class NewTestCase extends TestCase {public class NewTestCase extends TestCase {

public static void main(String[] args) {public static void main(String[] args) {public static void main(String[] args) {public static void main(String[] args) {}}}}

public NewTestCase(String arg0) {public NewTestCase(String arg0) {public NewTestCase(String arg0) {public NewTestCase(String arg0) {super(arg0);super(arg0);super(arg0);super(arg0);

}}}}

33CSC/ECE 517 Fall 2009 Lec. 2

protected void setUp() throws Exception {protected void setUp() throws Exception {protected void setUp() throws Exception {protected void setUp() throws Exception {super.setUp();super.setUp();super.setUp();super.setUp();

}}}}

protected void tearDown() throws Exception {protected void tearDown() throws Exception {protected void tearDown() throws Exception {protected void tearDown() throws Exception {super.tearDown();super.tearDown();super.tearDown();super.tearDown();

}}}}}}}}

Page 34: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Testing » Adding Tests to Test Cases

� Any method in a TestCase class is considered a test if it begins with the word “test”.� You can write many tests (have many test

34CSC/ECE 517 Fall 2009 Lec. 2

� You can write many tests (have many test methods)

� Each test method should use a variety of assert… methods to perform tests on the state of its class.� Assert methods are inherited

Page 35: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Testing » JUnit Assert Methods

� Assert methods include:

� assertEqual(x,y)

� assertFalse(boolean)

� assertTrue(boolean)

assertNull(object)

35CSC/ECE 517 Fall 2009 Lec. 2

� assertNull(object)

� assertNotNull(object)

� assetSame(firstObject, secondObject)

� assertNotSame(firstObject, secondObject)

Page 36: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Testing » Adding Two Tests to JUnit Test Case

public class NewTestCase extends TestCase {public class NewTestCase extends TestCase {public class NewTestCase extends TestCase {public class NewTestCase extends TestCase {

public static void main(String[] args) {public static void main(String[] args) {public static void main(String[] args) {public static void main(String[] args) {}}}}

public NewTestCase(String arg0) {public NewTestCase(String arg0) {public NewTestCase(String arg0) {public NewTestCase(String arg0) {super(arg0);super(arg0);super(arg0);super(arg0);

}}}}

protected void setUp() throws Exception {protected void setUp() throws Exception {protected void setUp() throws Exception {protected void setUp() throws Exception {super.setUp();super.setUp();super.setUp();super.setUp();

36CSC/ECE 517 Fall 2009 Lec. 2

super.setUp();super.setUp();super.setUp();super.setUp();}}}}

protected void tearDown() throws Exception {protected void tearDown() throws Exception {protected void tearDown() throws Exception {protected void tearDown() throws Exception {super.tearDown();super.tearDown();super.tearDown();super.tearDown();

}}}}public void testCompareSucceed() {public void testCompareSucceed() {public void testCompareSucceed() {public void testCompareSucceed() {

assertEquals(0, 0); //this assertion will succeedassertEquals(0, 0); //this assertion will succeedassertEquals(0, 0); //this assertion will succeedassertEquals(0, 0); //this assertion will succeed}}}}public void testCompareFail() {public void testCompareFail() {public void testCompareFail() {public void testCompareFail() {

assertEquals(0, 1); //this assertion will failassertEquals(0, 1); //this assertion will failassertEquals(0, 1); //this assertion will failassertEquals(0, 1); //this assertion will fail}}}}

}}}}

Page 37: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Testing » Running JUnit Test Case

� Select TestCase class

� From the Run menu select Run » Run As » JUnit Test

� This will run the tests in your TestCase class

37CSC/ECE 517 Fall 2009 Lec. 2

your TestCase class along with the setUpand tearDown methods

� You will then get a report in the JUnit window

Page 38: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Testing » JUnit Window

� Red indicates a test has failed

� You can see which test failed

� You can see the call trace leading to the

38CSC/ECE 517 Fall 2009 Lec. 2

trace leading to the failure

� If you wish to see the tests in TestCase click on the Hierarchy tab

Page 39: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Testing » JUnit Window

� You can see how many

tests ran

� How many failures

occurred

39CSC/ECE 517 Fall 2009 Lec. 2

occurred

� You can see the details

of the failure

� Errors occur when

exceptions are thrown

Page 40: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Testing » Creating JUnit Test Suite

� Test Suite

� Runs multiple test cases

or suites

� To create a TestSuite

� Select your testing

40CSC/ECE 517 Fall 2009 Lec. 2

Select your testing

package

� From the context menu

select New » Other…

� Then from the Wizard

select Java » JUnit »

JUnit Test Suite

Page 41: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Testing » Creating JUnit Test Suite

� Fill in the name of your

TestSuite class

� Select the TestCases

to include in your

TestSuite

41CSC/ECE 517 Fall 2009 Lec. 2

TestSuite

Page 42: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Testing » Unit Test Suite Template

import com.test;import com.test;import com.test;import com.test;import junit.framework.Test;import junit.framework.Test;import junit.framework.Test;import junit.framework.Test;

public class AllInclusiveTestSuite {public class AllInclusiveTestSuite {public class AllInclusiveTestSuite {public class AllInclusiveTestSuite {public static Test suite() {public static Test suite() {public static Test suite() {public static Test suite() {

TestSuite suite =TestSuite suite =TestSuite suite =TestSuite suite =new TestSuite("Test for new TestSuite("Test for new TestSuite("Test for new TestSuite("Test for

com.test");com.test");com.test");com.test");

42CSC/ECE 517 Fall 2009 Lec. 2

com.test");com.test");com.test");com.test");//$JUnit//$JUnit//$JUnit//$JUnit----BEGIN$BEGIN$BEGIN$BEGIN$suite.addTestSuite(NewTestCase.class));suite.addTestSuite(NewTestCase.class));suite.addTestSuite(NewTestCase.class));suite.addTestSuite(NewTestCase.class));suite.addTestSuite(SecondTestCase.class));suite.addTestSuite(SecondTestCase.class));suite.addTestSuite(SecondTestCase.class));suite.addTestSuite(SecondTestCase.class));//$JUnit//$JUnit//$JUnit//$JUnit----END$END$END$END$return suite;return suite;return suite;return suite;

}}}}}}}}

Page 43: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Testing » Running JUnit Test Suite

� Select TestSuite class

� From the Run menu

select Run » Run As »

JUnit Test

� This will run the test

43CSC/ECE 517 Fall 2009 Lec. 2

� This will run the test

cases in your TestSuite

class

� You will then get a

report in the JUnit

Window

Page 44: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Testing » JUnit Test Interface

� The JUnit classes TestCase and TestSuite both implement the JUnit Test interface

� Therefore, you can add JUnit TestSuites to other TestSuites

public static Test suite() {TestSuite suite = new TestSuite("Test for testing");

44CSC/ECE 517 Fall 2009 Lec. 2

TestSuite suite = new TestSuite("Test for testing");

//$JUnit-BEGIN$

suite.addTestSuite(FirstTestCase.class);

suite.addTestSuite(SecondTestCase.class);

suite.addTest(AllTests.suite());

//$JUnit-END$

return suite;

}

Page 45: Overview of Eclipse Lectures - Nc State Universitypeople.engr.ncsu.edu/efg/517/f09/common/lectures/notes/lec2-f09.pdf · 2. Installing and Running 3. Building and Running Java Classes

Exercise 3

� Create a JUnit test case class TestClass for the package

csc517 of the project EgApp.

� Add a test method testBoolean to the class TestClass.

� In the method testBoolean, invoke the assert routine

assertTrue with the argument “0” (FALSE).

Run the test case. What do you see in the JUnit window?

45CSC/ECE 517 Fall 2009 Lec. 2

� Run the test case. What do you see in the JUnit window?

� Now invoke the assertTrue routine with the argument “1” (TRUE).

� Run the test case. What is the output in the JUnit window?