1 / 27

JUnit

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

tiger
Download Presentation

JUnit

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 3rd Edition by Bruce Eckel • 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.php • http://junit.sourceforge.net/ • 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 TestCase TestRunner TestStudent run 1..* exercise 1..* test1 test2 … How to Test with JUnit? Student

  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.getScore==getScore(); } return false; }

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

More Related