java.lang.IllegalArgumentException: Could not find the specified Constructor: genmocks.***.***.***DelegateSubclass([])

76 views
Skip to first unread message

Billybear

unread,
Sep 1, 2011, 4:17:39 AM9/1/11
to Android Mock Discussion
Hi,When I use the AndroidMock,I got this error.
The class I want to mock is like this below:
public B extends A{
public B(Context ctx){
super(ctx);
}
other functions....
}
public A {
protected final Context context;

public A(Context ctx){
this.context = ctx;
}
}

when I want to Mock the class B ,I got the above error.
Is it because that the Variable ‘context’ is final and it needs the
Constructor to initializ?

thank you for you reply.

Andrey Fedorov

unread,
Sep 1, 2011, 4:45:50 AM9/1/11
to androi...@googlegroups.com
Hello.

It seems you are trying to create mock using parameterless constructor.

You have to specify papameter. In this case AndroidMock search for parametised constructor.
Try this:

B b = AndroidMock.CreateMock(B.class, ctx);

the ctx variable is a variable of type Context and is not null.

Billybear

unread,
Sep 1, 2011, 10:46:22 PM9/1/11
to Android Mock Discussion
Thank you very much ! I have resolved the problem.
There is another problem,I don't know whether the object can be
mocked.
for example:
Class A{
private B b;
public int fun1(){
return b.fun2();
}
}

Class B{
public int fun2(){
return 100;
}
}
I want to test the function fun1() in class A.And there is a 'b' in
the function and it's type is "B".
So My idea is that I mock a object of B,and set the expected value of
fun2 in "B",and then set the b in A with the mocked object,But the b
in A is private.
Is there a solution? Thanks~

Andrey Fedorov

unread,
Sep 2, 2011, 1:03:01 AM9/2/11
to androi...@googlegroups.com
Hello.

You have to use a dependency injection framework.
I use the RoboGuice one.

But beware to put mocked instances into injection container. The dalvik crashes this case.
I had written about this here:
https://groups.google.com/forum/#!searchin/roboguice/androidmock/roboguice/2t08KAbI_Ok/4booEhqvm60J
but I still have not got any solution. I've found how to avoid this problem, but it requres to create an intermediate mocable class and injection provider.

I can write it more granulary if you want to use RoboGiuce

Stephen Woodward

unread,
Sep 2, 2011, 1:15:26 AM9/2/11
to androi...@googlegroups.com

As an alternative you could do some manual dependency injection by:

1) create a constructor for A that takes parameter B
2) in your test create mock B and use it when instantiating A

You may find this easier than adding new framework components (eg RoboGuice) if you only have a few classes that match your A B example.

Steve

Reply all
Reply to author
Forward
0 new messages