#testinfected, Java test yöntemlerini sizin için inceliyor. Sunumda testin amaçları, JUnit ile test, mock test yöntemleri, arayüz testi, web servis vb. gibi framework testi, sürekli entegrasyon ve denetleme yöntemleri, test kapsama oranının arttırılması ve ürün kalitesi nasıl arttırılır konuları hakkında detaylı bilgiler edinebilirsiniz.
3. AnkaraJUG
first event @ November 2012...
Founding Fathers:
Barış BAL
Çağatay ÇİVİCİ
Mert ÇALIŞKAN
http://bit.ly/ankarajugKAYIT
ankarajug.blogspot.com
facebook.com/ankarajug
twitter.com/ankarajug @ankarajug
#testinfected
4. Mert ÇALIŞKAN
10+ years of experience w/ Java
Coder @ T2.com.tr
Open Source Software Advocate, Founder, Implementor
Member of Apache Software
Foundation and OpenLogic Expert
Community
Author of
PrimeFaces Cookbook
from PacktPub
tr.linkedin.com/in/mertcaliskan @ankarajug
#testinfected
6. What will I talk about?
Objectives of Testing
What to Test, How to Test with
Java Testing Frameworks
Unit, Integration, Mock and Functional Testing
Intensive Code Samples w/ github project
Test Automation with,
{
Integration
Continuous Inspection
Concluded with Delivery
Best Practices and Mottos
@ankarajug
#testinfected
7. Do you really need to
listen to me?
Please DO..!
The test coverage for the projects that I involved
with was SOOOO damn low.
So,
Let’s increase these numbers...!!
@ankarajug
#testinfected
8. DISCLAIMER
I’m a DEVELOPER..
so,
I develop code to test my code...
So you will see
- coding best practices that I gathered
rather than
- silver bullets, documentation procedures & etc.
which is cool, I suppose...
@ankarajug
#testinfected
9. How many Test Infected
Zombies among us?
Raise your hand..!
@ankarajug
#testinfected
15. webster says that...
1
test |tɛst|
noun
a procedure intended to establish the quality,
performance, or reliability of something, especially
before it is taken into widespread use...
@ankarajug
#testinfected
16. webster says that...
1
test |tɛst|
noun
a procedure intended to establish the quality,
performance, or reliability of something,
especially before it is taken into widespread use...
@ankarajug
#testinfected
18. The Wh- Questions
WHAT to test?
WHERE should we test it?
WHEN should we test it?
WHY do we need to test?
HOW should we test it?
WHO tests it?
@ankarajug
#testinfected
19. WHAT to Test?
Unit Tests
every method...with its every line of code...
Integration Tests
DAO tests, framework tests...
(web services, hibernate search and etc)
Mock Tests
service layer, mocking each other :)
Functional Tests
UI testing, WS testing
@ankarajug
#testinfected
20. WHERE should we test it?
BlackBox WhiteBox SmokeTest
=
Prod Staging Test Local
Go for 3 different locations other than local
if it’s applicable.
Try to make Prod and Staging identical...
@ankarajug
#testinfected
21. HOW should we test it?
Unit Tests
jUnit
Integration Tests
jUnit along with custom implementations
Mock Tests
mockito, hamcrest...
Functional Tests
selenium, soapUI
@ankarajug
#testinfected
22. How to test
The Boxing Approach
BlackBox Testing
you don’t know the internals of the system
a.k.a. Acceptance Testing
WhiteBox Testing
Tester knows the design/implementation of the
system
a.k.a. Unit/Integration Testing
GrayBox Testing
Black Box mixed with White Box :)
@ankarajug
#testinfected
23. Unit Testing
In unit testing, you're just verifying that a single unit
works according to expectations.
Unit tests are not interactive. Once written, they can
be run without further input.
Each test should be responsible for its own actions not
interfere with each other.
@ankarajug
#testinfected
24. Common Rules for
Testing Frameworks
Each unit test must run independently of all other
unit tests.
Errors must be detected and reported test by test.
It must be easy to define which unit tests will run.
JUnit
@ankarajug
#testinfected
25. JUnit
Founders, Kent Beck and Erich Gamma,
like ~14 years ago.
Kent Beck the creator of sUnit and tossed the coin on
the term, XP.
Major differences between 3.x and 4.x
new packaging, annotations and etc...
The Latest and Greatest of jUnit is: 4.11 (use it)
@ankarajug
#testinfected
26. The Simplest Test
t
public class Example {
@Test
T e s
public void method() {
u re
}
ix t
org.junit.Assert.assertTrue(new ArrayList().isEmpty());
}
F
@ankarajug
https://gist.github.com/mulderbaba/5013544 #testinfected
27. The Simplest Test
public class Example {
@Test
public void method() {
org.junit.Assert.assertTrue(new ArrayList().isEmpty());
}
}
What happens above?
- jUnit creates a new instance of Example class.
- It invokes the method() which is annotated with Test
@ankarajug
#testinfected
28. The Simplest Test
public class Example {
@Test
public void method() {
org.junit.Assert.assertTrue(new ArrayList().isEmpty());
}
}
@Test Annotation bundles 2 optional parameters.
expected: the exception that the method should throw.
timeout: the amount of time in ms for the test to fail.
@ankarajug
#testinfected
29. Preparing and Tearing down for tests
@Before
@BeforeClass
@After
@AfterClass
@ankarajug
#testinfected
30. Sample @Before and @After
public class SampleTest {
@Before
public void setup() {
/ prepare some stuff..
/
}
@Test
public void checkIfTheCodeWorksOkOrNot() {
/ Some assertions and bla bla...
/
}
@After
public void tearDown() {
/ tear down all...
/
}
}
@ankarajug
#testinfected
31. @BeforeClass and @AfterClass with Question
public class SampleTest {
@BeforeClass
Question is:
public void setup() {
/ prepare some stuff..
/ Will it compile?
}
@Test
public void checkIfTheCodeWorksOkOrNot() {
/ Some assertions and bla bla...
/
}
@AfterClass
public void tearDown() {
/ tear down all...
/
}
}
@ankarajug
#testinfected
32. @BeforeClass and @AfterClass with Question
public class SampleTest {
@BeforeClass
Question is:
public void setup() { YES..!
/ prepare some stuff..
/ Will it compile?
}
@Test
public void checkIfTheCodeWorksOkOrNot() {
/ Some assertions and bla bla...
/
}
@AfterClass
public void tearDown() {
/ tear down all...
/
}
}
@ankarajug
#testinfected
33. @BeforeClass and @AfterClass with Question
public class SampleTest {
@BeforeClass
But,
Question is:
public void setup() { YES..!
/ prepare some stuff..
/ will it compile?
Will it work?
}
@Test
public void checkIfTheCodeWorksOkOrNot() {
/ Some assertions and bla bla...
/
}
@AfterClass
public void tearDown() {
/ tear down all...
/
}
}
@ankarajug
#testinfected
34. Multiple methods with @Before or @After annotations?
public class BeforeOrderSample {
@Before
public void finalSetup() {
System.out.println("final stuff");
}
Question is:
@Test Will it work?
public void doTheTest() {
System.out.println("test test test"); If so,
} which method
works first?
@Before
public void initialSetup() {
System.out.println("initial stuff");
}
}
@ankarajug
https://gist.github.com/mulderbaba/5013850 #testinfected
35. Let’s introduce some inheritance :)
@Before
public void classASetup() {
Class A }
System.out.println("class A setup");
@After
public void classATearDown() {
System.out.println("class A tear down");
}
@Before
public void classBSetup() {
System.out.println("class B setup"); @Test
}
public void doTheTest() {
Class B @After
System.out.println("test test test");
}
public void classBTearDown() {
System.out.println("class B tear down");
}
@ankarajug
https://gist.github.com/mulderbaba/5014153 #testinfected
36. Let’s introduce some inheritance :)
@Before 1
public void classASetup() {
Class A }
System.out.println("class A setup");
@After 5
public void classATearDown() {
System.out.println("class A tear down");
}
@Before 2
public void classBSetup() {
System.out.println("class B setup"); @Test
3
}
public void doTheTest() {
Class B @After
4 System.out.println("test test test");
}
public void classBTearDown() {
System.out.println("class B tear down");
}
@ankarajug
https://gist.github.com/mulderbaba/5014153 #testinfected
37. Test Methods’ Execution Order
public class TestCasesExecutionOrder {
@Test
public void secondTest() {
System.out.println("Executing second test");
}
Question is:
@Test
public void firstTest() { What’s the
System.out.println("Executing first test"); execution order?
}
@Test
public void thirdTest() {
System.out.println("Executing third test");
}
}
@ankarajug
https://gist.github.com/mulderbaba/5014568 #testinfected
38. Test Methods’ Execution Order
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestCasesExecutionOrder {
@Test
public void secondTest() {
System.out.println("Executing second test");
}
Question is:
@Test
public void firstTest() { What’s the
System.out.println("Executing first test"); execution order?
}
@Test
public void thirdTest() {
System.out.println("Executing third test");
}
}
@ankarajug
https://gist.github.com/mulderbaba/5014568 #testinfected
39. My Way or the Way
For Unit Testing
surefire Attached to test phase
For Integration Tests
failsafe Attached to pre-integration-test phase and
others...
@ankarajug
#testinfected
40. testinfected demo @ github
2 projects (demo + template) available at
http://github.com/mulderbaba/testinfected
fork it and enjoy it..
demo is for demo as it is named...
template is a brand new beginning...template for all the
frameworks with testing infrastructure...
PrimeFaces CXF Hamcrest Mockito
Spring
JPA Hibernate jUnit
@ankarajug
#testinfected
43. Integration Tests
Framework testing is one of the coolest things...
You can even test Web Services like,
Get Jetty UP
Create a client
Did a WS call
Assert the result
@ankarajug
#testinfected
44. First Rules First...
Differentiate your DB’s...
PROD DEV TEST
Use an ORM (JPA, Hibernate, bla bla)
But WHY o’WHY...?
@ankarajug
#testinfected
45. First Rules First...
Differentiate your DB’s...
PROD DEV TEST
C LE SQL L DB
ORA MY HS Q
Always go for in memory DB for Testing environment.
Everything will vanish so you can do anything you want.
And you can easily tests frameworks like Hibernate Search
@ankarajug
#testinfected
46. Case Study: Quartz Scheduling
Testing
Question is: How would you test it?
@Service
public class SomePrettySophisticatedJob {
@Autowired
private SomePrettySophisticatedService1 service;
private SomePrettySophisticatedService2 service;
private SomePrettySophisticatedService3 service;
@Scheduled(fixedDelay=500)
public void execute() {
//Some heavy duty coding...
service calls, calls, calls, computations and bla bla..
}
}
@ankarajug
#testinfected
47. Quartz Scheduling Testing
Well you don’t need to test it, it’s a framework remember?
and since it’s a well-known one, just use it for scheduling.
It will work..trust me..
@Service
public class SomePrettySophisticatedJob {
@Autowired
private GatheredPrettySophisticatedService service;
@Scheduled(fixedDelay=500)
public void execute() {
service.execute();
}
} @ankarajug
#testinfected
49. Anatomy of a Mock Test
It’s good for service layer testing.
Set your expectations, do the service layer call,
verify the calls and assert the results.
@RunWith(MockitoJUnitRunner.class)
public class CalculatorServiceImplTest {
@Mock
private CalculatorDao dao;
@InjectMocks
private CalculatorServiceImpl impl = new CalculatorServiceImpl();
@ankarajug
#testinfected
50. Anatomy of a Mock Test
@Test
public void addInvokedSuccessfully() {
impl.add(4, 5);
verify(dao).save(any(CalculationResult.class));
}
@ankarajug
#testinfected
51. Anatomy of a Mock Test
@Test
public void addInvokedSuccessfully() {
when(helper.retrieveSomeStuff()).
thenReturn(someNiceInstance);
impl.add(4, 5);
verify(dao).save(any(CalculationResult.class));
}
@ankarajug
#testinfected
52. Anatomy of a Mock Test
Always use static imports while using Matchers...
Also see other annotations,
@Spy
@Captor
little something something on the topic:
http://www.jroller.com/mert/entry/anatomy_of_a_mock_test
@ankarajug
#testinfected
53. Functional Testing
Enough with coding..!
let’s see that it just works.. mode on...
UI Testing with Selenium with the IDE
Web Service Testing with SoapUI
Functional Testing
Load Testing
jMeter Performance Testing
@ankarajug
#testinfected
55. Continuous Integration leads to
Continuous Delivery
So you have 10.000 tests...
Congratz..!
But...
They’ll be useless if developers need to
run them by hand every time..!
We need a build triggered with each
commit, right?
@ankarajug
#testinfected
57. Release Life Cycle
Demystified
Pre-Alpha
}
Release
}
Candidate
General
Alpha Availability
(GA)
Release to
Manufacturing
(RTM)
Beta
@ankarajug
#testinfected
58. Release Life Cycle
Pre-Alpha More of an idea in development...
with nightly builds..milestones..
testing phase ox
eB x
Working draft but probably w/ lots W k hit Bo
Alpha lac Box
of bugs...do extensive testing on it...! B ey
feature freeze Gr
The prototype..features are complete..!
Beta
first time available to the outsiders
(open/closed beta)
The radiation level is high..! but not relevant of course, since
these are the first 2 letters of greek alphabet..
@ankarajug
#testinfected
59. Release Life Cycle
code complete
Release all features implemented/tested
Candidate call for a “code complete". you have the
candidate..!
ready to release
Release to You are ready to go mass manufacturing.
Manufacturing hardware + software bundled together.
(RTM)
ready to advertise
General You are now live..! you did all the commercial
Availability
(GA)
stuff and you are good to go.. just sell it..!!!
@ankarajug
#testinfected
61. Continuous Inspection
Metric dashboard for,
Test & Code Coverage (cobertura)
Coding Violations (checkstyle)
Triggering at each build so you’ll have incremental reports.
For installation and getting it up and running you need to,
configure its DB,
hook it with Jenkins to make more sense...
@ankarajug
#testinfected
63. Continuous Integration and Inspection
DEMO with
BAMBOO and CLOVER
Kerem ÇAĞLAR
Solveka Yazılım @ankarajug
#testinfected
64. Best Practices
Use Matchers with static imports...
Assert.assertEquals(result.getResult().doubleValue(), 9.0);
assertThat(result.getResult(), is(9.0));
Test method names can be long, no worries.
They should be understood when read by someone
like,
retrieveListStatusByNationalIdReturnsOneElementWh
enThereIsManuallyAddedPersonWithExemptedInfo()
@ankarajug
#testinfected
65. Some
Whenever you are tempted to type something into a print
statement or a debugger expression, write it as a test instead
(Martin Fowler)
Code without automated test simply doesn’t exist.
Once you get them running, make sure they stay running.
If you can’t test it, just trash it.
@ankarajug
#testinfected
66. Some
We do not need private methods, get rid of them..!
Run the fast tests first.. group them.. If they fail you’ll get
notified early.
Test your app with different localizations (for sorting and etc.)
Increase your coverage for sleeping tight at night.
Keep the bar green to keep the code clean.
You want to test the code, not yourself. Don’t be the
keyboard monkey...
@ankarajug
#testinfected