I'm attempting to unit test a class that needs a dependency. Creating the dependency class directly is not possible, as the constructor of the dependency has logic that needs objects only available at runtime.
However, when I attempt to mock the dependency class, I get a "java.lang.NoClassDefFoundError: javax/ejb/EJBLocalObject" exception. How can I go around this? The only solution I can think of at the moment is to change the ClassToTest to use an interface instead of the actual concrete Dependency class.
Some code to illustrate how I'm currently attempting to mock the dependency:
package mockTest;
import org.junit.Test;
import org.powermock.api.mockito.PowerMockito;
public class MockTest {
@Test
public void performTest() {
// Mock the dependency and create the class to test
Dependency dependency = PowerMockito.mock(Dependency.class);
ClassToTest classToTest = new ClassToTest(dependency);
// Invoke a method in classToTest, assert..
}
}
Further clarification:
Q: Is the Dependent class an impl of an interface?
ClassToTest (the dependent class) is a concrete class and implements no interfaces, although it easily could - I control the source. The Dependency class is a concrete class that does not implement interfaces and I have no control over the source.
Q: Are you only passing in the "dependency" object because it gets created in the normal constructor, so by providing one, you try and privde the working Mock instead of the created one?
Yes. ClassToTest uses methods of Dependency that cause the Dependency to e.g. make JDBC calls. I want to be able to either pass in the actual Dependency (from the implementation code) or a mock (from the test code).
Q: Is the dependency a static?
No, the Dependency or the methods in the Dependency are not static.