Tutorials For Hibernate, Ejb 2, Ejb 3 Struts, Javaserverfaces (JSF) Tomcat, Jboss, Myeclipse, Eclipse and Other
Tutorials For Hibernate, Ejb 2, Ejb 3 Struts, Javaserverfaces (JSF) Tomcat, Jboss, Myeclipse, Eclipse and Other
http://www.laliluna.de/eclipse-junit-testing-tutorial.html
Tutorials for Hibernate, EJB 2, EJB 3 Struts, JavaServerfaces (JSF) Tomcat, JBoss, Myeclipse, Eclipse and other
Tutorials Debugging, Testing, Tuning Eclipse Junit testing tutorial
Eclipse Junit testing tutorial JUnit is a simple Java testing framework to write tests for you Java application. This tutorial gives you an overview of the features of JUnit and shows a little example how you can write tests for your Java application. Navigation
Sprache / Language
News News from JavaOne 2008 in San Francisco (May. 06, 2008) New JBoss Seam tutorial (Apr. 25, 2008) 2007 was a succesful year (Jan. 11, 2008) Open sourced further struts tutorials (Dec. 12, 2007) Open sourced struts tutorials (Dec. 06, 2007) Hibernate ebook update (Oct. 28, 2007) Hibernate Developer Guide
General
Author: Sascha Wolski Sebastian Hennebrueder http://www.laliluna.de/tutorials.html ? Tutorials for Struts, EJB, xdoclet and eclipse.
Search
Homepage Blog Tutorials Schedule Hibernate, EJB 2, JDBC Tutorials EJB 3 development Spring framework General, Java technologies Web frameworks, JSP and Servlets Tutorials Struts 1.x JavaServer Faces Tutorials Debugging, Testing, Tuning Application Server administration and configuration Tutoriales (espaol) Bugs and Exceptions References Hibernate 3 / JPA book and ebook Tutorial and EBook Shop Training - Development Support Links/Tips Feedback Disclaimer Social projects
Software: Eclipse 3.x Junit 2.x basic and advanced topics, performance, working examples, integration with Spring, EJB3, Struts and JSF (MyFaces) There is an English eBook and a German paper book available.
What is JUnit
JUnit is a simple open source Java testing framework used to write and run repeatable automated tests. It is an instance of the xUnit architecture for unit testing framework. Eclipse supports Get more information. creating test cases and running test suites, so it is easy to use for your Java applications. Training
1 de 9
05-08-2008 10:31
http://www.laliluna.de/eclipse-junit-testing-tutorial.html
JUnit features include: Assertions for testing expected results Test fixtures for sharing common test data Test suites for easily organizing and running tests Graphical and textual test runners
You need consulting or a development team. We have a small but highly qualified 2. Write a test method to assert expected results on the object development under test: team. Get more information. Support You need one time or regular support. We offer flexibles support services. Get more information.
3. Write a suite() method that uses reflection to dynamically create a test suite containing all the testXXX() methods:
4. Activate the JUnit view in Eclipse (Window > Show View > Other.. > Java > JUnit).
2 de 9
05-08-2008 10:31
http://www.laliluna.de/eclipse-junit-testing-tutorial.html
You find the JUnit tab near the Package Explorer tab. You can change the position of the tab by drag and drop it.
5.
3 de 9
05-08-2008 10:31
http://www.laliluna.de/eclipse-junit-testing-tutorial.html
Right click on the subclass of TestCase and choose Run > JUnit Test to run the test.
The dynamic way to create a test case to be run uses reflection to implement runTest. It assumes the name of the test is the name of the test case method to invoke. It dynamically finds and invokes the test method. The dynamic way is more compact to write but it is less static type safe. An error in the name of the test case goes unnoticed until you run it and get a NoSuchMethodException. We leave the choice of which to use up to you.
4 de 9
05-08-2008 10:31
http://www.laliluna.de/eclipse-junit-testing-tutorial.html
What is a TestSuite
If you have two tests and you'll run them together you could run the tests one at a time yourself, but you would quickly grow tired of that. Instead, JUnit provides an object TestSuite which runs any number of test cases together. The suite method is like a main method that is specialized to run tests. Create a suite and add each test case you want to execute:
public static void suite(){ TestSuite suite = new TestSuite(); suite.addTest(new BookTest("testEquals")); suite.addTest(new BookTest("testBookAdd")); return suite; }
Since JUnit 2.0 there is an even simpler way to create a test suite, which holds all testXXX() methods. You only pass the class with the tests to a TestSuite and it extracts the test methods automatically. Note: If you use this way to create a TestSuite all test methods will be added. If you do not want all test methods in the TestSuite use the normal way to create it. Example:
public static void suite(){ return new TestSuite(BookTest.class); }
A little example
Create a new Java project named JUnitExample. Add a package de.laliluna.tutorial.junitexample where you place the example classes and a package test.laliluna.tutorial.junitexample where you place your test classes.
5 de 9
05-08-2008 10:31
http://www.laliluna.de/eclipse-junit-testing-tutorial.html
6 de 9
05-08-2008 10:31
http://www.laliluna.de/eclipse-junit-testing-tutorial.html
Now we want to write a test for the equals(..) method of the class Book. We provide three private properties, book1, book2 and book3 of type Book.
private Book book1; private Book book2; private Book book3;
Within the setUp() method we initializes the three properties with some values. Property book1 and book3 are the same.
protected void setUp() throws Exception { super.setUp(); book1 = new Book("ES", 12.99); book2 = new Book("The Gate", 11.99); book3 = new Book("ES", 12.99); }
7 de 9
05-08-2008 10:31
http://www.laliluna.de/eclipse-junit-testing-tutorial.html
Now, add a test method testEquals() to the test case. Within the method we use the assertFalse() method of the JUnit framework to test if the return-value of the equals(..) method is false, because book1 and book2 are not the same. If the return-value is false the logic of the equals() method is correct, otherwise there is a logical problem while comparing the objects. We want to test if the method compares the objects correctly by using the assertTrue() method. Book1 and Book3 are the same, because both are an instance of the class Book and have the same values. The following source code shows the testEquals() method:
public void testEquals(){ assertFalse(book2.equals(book1)); assertTrue(book1.equals(book1)); }
8 de 9
05-08-2008 10:31
http://www.laliluna.de/eclipse-junit-testing-tutorial.html
9 de 9
05-08-2008 10:31