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

Embed Size (px)

Text of JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java...

  • JUnitEclipse, Java and introduction to Junit

  • Topics CoveredUsing Eclipse IDEExample Java ProgramsJunit 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

  • JavaJava Tutorialshttp://java.sun.com/docs/books/tutorial/getStarted/index.html

    Thinking in Java 3rd Edition by Bruce Eckelhttp://www.mindview.net/Books/TIJ/Or get a printed version

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

  • Example Compilation #bash# javac Hello.java

    #bash# java HelloHello world

  • Compiling the Eclipse WayStep 1: Create a New Project. Click on File -> New ProjectSelect Java ProjectEnter the name for the projectStep 2: Create a Class FileClick on File -> New ClassEnter the name for the Class (Hello)

  • Compiling the Eclipse Way (Contd.)Step 3: Copy or create the java program (Hello.java) and Save.Step 4: Run the applicationSelect Run -> Run and click on Java Application.Click on the New button and then Click on Run.

  • Eclipse Output

  • What is Unit Test?A definition from IEEETesting of individual hardware or software or group of related unitsUsually, 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

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

  • What is JUnitJUnit is a Unit Testing Tool for JavaJUnit is a regression testing frameworkProgrammed by Erich Gamma and Kent BeckOpen Source Hosted on SourceForgehttp://sourceforge.net/index.php http://junit.sourceforge.net/ http://www.junit.org/

  • JUnits FeaturesAssertions for testing expected results Testing fixtures for sharing common test dataTest suites for easily organizing and running testsGraphical and textual test runners

  • JUnit Frameworkhttp://junit.sourceforge.net/javadoc/index.html

  • How to Test with JUnit?Student

  • 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;}

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

  • 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

  • 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);}}

  • 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()); }}

  • Assert in JunitClass Assert provide a set of methodsThese methods are static They can throw an object with failure messages. Messages are only displayed when an assert fails

  • 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()); }}

  • Junit in EclipseClick on File -> New -> Junit TestcaseGive 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.

  • Junit in Eclipse

  • Assert.assertEqualsassertEquals can be overrided many times for different comparison, such as for String, intFor object comparison, you have to override equals in order to compare fields of two objects

  • 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;}

  • JUnit Rules and ConventionsSubclass TestCaseTest methodspublic void testXXX() [throws ]Any number of assertions per method

    A set of assert methods. Messages are only displayed when an assert fails.