开发者

How do I use jMock's ClassImposteriser for Android unit testing?

开发者 https://www.devze.com 2023-03-08 09:02 出处:网络
In my unit test, I\'ve tried the following: import org.jmock.Mockery; import org.jmock.Expectations; import org.jmock.lib.legacy.ClassImposteriser;

In my unit test, I've tried the following:

import org.jmock.Mockery;
import org.jmock.Expectations;
import org.jmock.lib.legacy.ClassImposteriser; 

public class MyActivityTest extends ActivityUnitTestCase<MyActivity> {
    private Mockery context = new Mockery() {{
        setImposteriser(ClassImposteriser.INSTANCE);
    }};

    ...
}

My intended use is to mock my project's Application subclass. However, when I run my tests, I get an java.lang.ExceptionInInitializerError. Can I not use the ClassImposteriser ext开发者_JAVA技巧ension for running Android unit tests?


This is something I looked into extensively several months ago. Unfortunately the dalvik VM currently does not support the bytecode manipulations that are required to mock concrete classes.

So you won't be able to use any mocking library to mock a class. You'll have to extract an interface for each class you want to mock in your android tests and mock the interface instead.

Some further reading about the davlik limitations:

  • http://code.google.com/p/mockito/issues/detail?id=57
  • https://sites.google.com/site/androiddevtesting/
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号