The document discusses unit testing and provides examples of how to implement unit tests for Java classes using the JUnit framework. Some key points include:
- JUnit is an open source unit testing framework for Java that makes writing and maintaining unit tests easier. It uses test cases and test suites to test classes and the whole system.
- To unit test a class, a developer first builds a test class extending JUnit and writes test methods. They then stub out the target class and write code to make the tests pass.
- Examples show how to write test cases to test methods, setup and tear down code, and use assertions to validate test outcomes.
- The Spring framework can also be used for dependency
2. Reading
▪ Optional
– http://www.junit.org
– JUnit
Test
Infected:
Programmers
Love
Writing
Tests
3. ▪ Extreame
Programming
Philosophy:
– “Build
a
little,
test
a
little”
▪ All
classes
have
unit
testing
classes
– Run
on
regular
basis
▪ Unit
test
– A
structured
set
of
tests
that
exercises
the
methods
of
a
class
▪ Test
Driven
Development
–
TDD
Background
4. ▪ Is
this
your
code?
Big
ball
of
Mud
Background
5. ▪ Why
is
unit
testing
a
good
idea?
▪ Reasons:
– Gives
the
developer
confidence
that
the
class
is
working
properly
– Quickly
finds
the
source
of
bugs
– Focuses
the
developer
on
the
design
of
a
class
– Helps
define
the
requirements
of
the
individual
classes
Why Unit Testing
6. ▪ Why
is
unit
testing
a
good
idea?
▪ And
the
really
good
reason:
– In
large
application,
changes
and
re-‐factoring
are
possible
Why Unit Testing
7. 1. Build
a
unit
test
to
test
a
class
during
design
This
will
test
the
design
Focuses
the
developer
on
the
design
of
a
class
2. Stub
out
the
class
All
tests
will
fail
since
there
is
no
code!
3. Write
the
functionality
in
the
class
Until
the
test
will
pass
Building the Tests
8. ▪ An
open
source
Java
unit
testing
tool
– Developed
by
Erich
Gamma
and
Kent
Beck
▪ Location:
– www.junit.org
▪ JUnit
tests
does
not:
– Take
the
place
of
functional
testing
– Separate
data
from
test
code
(look
at
JUnit++)
– Can’t
unit
test
Swing
GUIs,
JSPs
or
HTML
(look
at
JUnit
extensions)
JUnit
9. ▪ Java
based
testing
framework
to
make
writing
and
maintaining
unit
tests
easier
▪ Uses
testcases
and
testsuits
to
build
a
hierarchy
of
tests
which
can
be
run
to
test
the
whole
system
or
individual
modules/classes
▪ The
developer
creates
the
unit
tests
using
JUnit
utilities,
extends
JUnit
test
classes
and
uses
a
test
runner
to
run
them
What is JUnit?
10. package test;
import junit.framework.TestCase;
import junit.framework.Assert;
import domain.Money;
public class MoneyTestCase extends TestCase {
private Money f12CHF;
private Money f14CHF;
protected void setUp () {
f12CHF = new Money(12, "CHF");
f14CHF = new Money(14, "CHF");
}
public void testSimpleAdd() {
Money expected = new Money(26,"CHF");
Money results = f12CHF.add(f14CHF);
Assert.assertTrue(expected.equals(results));
}
Test Case
11. public void testEquals ()
{
Assert.assertTrue(!f12CHF.equals(null));
Assert.assertEquals(f12CHF, f12CHF);
Assert.assertEquals(f12CHF, new Money (12, "CHF"));
Assert.assertTrue(!f12CHF.equals(f14CHF));
}
}
Test Case
12.
13. @Test
public void multiplicationOfZeroIntegersShouldReturnZero() {
// MyClass is tested
MyClass tester = new MyClass();
// Tests
assertEquals("10 x 0 must be 0", 0, tester.multiply(10, 0));
assertEquals("0 x 10 must be 0", 0, tester.multiply(0, 10));
assertEquals("0 x 0 must be 0", 0, tester.multiply(0, 0));
}
Annotations
14. package test;
import junit.framework.TestSuite;
public class TestRunner
{
public static void main(String[] args)
{
// the testcase
String[] testCaseName = { MoneyTestCase.class.getName()};
// testrunner
junit.swingui.TestRunner.main(testCaseName);
//junit.textui.TestRunner.main(testCaseName);
}
}
Test Case Runner
27. @Test
public void testUser()
{
Player player0 = new Player(0, "messi", "Messi");
Player player1 = new Player(1, "ronaldo", "Ronaldo");
service.addPlayer(player0);
service.addPlayer(player1);
// Test if add works
Player playerNew = service.getPlayer(1);
assertSame(playerNew, player1);
Player Example
28. // Test if same username fails
try
{
service.addPlayer(player1);
}
catch (ServiceException s)
{
assertSame(ServiceException.class, s.getClass());
}
}
}
Player Example