Test Infected
-
Upload
mert-caliskan -
Category
Technology
-
view
837 -
download
2
description
Transcript of Test Infected
#testinfectedMert ÇALIŞKAN
Feb 2013
@ankarajug #testinfected
#testinfected is:
@ankarajug #testinfected
Programmers that LOVE Writing Tests...so infected with of testing
@ankarajug #testinfected
AnkaraJUG
ankarajug.blogspot.com
facebook.com/ankarajug
twitter.com/ankarajug
first event @ November 2012...
Founding Fathers: Barış BAL Çağatay ÇİVİCİ Mert ÇALIŞKAN
http://bit.ly/ankarajugKAYIT
@ankarajug #testinfected
Mert ÇALIŞKAN
Open Source Software Advocate, Founder, Implementor
Member of Apache Software Foundation and OpenLogic ExpertCommunity
Author of PrimeFaces Cookbook from PacktPub
10+ years of experience w/ Java
Coder @ T2.com.tr
tr.linkedin.com/in/mertcaliskan
@ankarajug #testinfected
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,
Concluded with Best Practices and Mottos
Continuous{ IntegrationInspectionDelivery
@ankarajug #testinfected
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
DISCLAIMER
I’m a DEVELOPER.. so, I develop code to test my code...So you will see - coding best practices that I gatheredrather than - silver bullets, documentation procedures & etc.
which is cool, I suppose...
@ankarajug #testinfected
How many Test Infected Zombies among us?
Raise your hand..!
@ankarajug #testinfected
Do we need to TEST?@ankarajug #testinfected
@ankarajug #testinfected
Your Software...after some coding...
@ankarajug #testinfected
Your Software...after some coding...
@ankarajug #testinfected
Your Software...after some coding...
@ankarajug #testinfected
So again, Do we need to TEST?
@ankarajug #testinfected
@ankarajug #testinfected
test 1 |tɛst|noun
a procedure intended to establish the quality, performance, or reliability of something, especially before it is taken into widespread use...
webster says that...
@ankarajug #testinfected
test 1 |tɛst|noun
a procedure intended to establish the quality, performance, or reliability of something, especially before it is taken into widespread use...
webster says that...
@ankarajug #testinfected
The Wh- Questions
WHAT
WHERE
WHEN
WHY
HOW
WHO
to test?
should we test it?
should we test it?
do we need to test?
should we test it?
tests it?
@ankarajug #testinfected
The Wh- Questions
WHAT
WHERE
WHEN
WHY
HOW
WHO
to test?
should we test it?
should we test it?
do we need to test?
should we test it?
tests it?
@ankarajug #testinfected
WHAT to Test?Unit Tests
Integration Tests
Mock Tests
Functional Tests
every method...with its every line of code...
DAO tests, framework tests...(web services, hibernate search and etc)
service layer, mocking each other :)
UI testing, WS testing
@ankarajug #testinfected
WHERE should we test it?
Go for 3 different locations other than local if it’s applicable.
Try to make Prod and Staging identical...
LocalStagingProd Test
=
BlackBox WhiteBox SmokeTest
@ankarajug #testinfected
HOW should we test it?Unit Tests
Integration Tests
Mock Tests
Functional Tests
jUnit
jUnit along with custom implementations
mockito, hamcrest...
selenium, soapUI
@ankarajug #testinfected
BlackBox Testing
you don’t know the internals of the systema.k.a. Acceptance Testing
WhiteBox Testing
Tester knows the design/implementation of the systema.k.a. Unit/Integration Testing
GrayBox Testing Black Box mixed with White Box :)
How to testThe Boxing Approach
@ankarajug #testinfected
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
Common Rules forTesting 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
JUnitFounders, 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
The Simplest Test
public class Example {
@Test public void method() { org.junit.Assert.assertTrue(new ArrayList().isEmpty()); }}
Test
Fixture
https://gist.github.com/mulderbaba/5013544
@ankarajug #testinfected
The Simplest Testpublic 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
The Simplest Test
@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.
public class Example { @Test public void method() { org.junit.Assert.assertTrue(new ArrayList().isEmpty()); }}
@ankarajug #testinfected
Preparing and Tearing down for tests
@Before
@After@AfterClass
@BeforeClass
@ankarajug #testinfected
public class SampleTest {
@Before public void setup() { // prepare some stuff.. }
@Testpublic void checkIfTheCodeWorksOkOrNot() { // Some assertions and bla bla...}
@After public void tearDown() { // tear down all... }}
Sample @Before and @After
@ankarajug #testinfected
public class SampleTest {
@BeforeClass public void setup() { // prepare some stuff.. }
@Testpublic void checkIfTheCodeWorksOkOrNot() { // Some assertions and bla bla...}
@AfterClass public void tearDown() { // tear down all... }}
@BeforeClass and @AfterClass with Question
Question is: Will it compile?YES..!But,
will it work?
@ankarajug #testinfected
public class SampleTest {
@BeforeClass public void setup() { // prepare some stuff.. }
@Testpublic void checkIfTheCodeWorksOkOrNot() { // Some assertions and bla bla...}
@AfterClass public void tearDown() { // tear down all... }}
@BeforeClass and @AfterClass with Question
Question is: Will it compile?YES..!But,
will it work?
@ankarajug #testinfected
public class SampleTest {
@BeforeClass public void setup() { // prepare some stuff.. }
@Testpublic void checkIfTheCodeWorksOkOrNot() { // Some assertions and bla bla...}
@AfterClass public void tearDown() { // tear down all... }}
@BeforeClass and @AfterClass with Question
Question is: Will it compile?YES..!But,will it work?
@ankarajug #testinfected
public class BeforeOrderSample { @Before public void finalSetup() { System.out.println("final stuff"); }
@Test public void doTheTest() { System.out.println("test test test"); }
@Before public void initialSetup() { System.out.println("initial stuff"); }}
Multiple methods with @Before or @After annotations?
https://gist.github.com/mulderbaba/5013850
Question is: Will it work?
If so, which method works first?
@ankarajug #testinfected
Let’s introduce some inheritance :)
Class A
Class B
https://gist.github.com/mulderbaba/5014153
@Before public void classASetup() { 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"); }
@After public void classBTearDown() { System.out.println("class B tear down"); }
@Test public void doTheTest() { System.out.println("test test test"); }
@ankarajug #testinfected
Let’s introduce some inheritance :)
Class A
Class B
https://gist.github.com/mulderbaba/5014153
@Before public void classASetup() { 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"); }
@After public void classBTearDown() { System.out.println("class B tear down"); }
@Test public void doTheTest() { System.out.println("test test test"); }
1
23
4
5
@ankarajug #testinfected
Test Methods’ Execution Order
public class TestCasesExecutionOrder {
@Test public void secondTest() { System.out.println("Executing second test"); }
@Test public void firstTest() { System.out.println("Executing first test"); }
@Test public void thirdTest() { System.out.println("Executing third test"); }}
Question is:
What’s the execution order?
https://gist.github.com/mulderbaba/5014568
@ankarajug #testinfected
Test Methods’ Execution Order
public class TestCasesExecutionOrder {
@Test public void secondTest() { System.out.println("Executing second test"); }
@Test public void firstTest() { System.out.println("Executing first test"); }
@Test public void thirdTest() { System.out.println("Executing third test"); }}
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
Question is:
What’s the execution order?
https://gist.github.com/mulderbaba/5014568
@ankarajug #testinfected
My Way or the Way
surefire
failsafe
For Unit TestingAttached to test phase
For Integration TestsAttached to pre-integration-test phase and others...
@ankarajug #testinfected
testinfected demo @ github
2 projects (demo + template) available at
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...
JPA
PrimeFaces
HibernateSpring
CXF Hamcrest
jUnit
Mockito
http://github.com/mulderbaba/testinfected
@ankarajug #testinfected
Code Walkthrough
Unit Testing
@ankarajug #testinfected
Integration Tests
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = {"/applicationContext-test.xml"})@TransactionConfiguration(defaultRollback = false, transactionManager = "transactionManager")
public abstract class BaseIntegrationTestCase {}
What’s with this class?
@ankarajug #testinfected
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
Differentiate your DB’s...
PROD DEV TEST
First Rules First...
Use an ORM (JPA, Hibernate, bla bla)
But WHY o’WHY...?
@ankarajug #testinfected
First Rules First...
PROD DEV TEST
ORACLE
MYSQL HSQLDB
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
Differentiate your DB’s...
@ankarajug #testinfected
Case Study: Quartz Scheduling Testing
Question is: How would you test it?
@Servicepublic 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
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..
@Servicepublic class SomePrettySophisticatedJob { @Autowired private GatheredPrettySophisticatedService service; @Scheduled(fixedDelay=500) public void execute() { service.execute(); }}
@ankarajug #testinfected
Code Walkthrough
Integration Testing
@ankarajug #testinfected
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
Anatomy of a Mock Test
@Testpublic void addInvokedSuccessfully() { impl.add(4, 5);
verify(dao).save(any(CalculationResult.class));}
@Testpublic void addInvokedSuccessfully() {
when(helper.retrieveSomeStuff()). thenReturn(someNiceInstance);
impl.add(4, 5);
verify(dao).save(any(CalculationResult.class));}
@ankarajug #testinfected
Anatomy of a Mock Test
@Testpublic void addInvokedSuccessfully() {
when(helper.retrieveSomeStuff()). thenReturn(someNiceInstance);
impl.add(4, 5);
verify(dao).save(any(CalculationResult.class));}
@ankarajug #testinfected
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
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
Code Walkthrough
Mock TestingFunctional Testing
(SoapUI, jMeter, Selenium)
@ankarajug #testinfected
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
Continuous Integration Tools out in the field
Hudson
Jenkins
@ankarajug #testinfected
Release Life CycleDemystified
Pre-Alpha
Alpha
Beta
Release Candidate
Release to Manufacturing
(RTM)
General Availability
(GA)} }
@ankarajug #testinfected
Release Life CyclePre-Alpha
Alpha
Beta
More of an idea in development... with nightly builds..milestones..
Working draft but probably w/ lots of bugs...do extensive testing on it...!
The prototype..features are complete..! 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..
White Box
Black B
ox
Grey Box
feature freeze
testing phase
@ankarajug #testinfected
Release Life Cycleall features implemented/testedcall for a “code complete". you have the candidate..!
You are ready to go mass manufacturing.hardware + software bundled together.
You are now live..! you did all the commercial stuff and you are good to go.. just sell it..!!!
Release Candidate
Release to Manufacturing
(RTM)
General Availability
(GA)
code complete
ready to release
ready to advertise
@ankarajug #testinfected
Continuous Integration DEMO w/ Jenkins
@ankarajug #testinfected
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
Continuous Inspection DEMO w/ Sonar
@ankarajug #testinfected
Continuous Integration and Inspection DEMO with
BAMBOO and CLOVER
Kerem ÇAĞLARSolveka Yazılım
@ankarajug #testinfected
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,retrieveListStatusByNationalIdReturnsOneElementWhenThereIsManuallyAddedPersonWithExemptedInfo()
@ankarajug #testinfected
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.
Some
@ankarajug #testinfected
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...
Some
@ankarajug #testinfected@ankarajug #testinfected
You cannot test EVERYTHING..!
@ankarajug #testinfected