Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do we mock objects #26

Open
ravi-polampelli opened this issue Sep 14, 2012 · 5 comments
Open

How do we mock objects #26

ravi-polampelli opened this issue Sep 14, 2012 · 5 comments

Comments

@ravi-polampelli
Copy link

Let us say a method requires some values from another object or from some other service class.
How do we mock those values and provide test data for those values?
For ex:
In a method, they take Timestamp value from a facade and use that value as part of preparing criteria query in it, please note this Timestamp value is not a parameter. It would be better to somehow inject the list of timestamps while calling the method, Timestamps list would have a list including leap year dates, 31st Dec, 1st Jan etc..

Please think over it.

@anujgandharv
Copy link
Member

So, if I understand it correctly, the scenario is that we are testing Service A and Service A calls Service B's getTimestamps() method which returns mock Timestamp values. If this is the case then its easily covered by using EasyMock framework that can do this for us.

@ravi-polampelli
Copy link
Author

Can you put the gist of design?

Sent from my iPhone

On 16 Sep 2012, at 10:41, Anuj Kumar [email protected] wrote:

So, if I understand it correctly, the scenario is that we are testing Service A and Service A calls Service B's getTimestamps() method which returns mock Timestamp values. If this is the case then its easily covered by using EasyMock framework that can do this for us.


Reply to this email directly or view it on GitHub.

@anujgandharv
Copy link
Member

Heres a pseudo code of how you can do it:

class TestMockService{
private ServiceA serviceA;
private ServiceB testSubject;
private List<DataTime> timeStamps;

@Before
public void setup() {
   serviceA = EasyMock.createMock(ServiceA.class);
   testSubject = new ServiceB();
   testSubject.setServiceA(serviceA);
   timeStamps = new ArrayList<DateTime>();
}

 @Test 
 public void testMock(){
    EasyMock.expect(serviceA.getTimeStamps()).andReturn(timeStamps);
    EasyMock.replay(serviceA);
    testSubject.getValuewithTimeStampFromServiceB();
}

@ravi-polampelli
Copy link
Author

Ya, we can mock objects in this way.
But my question is, how can we populate that timestamp data from our
external test data file.
Is this possible?

Regards,
Ravi

On Mon, Sep 17, 2012 at 2:50 PM, Anuj Kumar [email protected]:

Heres a pseudo code of how you can do it:

class TestMockService{
private ServiceA serviceA;
private ServiceB testSubject;
private List timeStamps;

@before
public void setup() {
serviceA = EasyMock.createMock(ServiceA.class);
testSubject = new ServiceB();
testSubject.setServiceA(serviceA);
timeStamps = new ArrayList();
}

@test
public void testMock(){
EasyMock.expect(serviceA.getTimeStamps()).andReturn(timeStamps);
EasyMock.replay(serviceA);
testSubject.getValuewithTimeStampFromServiceB();
}


Reply to this email directly or view it on GitHubhttps://github.com//issues/26#issuecomment-8614717.

@anujgandharv
Copy link
Member

Currently there is no specific support for such a thing. And it would be a good idea to have such a support. Although, I am first inclined to finish the CSVWriter and XMLWriter functionality and cleaning up the DataDrivenTest Class slightly before introducing any new functionality. I have started work on XMLWriter already. Can you look at the CSVWriter if and when you get some time?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants