Illegal Access Error with Mock Private Methods in JUnit

When I write some unit test with Junit and JMockit, I make a mock for some private methods as

MockUp<MyClass> myMethod = new MockUp<MyClass>() {
     @Mock
     private void mockMethod() {
     }
};

however when it runs I got this error.

java.lang.IllegalAccessError: tried to access method path-to-test-file(;path-to-file-which-tested)
	at ...


I’m not exactly sure but probably with Junit-4.8.2 this problem does not happen, however a lower version like Junit-4.7 I got the exception above. If you cannot change the version of Junit like me, solution is simple.

Actually you do not have to write access levels in mock methods. So just delete “private” before “void mockMethod” and it will run without error.

Advertisements
Tagged ,

One thought on “Illegal Access Error with Mock Private Methods in JUnit

  1. Brilliant suggestion !! Works like a charm 🙂
    Saved my day.. thanks a lot.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Advertisements
%d bloggers like this: