SlideShare uma empresa Scribd logo
1 de 31
Test it! 
Unit, mocking and in-container 
Meet Arquillian! 
Ivan St. Ivanov
Agenda 
Introduction to testing 
Pure unit testing with JUnit 
Stubs and mocks. Mockito library 
In-container testing with Arquillian
Characteristics 
Fast 
Run anywhere 
◦ IDE 
◦ Build tool 
◦ CI server 
Repeatable 
Leverage known tooling
Levels 
Unit 
Functional 
Integration 
Acceptance
Unit testing 
Single unit of code 
In isolation 
Benefits: 
◦ Quick feedback 
◦ Facilitate change 
◦ Document features 
Limitations 
◦ Won’t catch integration errors 
◦ Too much mocking
“ The purpose of automated testing is 
to enable change. Verifying correctness 
is just a nice side effect. 
- Jeremy Norris
Test frameworks
Introduction to JUnit 
No need to extend framework classes 
Annotate test methods with @Test 
Preparation methods with @Before 
and @BeforeClass 
Cleanup activities with @After and 
@AfterClass 
Check with Assert methods 
Extensible with test runners
Example JUnit test case 
public class FooBarTest { 
private Foo foo; 
@Before 
public void setup() { 
foo = new Foo(); 
foo.init(); 
} 
@Test 
public void testBar() throws Exception { 
Assert.assertEquals("bar", foo.getBar()); 
} 
@After 
public void tearDown() { 
foo.cleanup(); 
} 
}
Stubs and mocks 
Units are not isolated 
Stubs 
◦ Test doubles 
◦ Predetermined behavior 
Mocks 
◦ Validate behavior as well 
Unit under test implies dependency 
injection
Dependency injectability 
Sample usecase 
◦ Conference manager 
◦ Cache 
Wrong: 
◦ Create Cache inside Conference 
manager 
◦ Lookup Cache 
Correct 
◦ Constructor or setter injection
Mocking libraries
Greeting earthlings
Core principles 
Tests should be portable to any 
container 
Tests should be executable from both 
IDE and build tool 
The platform should extend existing 
test frameworks
Step 1 – pick a container 
Container extensions 
◦ JBoss, Tomcat, Weld, Glassfish, Jetty, 
WebSphere, WebLogic
Step 2 – connect the 
container 
Container types 
◦ Embedded 
◦ Remote 
◦ Managed
Step 3 – package and deploy 
ShrinkWrap library 
◦ Deployment 
◦ Resolve from Maven 
◦ Create descriptors
Step 4 – run the test 
Tests runs in-container 
◦ CDI, EJB, JNDI available 
◦ No need to mock most of the services 
Present the result as a normal unit test
Step 5 – undeploy the test 
Undeploy the test archive 
Disconnect or stop the container
That’s not all 
Persistence extension 
Warp 
Drone 
Graphene 
AngularJS, Android, OSGi 
…
Resources 
Testing 
http://en.wikipedia.org/wiki/Software_testing 
Unit testing 
http://en.wikipedia.org/wiki/Unit_testing 
Mocks and stubs 
http://martinfowler.com/articles/mocksArentStubs.html 
Arquillian 
http://aslakknutsen.github.io/presentations/ 
https://rpestano.wordpress.com/2014/06/08/arquillian/ 
https://rpestano.wordpress.com/2014/10/25/arquillian-and-mocks/
About this demo 
Slides 
http://bit.ly/bgjug-testing-slides 
Showcase – initial 
http://bit.ly/bgjug-testing 
Showcase – final 
http://bit.ly/bgjug-testing-final

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Test Dependencies and the Future of Build Acceleration
Test Dependencies and the Future of Build AccelerationTest Dependencies and the Future of Build Acceleration
Test Dependencies and the Future of Build Acceleration
 
Android Meetup Slovenija #3 - Testing with Robolectric by Ivan Kust
Android Meetup Slovenija #3 - Testing with Robolectric by Ivan KustAndroid Meetup Slovenija #3 - Testing with Robolectric by Ivan Kust
Android Meetup Slovenija #3 - Testing with Robolectric by Ivan Kust
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End Development
 
ikp321-04
ikp321-04ikp321-04
ikp321-04
 
First adoption hackathon at BGJUG
First adoption hackathon at BGJUGFirst adoption hackathon at BGJUG
First adoption hackathon at BGJUG
 
Java Testing With Spock - Ken Sipe (Trexin Consulting)
Java Testing With Spock - Ken Sipe (Trexin Consulting)Java Testing With Spock - Ken Sipe (Trexin Consulting)
Java Testing With Spock - Ken Sipe (Trexin Consulting)
 
