Professional Documents
Culture Documents
Agenda
Testing overview Double objects Test Driven Development Best practices Some great tools
Testing overview
Why we test?
Test types
Unit tests Integration tests Functional tests Performance tests Acceptance tests
Unit tests
Double objects
Double objects?!
Dummy object
Passed as argument but never used. Just to fill parameter lists.
Fake object
Working implementations, but take some shortcut which makes them not suitable for production.
Stub
Pre-programmed with responses to calls made during tests. Sometimes with a memory.
http://martinfowler.com/
Mock
Pre-programmed with expectations which form a specification of the calls they are expected to receive.
Security Solutions for a Changing World 9
Mockito
10
Drink drink = new Drink("martini", null); Drink drink = new Drink("martini", new VolumeImpl(0)); Drink drink = new Drink("martini", new DummyVolume());
11
12
AtomicClock fakeAtomicClock = new AtomicClock() { @Override public long getTime() { return System.nanoTime(); } }
13
14
class SmtpServerStub implements SmtpServer { @Override public boolean send(Email email) { count++; return true; } int count = 0; int count() { return count; } }
Security Solutions for a Changing World 15
16
class SmtpServerStub implements SmtpServer { @Override public boolean send(Email email) { count++; return true; } int count = 0; int count() { return count; } }
Security Solutions for a Changing World 17
18
19
Demo Mockito
20
21
TDD history
Officially developed by Kent Beck Related to the test-first programming concepts of XP (1999) Now, created more general interest in its own right First book in 2002
22
Whats TDD?
Add a Test
23
Whats TDD?
24
TDD philosophy
Programming by intention Mastering requirements
Implement only identified requirements Decomposing requirements into a test list Write only what we know how to test
Emergent Design
Start with the simplest design/architecture Evolve it if needed
Others
KISS (Keep It Simple, Stupid) YAGNI (You Aint Gonna Need It) DRY (Dont Repeat Yourself)
25
Demo TDD
26
Best practices
27
Readability Tips
28
29
divide_2ndParamIsZero_ThrowException()
30
public class CustomerDaoTest { @Test public void testGetNameById() { CustomerDao customerDao = new CustomerDao(); JpaUtils.injectEntityManager(customerDao); String name = customerDao.getGetNameById(1); assertEquals(name, "Kevin"); } }
31
32
Tip 3: Setup
@Test public void assertCardProfileNfc_IsNfc_DoNothing() throws CardProfileNotNfcException { EligibilityService eligibilityService = new EligibilityServiceBean(); CardProfile cardProfile = new CardProfile(); cardProfile.setSwpHciVersion(SWP_HCI_VERSION); eligibilityService.setCardProfile(cardProfile); eligibilityService.assertCardProfileNfc(); }
33
Tip 3: Setup
private EligibilityService eligibilityService; @Before public void setUp() { eligibilityService = new EligibilityServiceBean(); } @Test public void assertCardProfileNfc_IsNfc_DoNothing() throws CardProfileNotNfcException { CardProfile cardProfile = new CardProfile(); cardProfile.setSwpHciVersion(SWP_HCI_VERSION); eligibilityService.setCardProfile(cardProfile); eligibilityService.assertCardProfileNfc(); }
34
35
Map<Date, BigDecimal> charges = new HashMap<Date, BigDecimal>(); charges.put(DATE_DEBUT_13_03_2011, new BigDecimal(QUANTITE_1000)); charges.put(addDaysMonthsToDate(DATE_DEBUT_13_03_2011, 2), new BigDecimal(QUANTITE_3000));
37
Map<Date, BigDecimal> charges = new MapCharges<Date>() .with(DATE_DEBUT_13_03_2011, QUANTITE_1000) .and(addDaysMonthsToDate(DATE_DEBUT_13_03_2011, 2), QUANTITE_3000) .build();
38
39
40
41
42
assertEquals( "Service not equal: " + result.getId() + "/" + result.getServId() + "/" + result.getVersion(), result, expectedService);
43
Site siteNord = createSite(NORD, AFFECTATION_NORD); Site siteSud = createSite(SUD, AFFECTATION_SUD); int affectations = dao.selectAffectationsNord(siteNord, siteSud, SITE_NULL); assertThat(affectations).isEqualTo(AFFECTATION_NORD);
Security Solutions for a Changing World 44
45
46
Robustness Tips
47
Tip 1: No duplication
Hide useless information
Creation Method Creation Builder
48
public final class MessageFormat { public static String getPScript(String slot, String serviceLabel, String passPhrase, byte[] key, long counterValue, String salt) throws SASException; }
49
50
"isEligible");
51
52
53
SASException {
54
Some tools
55
MoreUnit
Open source Eclipse plugin http://moreunit.sourceforge.net/ See whats tested Jump to test Run current test Refactoring friendly Generate tests and Mocks
56
infinitest
Open source Eclipse plugin http://infinitest.github.com/ Continuous testing Run all impacted tests after each code change
57
58
Fest Assert
Open source framework http://fest.easytesting.org/ More readable and fluent assertions Method chaining pattern
59
assertThat(companies).onProperty("status").containsOnly(VALID);
60
61
Books
62