Note that there are some explanatory texts on larger screens.

plurals
  1. POAvoiding a javax/ejb/EJBLocalObject exception when mocking with PowerMock
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>Some code to illustrate how I'm currently attempting to mock the dependency:</p> <pre><code>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.. } } </code></pre> <p>Further clarification:</p> <p>Q: Is the Dependent class an impl of an interface?</p> <p>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. </p> <p>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?</p> <p>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).</p> <p>Q: Is the dependency a static?</p> <p>No, the Dependency or the methods in the Dependency are not static.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload