New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking of InitialContext not working in @Tested instantiation #168

Closed
thwaerte opened this Issue Apr 27, 2015 · 0 comments

Comments

2 participants
@thwaerte

thwaerte commented Apr 27, 2015

I want to test a class that does a new InitialContext().lookup(String) in its constructor:

public class A {
    public A() throws NamingException {
        new InitialContext().lookup("something");
    }
}

If A is instantiated via @tested, InitialContext is not properly mocked. A NoInitialContextException is raised on calling lookup()

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
    at javax.naming.InitialContext.lookup(InitialContext.java:411)
    at A.<init>(A.java:21)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

public class ATest {
    @Mocked
    InitialContext initialContext;
    @Tested
    A tested;
    @Test
    public void test() {
    }
}

Using the MockUp API does not help:

public class AMockUpTest {
    @Tested
    A tested;
    @Before
    public void thisDoesNotWork() {
        new InitialContextMock();
    }
    @Test
    public void test() {
    }
    static class InitialContextMock extends MockUp<InitialContext> {
        @Mock
        public void $init() {
        }
        @Mock
        public Object lookup(final String name) {
            return new Object();
        }
    }
}

If class A is instantiated in a @before method, everything seems to be working.

public class A2Test {
    @Mocked
    InitialContext initialContext;
    @Tested
    A tested;
    @Before
    public void thisWorks() throws NamingException {
        tested = new A();
    }
    @Test
    public void test() {
    }
}
public class AMockUp2Test {
    @Tested
    A tested;
    @Before
    public void thisWorks() throws NamingException {
        new InitialContextMock();
        tested = new A();
    }
    @Test
    public void test() {
    }
    static class InitialContextMock extends MockUp<InitialContext> {
        @Mock
        public void $init() {
        }
        @Mock
        public Object lookup(final String name) {
            return new Object();
        }
    }
}

@rliesenfeld rliesenfeld added the bug label Apr 27, 2015

@rliesenfeld rliesenfeld self-assigned this Apr 27, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment