Nit Tests: Object Oriented Programming
Nit Tests: Object Oriented Programming
http://softeng.polito.it/courses/09CBI
§ Non-commercial. You may not use this work for commercial purposes.
§ No Derivative Works. You may not alter, transform, or build upon this
work.
§ For any reuse or distribution, you must make clear to others the
license terms of this work.
§ Any of these conditions can be waived if you get permission from the
copyright holder.
Your fair use and other rights are in no way affected by the above.
2
JUnit
§ JUnit is a testing framework for Java
programs
w Written by Kent Beck and Erich Gamma
§ It is a framework with unit-testing
functionalities
§ Integrated in Eclipse development
Environment
http://www.junit.org
Unit Testing
§ Unit testing is particularly important
when software requirements change
frequently
w Code often has to be refactored to
incorporate the changes
w Unit testing helps ensure that the
refactored code continues to work
JUnit Framework
§ JUnit helps the programmer:
w Define and execute tests and test suites
w Formalize requirements and clarify
architecture
w Write and debug code
w Integrate code and always be ready to
release a working version
History
§ 1997 on the plane to OOPSLA97 Kent
Beck and Erich Gamma wrote JUnit
§ Junit.org – August 2000
§ Junit 3.8.1 – September 2002
§ Junit 4.0 – February 2006
w Latest release: 4.12 - Dec 2012
§ Junit 5.0 – September 2017
6
What JUnit does
§ For each test (method) JUnit
w calls pre-test fixture
– Intended to acquire resources and create any
objects that may be needed for testing
w calls the test method
w calls post-test fixtures
– Intended to release resources and remove any
objects you created
Test method
§ A test method doesn’t return a result
§ The test method performs operations
and checks the results
§ Checks are performed using a set of
assert*() method
§ The JUnit framework detects the
anomalies and deals with them
assert*() methods
assertTrue(boolean test)
assertFalse(boolean test)
assertEquals(expected, actual)
assertSame(Object expected,
Object actual)
assertNotSame(Object expected,
Object actual)
assertNull(Object object)
assert*() methods
§ For a condition
assertTrue(condition)
w If the tested condition is
– true => proceed with execution
– false => abort the test method execution,
prints out the optional message
assert*() methods
assertNotNull(Object object)
fail()
w All the above may take an optional String
message as the first argument, e.g.
static void assertTrue(
String message,
boolean test)
assert*()
§ For objects, int, long, byte:
assertEquals( expected, actual)
w Ex. assertEquals( 2 , unoStack.size() );
§ For floating point values:
assertEquals(expected,actual,err)
w Ex. assertEquals(1.0, Math.cos(3.14),
0.01);
JUnit 3
SYNTAX
13
Test suites
§ Allow running a group of related tests
§ To do so, group your test methods in
a class which extends TestSuite
TestSuite
§ Combine many test cases in a test suite:
JUnit tests for Counter…
public void testIncrement() {
assertTrue(counter1.increment()== 1);
assertTrue(counter1.increment()== 2);
}
public void testDecrement() {
assertTrue(counter1.decrement()==-1);
}
} // End from last slide
SYNTAX
27
JUnit 4
§ Make use of java annotations
w Less constraints on names
w Easier to read/write
§ Backward compatible with JUnit 3
§ Assertions
w assert*() methods
w assertThat() method
– To use the Hamcrest matchers
28
Test a Stack (JUnit4) Any class
@RunWith(Suite.class)
@SuiteClasses({
TestStack.class, AnotherTest.class
})
public class AllTests { }
JUnit 4 Annotations
§ @Test
w Marks test methods
§ @Before and @After
w Mark pre and post fixtures
§ Test suites require:
§ @RunWith(Suite.class)
§ @Suite.SuiteClasses({ … })
34
JUnit 4 Packages and classes
§ All classes are in packages org.junit
§ Assertions are made available with
w import static org.junit.Assert.*;
§ Annotations have to be imported as
w import org.junit.After;
w import org.junit.Before;
w import org.junit.Test;
35
36
Counter test with Junit 4
@Test
public void testGetCounterInitial() {
assertEquals(0,counter.getCount()); }
@Test
public void testIncrement() {
assertEquals(1,counter.increment());
assertEquals(2,counter.increment()); }
@Test
public void testDecrement() {
assertEquals(-1,counter.decrement()); }
}
37
@RunWith(Suite.class)
@SuiteClasses({ CounterTests.class })
public class AllTests { }
38
ECLIPSE JUNIT PLUG-IN
…use JUnit
45
Test-Driven Development
§ Specify a portion of the feature yet to be
coded
§ Run the test and see it fail (red bar)
§ Write code until the tests pass (green
bar)
§ Repeat until whole feature implemented
§ Refactor
w Keeping the bar green
Bug reproduction
§ When a bug is reported
§ Specify the expected correct outcome
§ See the test fail
w Reproduce the bug
§ Modify the code until the bug-fix tests
pass.
§ Check for regressions
47
Guidelines
§ Test should be written before code
§ Test everything that can break
§ Run tests as often as possible
48
Limitations of unit testing
§ JUnit is designed to call methods and
compare the results they return
against expected results
w This ignores:
– Programs that do work in response to
GUI commands
– Methods that are used primary to
produce output
Why JUnit
§ Allow you to write code faster while increasing
quality
§ Elegantly simple
§ Check their own results and provide immediate
feedback
§ Tests is inexpensive
§ Increase the stability of software
§ Developer tests
§ Written in Java
§ Free
§ Gives proper uniderstanding of unit testing
References
§ K.Beck, E.Gamma. Test Infected:
Programmers Love Writing Tests
w http://members.pingnet.ch/gamma/
junit.htm
§ Junit home page
w https://junit.org
§ Hamcrest matchers
w http://hamcrest.org/JavaHamcrest/
53