SlideShare una empresa de Scribd logo
1 de 15
EasyMock
Mock object framework for java
development
By Franck Benault
Created 16/11/2014
Last updated 11/11/2014
EasyMock plan
Unit test in java
● Main framework : Junit and TestNG
● Database management (fill or check content)
– DbUnit
– DbSetup
● Mock object frameworks
– EasyMock
– Mockito
Goal of EasyMock
● On the web site of EasyMock (www.easymock.org)
– Great (unit) testing includes isolation
– Isolation involves mock objects
– EasyMock makes mocking easier
What is a mock
Mock objects
● Unit tests are white box tests
– The developers is allowed to look at the code
● Mock objects replaces the objects linked to the object we
want to test
● It becomes possible to test more path (exceptions, errors)
● We have to change a little the implementation to make the
mocking easier
Mock objects
● Unit tests are white box tests
– The developers is allowed to look at the code
● Mock objects replaces the objects linked to the object we
want to test
● It becomes possible to test more path (exceptions, errors)
● We have to change a little the implementation to make the
mocking easier
Features of Mock frameworks
● Main features
– Value returning for a stubbed method
– Exception throwing for a stubbed method
– Invocation check method
– Method arguments check
– Invocation order for one mock check
● Advance features
– Stubbing of equals() and hashCode() methods
– ...
Features of Mock frameworks
● Main features
– Value returning for a stubbed method
– Exception throwing for a stubbed method
– Invocation check method
– Method arguments check
– Invocation order for one mock check
● Advance features
– Stubbing of equals() and hashCode() methods
– ...
Main mock frameworks (in java)
● Open projects well know
– EasyMock
– Jmockit
– Mockito
– Jmock
EasyMock live cycle of mock
objects
● Create a mock
● Set up your expectation
● Set the mock to replay mode
– Call your code under test
● Verify that your expectation have been set
EasyMock create mock (strict of
nice)
● Create a mock
– CreateNiceMock()
● Unexpected returns an empty value (0 or null)
– CreateMock()
● The order of the call is not checked
– CreateStrictMock()
● The order of the call is checked
EasyMock expection/replay/verify
EntityManager emMock =
EasyMock.createNiceMock(EntityManager.class);
EntityTransaction entityTransactionMock =
EasyMock.createNiceMock(EntityTransaction.class);
EasyMock.expect(emMock.getTransaction()).andReturn(entityT
ransactionMock).times(2);
EasyMock.replay(entityTransactionMock);
EasyMock.verify(entityTransactionMock);
EasyMock How to inject the mock
StudentManager StudentQuery
EasyMock How to inject the mock
@RunWith(EasyMockRunner.class)
public class StudentManagerMockTest {
@TestSubject //where to inject mocks
private static StudentManager studentManager;
@Mock(type = MockType.NICE) //mock to inject
private StudentQuery studentQueryMock =
EasyMock.createNiceMock(StudentQuery.class);
.../...

Más contenido relacionado

Similar a Java Mock Object Framework EasyMock Guide

CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!Ortus Solutions, Corp
 
Junit, mockito, etc
Junit, mockito, etcJunit, mockito, etc
Junit, mockito, etcYaron Karni
 
Bytecode Manipulation with a Java Agent and Byte Buddy
Bytecode Manipulation with a Java Agent and Byte BuddyBytecode Manipulation with a Java Agent and Byte Buddy
Bytecode Manipulation with a Java Agent and Byte BuddyKoichi Sakata
 
Angular testing
Angular testingAngular testing
Angular testingYu Jin
 
Behat Workshop at WeLovePHP
Behat Workshop at WeLovePHPBehat Workshop at WeLovePHP
Behat Workshop at WeLovePHPMarcos Quesada
 
Devday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvuDevday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvuPhat VU
 
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...DevDay.org
 
Unit Testing Android Applications
Unit Testing Android ApplicationsUnit Testing Android Applications
Unit Testing Android ApplicationsRody Middelkoop
 
Just Mock It - Mocks and Stubs
Just Mock It - Mocks and StubsJust Mock It - Mocks and Stubs
Just Mock It - Mocks and StubsGavin Pickin
 
How and what to unit test
How and what to unit testHow and what to unit test
How and what to unit testEugenio Lentini
 
Improving Your Selenium WebDriver Tests - Belgium testing days_2016
Improving Your Selenium WebDriver Tests - Belgium testing days_2016Improving Your Selenium WebDriver Tests - Belgium testing days_2016
Improving Your Selenium WebDriver Tests - Belgium testing days_2016Roy de Kleijn
 
Unit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaUnit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaErick M'bwana
 

Similar a Java Mock Object Framework EasyMock Guide (20)

CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!
 
EasyMock for Java
EasyMock for JavaEasyMock for Java
EasyMock for Java
 
Junit, mockito, etc
Junit, mockito, etcJunit, mockito, etc
Junit, mockito, etc
 
Bytecode Manipulation with a Java Agent and Byte Buddy
Bytecode Manipulation with a Java Agent and Byte BuddyBytecode Manipulation with a Java Agent and Byte Buddy
Bytecode Manipulation with a Java Agent and Byte Buddy
 
Mock your way with Mockito
Mock your way with MockitoMock your way with Mockito
Mock your way with Mockito
 
Angular testing
Angular testingAngular testing
Angular testing
 
Mockito
MockitoMockito
Mockito
 
Behat Workshop at WeLovePHP
Behat Workshop at WeLovePHPBehat Workshop at WeLovePHP
Behat Workshop at WeLovePHP
 
Spock pres
Spock presSpock pres
Spock pres
 
Devday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvuDevday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvu
 
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
 
Codemotion 2015 spock_workshop
Codemotion 2015 spock_workshopCodemotion 2015 spock_workshop
Codemotion 2015 spock_workshop
 
Greach 2015 Spock workshop
Greach 2015 Spock workshopGreach 2015 Spock workshop
Greach 2015 Spock workshop
 
Unit Testing Android Applications
Unit Testing Android ApplicationsUnit Testing Android Applications
Unit Testing Android Applications
 
Spring IO 2015 Spock Workshop
Spring IO 2015 Spock WorkshopSpring IO 2015 Spock Workshop
Spring IO 2015 Spock Workshop
 
Just Mock It - Mocks and Stubs
Just Mock It - Mocks and StubsJust Mock It - Mocks and Stubs
Just Mock It - Mocks and Stubs
 
How and what to unit test
How and what to unit testHow and what to unit test
How and what to unit test
 
Improving Your Selenium WebDriver Tests - Belgium testing days_2016
Improving Your Selenium WebDriver Tests - Belgium testing days_2016Improving Your Selenium WebDriver Tests - Belgium testing days_2016
Improving Your Selenium WebDriver Tests - Belgium testing days_2016
 
Unit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaUnit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - Kenya
 
Cnc scala-presentation
Cnc scala-presentationCnc scala-presentation
Cnc scala-presentation
 

Más de fbenault

Property based-testing
Property based-testingProperty based-testing
Property based-testingfbenault
 
Java concurrency
Java concurrencyJava concurrency
Java concurrencyfbenault
 
Assertj-DB
Assertj-DBAssertj-DB
Assertj-DBfbenault
 
Introduction to the language R
Introduction to the language RIntroduction to the language R
Introduction to the language Rfbenault
 
Assertj-core
Assertj-coreAssertj-core
Assertj-corefbenault
 
System rules
System rulesSystem rules
System rulesfbenault
 
Db in-memory
Db in-memoryDb in-memory
Db in-memoryfbenault
 

Más de fbenault (13)

Bdd java
Bdd javaBdd java
Bdd java
 
Property based-testing
Property based-testingProperty based-testing
Property based-testing
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Test ng
Test ngTest ng
Test ng
 
Assertj-DB
Assertj-DBAssertj-DB
Assertj-DB
 
Introduction to the language R
Introduction to the language RIntroduction to the language R
Introduction to the language R
 
Assertj-core
Assertj-coreAssertj-core
Assertj-core
 
Junit
JunitJunit
Junit
 
System rules
System rulesSystem rules
System rules
 
Db in-memory
Db in-memoryDb in-memory
Db in-memory
 
DbSetup
DbSetupDbSetup
DbSetup
 
Guava
GuavaGuava
Guava
 
Java8
Java8Java8
Java8
 

Último

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 productivityPrincipled Technologies
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
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 2024The Digital Insurer
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
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 interpreternaman860154
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
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 2024The Digital Insurer
 
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 Processorsdebabhi2
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
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)wesley chun
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 

Ú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
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
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
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
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
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
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
 
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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

Java Mock Object Framework EasyMock Guide

  • 1. EasyMock Mock object framework for java development By Franck Benault Created 16/11/2014 Last updated 11/11/2014
  • 3. Unit test in java ● Main framework : Junit and TestNG ● Database management (fill or check content) – DbUnit – DbSetup ● Mock object frameworks – EasyMock – Mockito
  • 4. Goal of EasyMock ● On the web site of EasyMock (www.easymock.org) – Great (unit) testing includes isolation – Isolation involves mock objects – EasyMock makes mocking easier
  • 5. What is a mock
  • 6. Mock objects ● Unit tests are white box tests – The developers is allowed to look at the code ● Mock objects replaces the objects linked to the object we want to test ● It becomes possible to test more path (exceptions, errors) ● We have to change a little the implementation to make the mocking easier
  • 7. Mock objects ● Unit tests are white box tests – The developers is allowed to look at the code ● Mock objects replaces the objects linked to the object we want to test ● It becomes possible to test more path (exceptions, errors) ● We have to change a little the implementation to make the mocking easier
  • 8. Features of Mock frameworks ● Main features – Value returning for a stubbed method – Exception throwing for a stubbed method – Invocation check method – Method arguments check – Invocation order for one mock check ● Advance features – Stubbing of equals() and hashCode() methods – ...
  • 9. Features of Mock frameworks ● Main features – Value returning for a stubbed method – Exception throwing for a stubbed method – Invocation check method – Method arguments check – Invocation order for one mock check ● Advance features – Stubbing of equals() and hashCode() methods – ...
  • 10. Main mock frameworks (in java) ● Open projects well know – EasyMock – Jmockit – Mockito – Jmock
  • 11. EasyMock live cycle of mock objects ● Create a mock ● Set up your expectation ● Set the mock to replay mode – Call your code under test ● Verify that your expectation have been set
  • 12. EasyMock create mock (strict of nice) ● Create a mock – CreateNiceMock() ● Unexpected returns an empty value (0 or null) – CreateMock() ● The order of the call is not checked – CreateStrictMock() ● The order of the call is checked
  • 13. EasyMock expection/replay/verify EntityManager emMock = EasyMock.createNiceMock(EntityManager.class); EntityTransaction entityTransactionMock = EasyMock.createNiceMock(EntityTransaction.class); EasyMock.expect(emMock.getTransaction()).andReturn(entityT ransactionMock).times(2); EasyMock.replay(entityTransactionMock); EasyMock.verify(entityTransactionMock);
  • 14. EasyMock How to inject the mock StudentManager StudentQuery
  • 15. EasyMock How to inject the mock @RunWith(EasyMockRunner.class) public class StudentManagerMockTest { @TestSubject //where to inject mocks private static StudentManager studentManager; @Mock(type = MockType.NICE) //mock to inject private StudentQuery studentQueryMock = EasyMock.createNiceMock(StudentQuery.class); .../...