27
JUnit Eclipse, Java and introduction to Junit

JUnit Eclipse, Java and introduction to Junit. Topics Covered Using Eclipse IDE Example Java Programs Junit Introduction

Embed Size (px)

Citation preview

Page 1: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

JUnit

Eclipse, Java and introduction to Junit

Page 2: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

Topics Covered Using Eclipse IDE Example Java Programs Junit Introduction

Page 3: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

What is Eclipse?Eclipse is an open source community whose projects are focused on providing an extensible development platform and application frameworks for building software.

Download eclipse fromhttp://www.eclipse.org/downloads/index.php

Article on using junit with Eclipsehttp://www.onjava.com/pub/a/onjava/2004/02/04/juie.html

Page 4: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

JavaJava Tutorials http://java.sun.com/docs/books/

tutorial/getStarted/index.html

Thinking in Java 3rd Edition by Bruce Eckel

http://www.mindview.net/Books/TIJ/Or get a printed version

Page 5: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

A simple java Program

//A Very Simple Example - Hello.java

public class Hello {

public static void main(String[] args) { System.out.println(“Hello world");

}

}

Page 6: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

Example Compilation

#bash# javac Hello.java

#bash# java Hello

Hello world

Page 7: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

Compiling the Eclipse Way

Step 1: Create a New Project. Click on File -> New Project Select Java Project Enter the name for the project

Step 2: Create a Class File Click on File -> New Class Enter the name for the Class (Hello)

Page 8: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

Compiling the Eclipse Way (Contd.)

Step 3: Copy or create the java program (Hello.java) and Save.

Step 4: Run the application Select Run -> Run and click on Java

Application. Click on the New button and then Click

on Run.

Page 9: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

Eclipse Output

Page 10: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

What is Unit Test? A definition from IEEE

– Testing of individual hardware or software or group of related units

Usually, a unit test is code that tests a "unit": the unit could be a class, a component, a module, a function

Unit Test usually built and run at the development cycle.

Unit test is not functional, application testing Unit tests are not interactive

Page 11: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

We need a framework A unit testing framework provides

A mechanism to organize and group multiple tests

A simple way to invoke tests Clear indication of which tests passed/failed A standard way to write tests and specify

expected results.

Page 12: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

What is JUnit JUnit is a Unit Testing Tool for Java JUnit is a regression testing framework Programmed by Erich Gamma and Kent Be

ck Open Source

– Hosted on SourceForge– http://sourceforge.net/index.php – http://junit.sourceforge.net/ – http://www.junit.org/

Page 13: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

JUnit’s Features Assertions for testing expected results Testing fixtures for sharing common

test data Test suites for easily organizing and

running tests Graphical and textual test runners

Page 14: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

JUnit Framework

http://junit.sourceforge.net/javadoc/index.html

run()

<< interface >>Test

fName

setUp()runTest()tearDown()run()

TestCase

run()addTest()

TestSuite

*

TestResult

junit.framework

assertTrue()assertEquals()...

Assert

junit.textui.TestRunner junit.swingui.TestRunner

Page 15: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

junit

How to Test with JUnit?

Student

TestCase

exercise1..*

TestRunner

run1..*TestStudent

test1

test2

Page 16: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

Another Java Examplepublic class Student{

private String sname;private String sid;private int score;public Student(String num, String nam, int scr){

sid = num;sname = nam;score = scr;

}

Page 17: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

Another Example(cont.)public String getNumber(){

return sid;}public String getName(){

return sname;}public int getScore(){

return score;}

Page 18: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

Another Example(cont.)public void setNumber(String num){

sid = num;}public void setName(String name){

sname=name;}public void setScore(int scr){

score=scr;}} //End Class Student

Page 19: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

Testing the Student Classpublic class TestStudent {

public static void main(String[] args) { Student student=new Student(“043”, “Justin”,100);

if (!(student.getName()!=null && student.getName().equals(“Justin”)))

System.out.println(“getName method failure”);}

}

Page 20: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

Testing the Junit wayimport junit.framework.*;public class TestStudent1 {

public static void main(String[] args) { Student student = new Student(“043", "Justin", 100);Assert.assertEquals("getName method failure", "Justin", student.getName());}

}

Page 21: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

Assert in Junit Class Assert provide a set of methods These methods are static They can throw an object with failure

messages. – Messages are only displayed when an

assert fails

Page 22: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

Junit TestCasepublic class TestIt extends TestCase { public TestIt (String name) { super (name); } public void testGetMethod() {

Student student=new Student(“013", "Justin", 100);

assertEquals(“013", student.getNumber()); assertEquals("Justin", student.getName());assertEquals(100, student.getScore());

}}

Page 23: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

Junit in Eclipse Click on File -> New -> Junit Testcase Give the name for the testcase and

create/copy the file in the editor. Click on Run -> Run. Select Junit

Testcase, New and click on Run.

Page 24: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

Junit in Eclipse

Page 25: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

Assert.assertEquals assertEquals can be overrided many ti

mes for different comparison, such as for String, int

For object comparison, you have to override equals in order to compare fields of two objects

Page 26: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

Equals in our examplepublic boolean equals(Object anObject){

if (anObject instanceof Student){

Student aStudent=(Student) anObject;return

aStudent.getNumber().equals(getNumber())&& aStudent.getName()==getName()&&aStudent.getScore==getScore();}return false;

}

Page 27: JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

JUnit Rules and Conventions

Subclass TestCase Test methods

– public void testXXX() [throws …]

– Any number of assertions per method