简体   繁体   中英

EasyMock - Mock a class having other class object and .class as parameters

I am currently using EasyMock and trying a lot to fix my problem.

Let me explain you in the simple words, Totally I have 5 classes.

  • Main.java
  • A.java
  • B.java
  • C.java
  • Result.java

From Main.java I am calling a method of A.java

objectOfA.someMethod("String",objectofB, C.class);

While Mocking...

expect( objectofA.someMethod( "given some String", Prepared some dummy object of B and passing , C.class ) ).andReturn( objectofResult ).anyTimes();

While I am running the application its giving AssersionError.

My Questions:

  1. In the expect method > while calling objectofA.someMethod(), In the second parameter I am creating one dummy object of B, actually B has nearly 10 variables and among them one will be generated randomly, but while creating the dummy object I am giving some value of my own, will it be a problem ?

  2. 3rd parameter, we have to pass C.class, How can we pass this ?

  3. Finally in andReturn() I am passing objectofResult class, do I need to parpare the dummy object of Result in this case ? because in Result.java we have nearly 20 variables and its really difficult for me to guess the values.

In Result.java I just need to verify a single String variable value.

I am trying a lot since 3 days, can someone help me to fix this please.

We can't tell you what is "right" for your code; because we don't have the full picture (and you should not expect that somebody will spent his time to dive into all your code if you would have posted it).

So, for your questions:

1) +2) I think you are getting it backward. The point is: you tell EasyMock about the invocations that your "code under test" should make.

This means: you tell EasyMock that someMethod should be invoked; and EasyMock will check if the actual invocation matches your specification.

So, it really depends on your implementation of your B class. If the B object that your actual code passes with someMethod() is equal to the B object that you provide in the EasyMock staging; than all is fine. But if they are not equal, then EasyMock will complain. You can change that for example by using matchers, like

EasyMock.expect(
  yourMock.someMethod(
    eq("string to match"), anyObject(B.class), ...) 

(where eq, anyObject being static methods in EasyMock).

But the problem there is: if you use matchers; you have to use matchers on all your arguments. And (for the moment); I am not aware of a matcher that would work for the "class" argument. So, for now I can only advise to do something like:

EasyMock.expect(
  yourMock.someMethod("string to match", expectedB, C.class))

where "expectedB" is a B object that you setup in advance; so that it matches what is created by your code under test. In order to get there, you will have to make sure that your B class as a "good" equals() method.

3) Again; the question what your "dummy result" is capable of; depends on how your code under test will be using it.

Example: assume that your code under test will invoke .toString() on that result object. Then you might want to prepare for that; for example by making that result object ... be another mock; that expects calls to toString().

Long story short: you use EasyMock to specify everything that you expect to "come out" of your class under test; and to control everything that "flows into" that class under test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM