Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction."— Presentation transcript:

1 JUnit Eclipse, Java and introduction to Junit

2 Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction

3 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 from http://www.eclipse.org/downloads/index.php Article on using junit with Eclipse http://www.onjava.com/pub/a/onjava/2004/02/04/juie.html

4 Java Java Tutorials  http://java.sun.com/docs/books/tutorial/ getStarted/index.html Thinking in Java 3 rd Edition by Bruce Eckel  http://www.mindview.net/Books/TIJ/ http://www.mindview.net/Books/TIJ/ Or get a printed version

5 A simple java Program //A Very Simple Example - Hello.java public class Hello { public static void main(String[] args) { System.out.println(“Hello world"); }

6 Example Compilation #bash# javac Hello.java #bash# java Hello Hello world

7 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)

8 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.

9 Eclipse Output

10 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

11 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.

12 What is JUnit  JUnit is a Unit Testing Tool for Java  JUnit is a regression testing framework  Programmed by Erich Gamma and Kent Beck  Open Source –Hosted on SourceForge –http://sourceforge.net/index.phphttp://sourceforge.net/index.php –http://junit.sourceforge.net/http://junit.sourceforge.net/ –http://www.junit.org/http://www.junit.org/

13 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

14 JUnit Framework http://junit.sourceforge.net/javadoc/index.html

15 junit How to Test with JUnit? Student TestCase exercise 1..* TestRunner run 1..* TestStudent test 1 test 2 …

16 Another Java Example public 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; }

17 Another Example(cont.) public String getNumber() { return sid; } public String getName() { return sname; } public int getScore() { return score; }

18 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

19 Testing the Student Class public 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”); }

20 Testing the Junit way import 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()); } }

21 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

22 Junit TestCase public 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()); }

23 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.

24 Junit in Eclipse

25 Assert.assertEquals  assertEquals can be overrided many times for different comparison, such as for String, int  For object comparison, you have to override equals in order to compare fields of two objects

26 Equals in our example public boolean equals(Object anObject) { if (anObject instanceof Student) { Student aStudent=(Student) anObject; return aStudent.getNumber().equals(getNumber())&& aStudent.getName()==getName()&&aStudent.getS core==getScore(); } return false; }

27 JUnit Rules and Conventions  Subclass TestCase  Test methods –public void testXXX() [throws …] –Any number of assertions per method


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

Similar presentations


Ads by Google