Issue in Mocking constructor

531 views
Skip to first unread message

vikas n

unread,
Jun 10, 2011, 5:57:54 AM6/10/11
to powe...@googlegroups.com
Hi Johan,


This is regarding the issue that i am facing in mocking a constructor.

I have two different constructors for a class with different arguments as below (though it doesn't make sense) :

public ClassA(Long relationshipId) { this.relationshipId = relationshipId; } public ClassA(long relationshipId) { this.relationshipId = Long.valueOf(relationshipId); }

When i use the expectNew call as below to mock the object, it gives me the error as mentioned below
expectNew(ClassA.class,21L).andReturn(mockClassA)

Error:
org.powermock.reflect.exceptions.TooManyConstructorsFoundException: Several matching constructors found, please specify the argument parameter types so that PowerMock can determine which method you're referring to. Matching constructors in class com.att.osd.framework.core.model.relation.DimRelationship were: com.att.osd.framework.core.model.relation.ClassA( java.lang.Long.class ) com.att.osd.framework.core.model.relation.ClassA( long.class )

I read about this issue in the group and found that we can use suppressconstructor through one of your example stated below
suppress(constructor(EvilParent.class));

I am not able find the method "suppress(constructor())" can you please let me know where can i find it.
I couldn't find the method in MemberModifier, please refer to the attached image file.

I had to use anyObject(Shown below) as an aletrnate for the time being.
mockClassB.relateTo((ClassC)anyObject(),(ClassA)anyObject());

Also let me know if there is any other solution for the prob.

Thanks in Advance
Vikas.N





suppress.JPG

Johan Haleby

unread,
Jun 10, 2011, 9:15:58 AM6/10/11
to powe...@googlegroups.com
Hi, 

If PowerMock cannot determine which constructor you refer to you need to provide the parameter types using this method:

expectNew(ClassA.class, new Class<?>[]{ Long.class }, 21L).andReturn(mockClassA);

Regarding your second questions it's stated in the docs that you should statically import methods from:
  • org.powermock.api.support.membermodification.MemberModifier
  • org.powermock.api.support.membermodification.MemberMatcher
I.e. the constructor(..) method you're looking for is located in org.powermock.api.support.membermodification.MemberMatcher.

/Johan


--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/powermock?hl=en.

Reply all
Reply to author
Forward
0 new messages