easymock

Download Easymock

If you can't read please download the document

Upload: fbenault

Post on 16-Apr-2017

1.655 views

Category:

Technology


1 download

TRANSCRIPT

EasyMockMock object framework for java development

By Franck Benault

Created 16/11/2014Last 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 frameworksEasyMock

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 testsThe 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 testsThe 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 featuresValue returning for a stubbed method

Exception throwing for a stubbed method

Invocation check method

Method arguments check

Invocation order for one mock check

Advance featuresStubbing of equals() and hashCode() methods

...

Features of Mock frameworks

Main featuresValue returning for a stubbed method

Exception throwing for a stubbed method

Invocation check method

Method arguments check

Invocation order for one mock check

Advance featuresStubbing of equals() and hashCode() methods

...

Main mock frameworks (in java)

Open projects well knowEasyMock

Jmockit

Mockito

Jmock

EasyMock live cycle of mock objects

Create a mock

Set up your expectation

Set the mock to replay modeCall your code under test

Verify that your expectation have been set

EasyMock create mock (strict of nice)

Create a mockCreateNiceMock()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(entityTransactionMock).times(2);EasyMock.replay(entityTransactionMock);EasyMock.verify(entityTransactionMock);

EasyMock How to inject the mock

StudentManagerStudentQuery

EasyMock How to inject the mock

@RunWith(EasyMockRunner.class)public class StudentManagerMockTest {@TestSubject //where to inject mocksprivate static StudentManager studentManager;@Mock(type = MockType.NICE) //mock to injectprivate StudentQuery studentQueryMock = EasyMock.createNiceMock(StudentQuery.class);.../...