Comprehensive JUnit Guide: Tags, Methods, and Examples
Learn how to use JUnit effectively with tags, methods, and examples. Understand the importance of equals and hashCode methods. Discover best practices and design decisions for successful testing.
Comprehensive JUnit Guide: Tags, Methods, and Examples
E N D
Presentation Transcript
Programo Issues Cora PérezAriza ~ DECSAI ~ UGR Granada, January 28th & 29th, 2009
JUnit • Introduction to the topic • Tags • Example • Design Decisions • equals and hashCode methods • toString method Index
Why a JUnit? They reassure us that the expected behavior of our work is the actual behavior (and will stay like that in the future) • How to use it? • Creation is easy: short methods that test critical parts of the project’s classes. • Integrated on NetBeans • Few “tags” to learn JUnit Introduction
Tags: • @Test • @Before and @After • @BeforeClass and @AfterClass • @Ignore JUnit Tags
package programo.core.potential; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.Ignore; import static org.junit.Assert.*; import programo.core.variable.*; import programo.core.assignation.*; Youshouldimport as manyclasses as tagsyou are goingto use JUnit Example: Beginning
public class PotentialTableTest { PotentialTable pot1, pot2, resNorm, resMarg, resComb, resProj; CategoricalVariable X1, X2, X3, X4; CategoricalAssignation configurationToProject; VariableSet setOfVars1, setOfVars2; You can declare variables as classmembers, to use as global variables foryourtests JUnit Example: data members
@BeforeClass public static void setUpClass() throws Exception { …. } @AfterClass public static void tearDownClass() throws Exception { … } Thesemethodswillrunbefore/afterthebunch of tests (only once) JUnit Example: before/after the tests
@Before public void setUp() { … } @After public void tearDown() { … } Thesemethodswillrunbefore/aftereach test (so one time per test) JUnit Example: before/after each test
public void initializeVariables(){ … } You can define auxiliar methodstoperformspecificoperationswithinyourJUnitfile JUnit Example: auxiliar methods
@Test tagmarksthemethod as a test @Test public void testNormalizePotential() { initializeVariables(); pot1.normalizePotential(); assertTrue(resNorm.equalValues(pot1)); } Body of the test case Assertiontocheckifthe test has worked JUnit Example: test cases
Youmaynotwanttoexecuteoneor more test cases: use @Ignore @Ignore("Not ready to run") @Test public void testSumMarginalize() { initializeVariables(); Potential result = pot1.sumMarginalize(setOfVars2); assertTrue(resMarg.equalValues(result)); } JUnit Example: test cases
JUnit Result in NetBeans
Other utilities: • Exception Handling • Use “expected” parameter with @Test tag for test cases that expect exception: • @Test(expected = ArithmeticException.class) • Timeout • Define a timeout period in miliseconds: • @Test(timeout = 1000) JUnit Other useful utilities
public boolean equals(Object obj): • Implements an equivalence relation: • It is reflexive • For any reference value x, x.equals(x) should return true • It is symmetric • For any x and y, x.equals(y) == y.equals(x) • It is transitive • For any x, y, z, if x.equals(y) and y.equals(z), then x.equals(z) • It is consistent • For any x and y, x.equals(y) should return the same in every invocation if both remains inmutable • For any non-null reference value x, x.equals(null) should return false • Equal objects must have equal hash codes DesignDecisions Overview: equals General Contract
public int hashCode(): • Must be consistent during the same execution of the application • Equal objects must produce equal hash codes, however unequal objects need not produce distinct hash codes • Different hash codes for different objects may improve the performance of hashtables DesignDecisions Overview: hashCode General Contract
Do not override: • Each instance of the class is inherently unique • You don’t care whether the class provides a “logical equality” test • A super class has already overridden equals appropriately for this class • The class is private or package private, and you are certain that its equals method will never be invoked • Override: • When a class has a notion of logical equality that differs from mere object identity, and a super class has not already overridden equals to implement the desired behavior DesignDecisions equals and hashCode: when to override
Problems with sets and hash maps: Overriding equals enables instances of the class to serve as map keys or set elements with predictable, desirable behavior DesignDecisions equals and hashCode: possible problems
Problems with sets and hash maps: public class Point{ private final int x; private final int y; public Point(int x, int y){ this.x = x; this.y = y; } } … HashMap tabla = new HashMap(); tabla.put(new Point(1,3), 1); if(tabla.containsKey(new Point(1,3))) System.out.println("both points are the same"); else System.out.println("two different objects"); DesignDecisions equals and hashCode: possible problems: Example
Problems with sets and hash maps: public class Point{ private final int x; private final int y; public Point(int x, int y){ this.x = x; this.y = y; } @Override public boolean equals(Object obj){ if(this == obj) return true; if(obj == null) || (obj.getClass() != this.getClass()) return false; Point p = (Point) obj; return (p.x == this.x && p.y == this.y) } } DesignDecisions equals and hashCode: possible problems: Example
Problems with sets and hash maps: public class Point{ private final int x; private final int y; public Point(int x, int y){ this.x = x; this.y = y; } @Override public boolean equals(Object obj){ if(this == obj) return true; if(obj == null) || (obj.getClass() != this.getClass()) return false; Point p = (Point) obj; return (p.x == this.x && p.y == this.y) } @Override public int hashCode(){ int hash = 7; hash = 31 * hash + this.x; hash = 31 * hash + this.y; return hash; } } DesignDecisions equals and hashCode: possible problems: Example
equals: • Do not change the type of the argument, it takes java.lang.Object • Review your method to verify that it fulfills all the requirements stated by the general contract • Do not forget to override hashCode method whenever you override equals method • Primitives can be compared directly with equality operator (==) • float to Float.floatToIntBits • double to Double.doubleToLongBits • instance of VS getClass • Instance of checks if same class or subclass • May break symmetry requirement • Use only if class final • getClass checks if same class exclusively DesignDecisions equals and hashCode: general advice
hashCode: • Involve significant variables of your object in the calculation of the hash code • Review your hashCode method and check if it is returning equal hash codes for equals objects • Different hash codes for different objects is not mandatory, but advisable DesignDecisions equals and hashCode: general advice
When should two objects be equals? • When they refer to the same object? • Shallow comparison • When although they are different objects, their data members’ values are the same? • Deep comparison Design Decisions equals method in ProGraMo
We should always override toString() • Providing a good toString implementation makes a class much more pleasant to use • toString method should return all of the interesting information contained in the object DesignDecisions toString method
“Effective Java”, by Joshua Bloch. Addison Wesley “Equals and HashCode”, by Manish Hatwalne http://www.geocities.com/technofundo/tech/java/equalhash.html “Junit 4 in 60 Seconds”, by cavdar.net http://www.cavdar.net/2008/07/21/junit-4-in-60-seconds/ Thanks! DECSAI ~ UGR