java - Avoiding a javax/ejb/EJBLocalObject exception when mocking with PowerMock -
i'm attempting unit test class needs dependency. creating dependency class directly not possible, constructor of dependency has logic needs objects available @ runtime.
however, when attempt mock dependency class, "java.lang.noclassdeffounderror: javax/ejb/ejblocalobject" exception. how can go around this? solution can think of @ moment change classtotest use interface instead of actual concrete dependency class.
some code illustrate how i'm attempting mock dependency:
package mocktest; import org.junit.test; import org.powermock.api.mockito.powermockito; public class mocktest { @test public void performtest() { // mock dependency , create class test dependency dependency = powermockito.mock(dependency.class); classtotest classtotest = new classtotest(dependency); // invoke method in classtotest, assert.. } } further clarification:
q: dependent class impl of interface?
classtotest (the dependent class) concrete class , implements no interfaces, although - control source. dependency class concrete class not implement interfaces , have no control on source.
q: passing in "dependency" object because gets created in normal constructor, providing one, try , privde working mock instead of created one?
yes. classtotest uses methods of dependency cause dependency e.g. make jdbc calls. want able either pass in actual dependency (from implementation code) or mock (from test code).
q: dependency static?
no, dependency or methods in dependency not static.
having class constructor requires ton of dependent objects (singleton static @ runtime, etc.) pain when creating mock tests. best way can think of use combination of powermockito.whennew() call, , mockito.any(class.class) call. way, when constructor called, can hook in objects needed. if follow similar singleton pattern on dependent objects, can mock classes getinstance() calls return copy of mock instantiate in tests.
example:
dependency dependmock = powermockito.mock(dependency.class); powermockito.whennew(dependency.class).withnoarguments().thenreturn(dependmock); or, if dependency needs args passed in 2nd level dependency:
dependency dependmock = powermockito.mock(dependency.class); powermockito.whennew(dependency.class) .witharguments(mockito.any(leveltwodependency.class)).thenreturn(dependmock); note, when using whennew need have hook class testing, code gets fired off. can done adding mock annotations test class.
@runwith(powermockrunner.class) @preparefortest({classyouaretesting.class}) public class classtest{ // code } this allows have full control on 2nd level dependency objects mocks. removing them logic (by providing mocked functionality), core of tests, ends testing pure functionality of class in question. removing odd behavior these dependent classes, remove risk of having bugs in code, end breaking , giving false test results class using dependency classes.
by chainging these mocks, can mock main dependent class , sub-dependency classes using other mocks. can little messy depending on how constrcutors work , you setup private variables these objects.
Comments
Post a Comment