Building XWiki
Building XWikiBuilding XWiki
Building XWiki
 
Integration tests: use the containers, Luke!
Integration tests: use the containers, Luke!Integration tests: use the containers, Luke!
Integration tests: use the containers, Luke!
 
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and MockitoAn Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
 
September 2010 - Arquillian
September 2010 - ArquillianSeptember 2010 - Arquillian
September 2010 - Arquillian
 
Hyper-pragmatic Pure FP testing with distage-testkit
Hyper-pragmatic Pure FP testing with distage-testkitHyper-pragmatic Pure FP testing with distage-testkit
Hyper-pragmatic Pure FP testing with distage-testkit
 
Integration testing
Integration testingIntegration testing
Integration testing
 
ScalaUA - distage: Staged Dependency Injection
ScalaUA - distage: Staged Dependency InjectionScalaUA - distage: Staged Dependency Injection
ScalaUA - distage: Staged Dependency Injection
 
Implementing quality in Java projects
Implementing quality in Java projectsImplementing quality in Java projects
Implementing quality in Java projects
 
Izumi 1.0: Your Next Scala Stack
Izumi 1.0: Your Next Scala StackIzumi 1.0: Your Next Scala Stack
Izumi 1.0: Your Next Scala Stack
 
Maven2交流
Maven2交流Maven2交流
Maven2交流
 
Junit4&testng presentation
Junit4&testng presentationJunit4&testng presentation
Junit4&testng presentation
 
Scala, Functional Programming and Team Productivity
Scala, Functional Programming and Team ProductivityScala, Functional Programming and Team Productivity
Scala, Functional Programming and Team Productivity
 
Mutation Testing DevoxxUK 2021
Mutation Testing DevoxxUK 2021Mutation Testing DevoxxUK 2021
Mutation Testing DevoxxUK 2021
 
Cactus in container_unit_test_framework
Cactus in container_unit_test_frameworkCactus in container_unit_test_framework
Cactus in container_unit_test_framework
 

Semelhante a Test it! Unit, mocking and in-container Meet Arquillian!

190711_Testbirds_Selenium_eclipsecon_FINAL_0.ppt
190711_Testbirds_Selenium_eclipsecon_FINAL_0.ppt190711_Testbirds_Selenium_eclipsecon_FINAL_0.ppt
190711_Testbirds_Selenium_eclipsecon_FINAL_0.ppt
NaviAningi
 
Into The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsInto The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applications
Ortus Solutions, Corp
 

Semelhante a Test it! Unit, mocking and in-container Meet Arquillian! (20)

Getting Started with Selenium
Getting Started with SeleniumGetting Started with Selenium
Getting Started with Selenium
 
Throwing complexity over the wall: Rapid development for enterprise Java (Jav...
Throwing complexity over the wall: Rapid development for enterprise Java (Jav...Throwing complexity over the wall: Rapid development for enterprise Java (Jav...
Throwing complexity over the wall: Rapid development for enterprise Java (Jav...
 
190711_Testbirds_Selenium_eclipsecon_FINAL_0.ppt
190711_Testbirds_Selenium_eclipsecon_FINAL_0.ppt190711_Testbirds_Selenium_eclipsecon_FINAL_0.ppt
190711_Testbirds_Selenium_eclipsecon_FINAL_0.ppt
 
KKSD_Testbirds_Selenium_eclipsecon_FINAL_0.ppt
KKSD_Testbirds_Selenium_eclipsecon_FINAL_0.pptKKSD_Testbirds_Selenium_eclipsecon_FINAL_0.ppt
KKSD_Testbirds_Selenium_eclipsecon_FINAL_0.ppt
 
How to Build Your Own Test Automation Framework?
How to Build Your Own Test Automation Framework?How to Build Your Own Test Automation Framework?
How to Build Your Own Test Automation Framework?
 
Deliver Faster with BDD/TDD - Designing Automated Tests That Don't Suck
Deliver Faster with BDD/TDD - Designing Automated Tests That Don't SuckDeliver Faster with BDD/TDD - Designing Automated Tests That Don't Suck
Deliver Faster with BDD/TDD - Designing Automated Tests That Don't Suck
 
Testing Angular
Testing AngularTesting Angular
Testing Angular
 
Into The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsInto The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applications
 
Test automation proposal
Test automation proposalTest automation proposal
Test automation proposal
 
Level Up Your Integration Testing With Testcontainers
Level Up Your Integration Testing With TestcontainersLevel Up Your Integration Testing With Testcontainers
Level Up Your Integration Testing With Testcontainers
 
Qt test framework
Qt test frameworkQt test framework
Qt test framework
 
Java 201 Intro to Test Driven Development in Java
Java 201   Intro to Test Driven Development in JavaJava 201   Intro to Test Driven Development in Java
Java 201 Intro to Test Driven Development in Java
 
Jakarta EE Test Strategies (2022)
Jakarta EE Test Strategies (2022)Jakarta EE Test Strategies (2022)
Jakarta EE Test Strategies (2022)
 
How do you tame a big ball of mud? One test at a time.
How do you tame a big ball of mud? One test at a time.How do you tame a big ball of mud? One test at a time.
How do you tame a big ball of mud? One test at a time.
 
Testing In Java
Testing In JavaTesting In Java
Testing In Java
 
Testing In Java4278
Testing In Java4278Testing In Java4278
Testing In Java4278
 
Principles and patterns for test driven development
Principles and patterns for test driven developmentPrinciples and patterns for test driven development
Principles and patterns for test driven development
 
Enabling Agile Testing Through Continuous Integration Agile2009
Enabling Agile Testing Through Continuous Integration Agile2009Enabling Agile Testing Through Continuous Integration Agile2009
Enabling Agile Testing Through Continuous Integration Agile2009
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Performance Test Driven Development with Oracle Coherence
Performance Test Driven Development with Oracle CoherencePerformance Test Driven Development with Oracle Coherence
Performance Test Driven Development with Oracle Coherence
 

Último

Último (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

Test it! Unit, mocking and in-container Meet Arquillian!

  • 1. Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov
  • 2. Agenda Introduction to testing Pure unit testing with JUnit Stubs and mocks. Mockito library In-container testing with Arquillian
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8. Characteristics Fast Run anywhere ◦ IDE ◦ Build tool ◦ CI server Repeatable Leverage known tooling
  • 9. Levels Unit Functional Integration Acceptance
  • 10. Unit testing Single unit of code In isolation Benefits: ◦ Quick feedback ◦ Facilitate change ◦ Document features Limitations ◦ Won’t catch integration errors ◦ Too much mocking
  • 11. “ The purpose of automated testing is to enable change. Verifying correctness is just a nice side effect. - Jeremy Norris
  • 13. Introduction to JUnit No need to extend framework classes Annotate test methods with @Test Preparation methods with @Before and @BeforeClass Cleanup activities with @After and @AfterClass Check with Assert methods Extensible with test runners
  • 14. Example JUnit test case public class FooBarTest { private Foo foo; @Before public void setup() { foo = new Foo(); foo.init(); } @Test public void testBar() throws Exception { Assert.assertEquals("bar", foo.getBar()); } @After public void tearDown() { foo.cleanup(); } }
  • 15.
  • 16. Stubs and mocks Units are not isolated Stubs ◦ Test doubles ◦ Predetermined behavior Mocks ◦ Validate behavior as well Unit under test implies dependency injection
  • 17. Dependency injectability Sample usecase ◦ Conference manager ◦ Cache Wrong: ◦ Create Cache inside Conference manager ◦ Lookup Cache Correct ◦ Constructor or setter injection
  • 19.
  • 21. Core principles Tests should be portable to any container Tests should be executable from both IDE and build tool The platform should extend existing test frameworks
  • 22. Step 1 – pick a container Container extensions ◦ JBoss, Tomcat, Weld, Glassfish, Jetty, WebSphere, WebLogic
  • 23. Step 2 – connect the container Container types ◦ Embedded ◦ Remote ◦ Managed
  • 24. Step 3 – package and deploy ShrinkWrap library ◦ Deployment ◦ Resolve from Maven ◦ Create descriptors
  • 25. Step 4 – run the test Tests runs in-container ◦ CDI, EJB, JNDI available ◦ No need to mock most of the services Present the result as a normal unit test
  • 26. Step 5 – undeploy the test Undeploy the test archive Disconnect or stop the container
  • 27.
  • 28. That’s not all Persistence extension Warp Drone Graphene AngularJS, Android, OSGi …
  • 29.
  • 30. Resources Testing http://en.wikipedia.org/wiki/Software_testing Unit testing http://en.wikipedia.org/wiki/Unit_testing Mocks and stubs http://martinfowler.com/articles/mocksArentStubs.html Arquillian http://aslakknutsen.github.io/presentations/ https://rpestano.wordpress.com/2014/06/08/arquillian/ https://rpestano.wordpress.com/2014/10/25/arquillian-and-mocks/
  • 31. About this demo Slides http://bit.ly/bgjug-testing-slides Showcase – initial http://bit.ly/bgjug-testing Showcase – final http://bit.ly/bgjug-testing-final