Your SlideShare is downloading. ×
Test Infected
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Test Infected

786
views

Published on

#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 …

#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.

Published in: Technology

2 Comments
1 Like
Statistics
Notes
No Downloads
Views
Total Views
786
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
18
Comments
2
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. #testinfected Mert ÇALIŞKAN Feb 2013 @ankarajug #testinfected
  • 2. #testinfected is:Programmers that LOVE Writing Tests...so infected with of testing @ankarajug #testinfected
  • 3. AnkaraJUG first event @ November 2012... Founding Fathers: Barış BAL Çağatay ÇİVİCİ Mert ÇALIŞKANhttp://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.trOpen Source Software Advocate, Founder, ImplementorMember of Apache SoftwareFoundation and OpenLogic ExpertCommunityAuthor ofPrimeFaces Cookbookfrom PacktPub tr.linkedin.com/in/mertcaliskan @ankarajug #testinfected
  • 5. We are hiring..!Send Resumes to mert@t2.com.tr @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 involvedwith was SOOOO damn low. So, Let’s increase these numbers...!! @ankarajug #testinfected
  • 8. DISCLAIMERI’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
  • 9. How many Test Infected Zombies among us?Raise your hand..! @ankarajug #testinfected
  • 10. Do we need to TEST? @ankarajug #testinfected
  • 11. Your Software...after some coding... @ankarajug #testinfected
  • 12. Your Software...after some coding... @ankarajug #testinfected
  • 13. Your Software...after some coding... @ankarajug #testinfected
  • 14. So again,Do we need to TEST? @ankarajug #testinfected
  • 15. webster says that... 1test |tɛst|nouna procedure intended to establish the quality,performance, or reliability of something, especiallybefore it is taken into widespread use... @ankarajug #testinfected
  • 16. webster says that... 1test |tɛst|nouna procedure intended to establish the quality,performance, or reliability of something,especially before it is taken into widespread use... @ankarajug #testinfected
  • 17. The Wh- QuestionsWHATWHEREWHENWHYHOWWHO @ankarajug #testinfected
  • 18. The Wh- QuestionsWHAT 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 LocalGo 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 jUnitIntegration Tests jUnit along with custom implementationsMock Tests mockito, hamcrest...Functional Tests selenium, soapUI @ankarajug #testinfected
  • 22. How to test The Boxing ApproachBlackBox Testing you don’t know the internals of the system a.k.a. Acceptance TestingWhiteBox Testing Tester knows the design/implementation of the system a.k.a. Unit/Integration TestingGrayBox Testing Black Box mixed with White Box :) @ankarajug #testinfected
  • 23. Unit TestingIn unit testing, youre just verifying that a single unitworks according to expectations.Unit tests are not interactive. Once written, they canbe run without further input.Each test should be responsible for its own actions notinterfere with each other. @ankarajug #testinfected
  • 24. Common Rules for Testing FrameworksEach unit test must run independently of all otherunit 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. JUnitFounders, Kent Beck and Erich Gamma, like ~14 years ago.Kent Beck the creator of sUnit and tossed the coin onthe 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 @ankarajughttps://gist.github.com/mulderbaba/5013544 #testinfected
  • 27. 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
  • 28. The Simplest Testpublic 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 @Afterpublic 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 Questionpublic 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 Questionpublic 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 Questionpublic 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"); } } @ankarajughttps://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"); } @ankarajughttps://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"); } @ankarajughttps://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"); } } @ankarajughttps://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"); } } @ankarajughttps://gist.github.com/mulderbaba/5014568 #testinfected
  • 39. My Way or the Way For Unit Testingsurefire Attached to test phase For Integration Testsfailsafe Attached to pre-integration-test phase and others... @ankarajug #testinfected
  • 40. testinfected demo @ github2 projects (demo + template) available at http://github.com/mulderbaba/testinfectedfork it and enjoy it.. demo is for demo as it is named... template is a brand new beginning...template for all theframeworks with testing infrastructure... PrimeFaces CXF Hamcrest Mockito Spring JPA Hibernate jUnit @ankarajug #testinfected
  • 41. Code Walkthrough Unit Testing @ankarajug #testinfected
  • 42. 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
  • 43. Integration TestsFramework 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 QAlways 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?@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
  • 47. Quartz Scheduling TestingWell 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
  • 48. Code WalkthroughIntegration Testing @ankarajug #testinfected
  • 49. Anatomy of a Mock TestIt’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@Testpublic void addInvokedSuccessfully() { impl.add(4, 5); verify(dao).save(any(CalculationResult.class));} @ankarajug #testinfected
  • 51. 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
  • 52. Anatomy of a Mock TestAlways use static imports while using Matchers...Also see other annotations, @Spy @Captorlittle something something on the topic:http://www.jroller.com/mert/entry/anatomy_of_a_mock_test @ankarajug #testinfected
  • 53. Functional TestingEnough with coding..! let’s see that it just works.. mode on...UI Testing with Selenium with the IDEWeb Service Testing with SoapUI Functional Testing Load TestingjMeter Performance Testing @ankarajug #testinfected
  • 54. Code Walkthrough Mock Testing Functional Testing(SoapUI, jMeter, Selenium) @ankarajug #testinfected
  • 55. Continuous Integration leads to Continuous DeliverySo you have 10.000 tests...Congratz..!But...They’ll be useless if developers need torun them by hand every time..!We need a build triggered with eachcommit, right? @ankarajug #testinfected
  • 56. Continuous Integration Tools out in the field HudsonJenkins @ankarajug #testinfected
  • 57. Release Life Cycle DemystifiedPre-Alpha } Release } Candidate General Alpha Availability (GA) Release to Manufacturing (RTM) Beta @ankarajug #testinfected
  • 58. Release Life CyclePre-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
  • 60. Continuous Integration DEMO w/ Jenkins @ankarajug #testinfected
  • 61. Continuous InspectionMetric 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
  • 62. Continuous Inspection DEMO w/ Sonar @ankarajug #testinfected
  • 63. Continuous Integration and Inspection DEMO with BAMBOO and CLOVER Kerem ÇAĞLAR Solveka Yazılım @ankarajug #testinfected
  • 64. Best PracticesUse 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 someonelike,retrieveListStatusByNationalIdReturnsOneElementWhenThereIsManuallyAddedPersonWithExemptedInfo() @ankarajug #testinfected
  • 65. SomeWhenever you are tempted to type something into a printstatement 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. SomeWe do not need private methods, get rid of them..!Run the fast tests first.. group them.. If they fail you’ll getnotified 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 thekeyboard monkey... @ankarajug #testinfected
  • 67. You cannot test EVERYTHING..! @ankarajug #testinfected
  • 68. @ankarajug#testinfected