0

I'm using the newest versions of junit and jmockit and Oracle JDK 7 in Eclipse. When I try to mock java.net.URL my test won't run.

I have in my code something like:

URL url = new URL("String representing the url.");

So I figured in my test I could mock this like so:

@Mocked private URL _url;

Since this works for pretty much everything else, I know URL is final but I thought that was okay with JMockit.

When I run a test class with the above mock in eclipse the result is a grey line(as opposed to green or red.) So I'm assuming some kind of initialization problem. The rest of the test or code doesn't seem to matter, no matter what I put that @Mocked line in, this happens.

A workaround would be great, an explanation of what is actually causing this would be even better. Any help is definitely appreciated! Thanks!

Quick example. This actually gives an exception, but I think it is basically doing the same thing I have seen:

package demo;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class Connecting {
    public boolean connectionattempt() throws IOException {
    URL url = new URL("http://nowhere/");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        if (connection != null) {
            return true;
        }
        else {
            return false;
        }
    }
}

And this test:

package demo;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import org.junit.Test;
import mockit.Expectations;
import mockit.Mocked;
import mockit.Tested;

public class TestConnecting {
    @Mocked URL _url;
    @Mocked HttpURLConnection _connection;
    @Tested Connecting _sut;

    @Test
    public void testConnect() throws IOException {
        new Expectations() { {
            _url.openConnection(); result = _connection;
        } };
        assertEquals(true, _sut.connectionattempt());
    }
}

and the stack trace:

java.lang.NoClassDefFoundError: org/eclipse/jdt/internal/junit/runner/TestReferenceFailure
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestListener.testFailure(JUnit4TestListener.java:91)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestListener.testFailure(JUnit4TestListener.java:69)
    at org.junit.runner.notification.RunNotifier$4.notifyListener(RunNotifier.java:139)
    at org.junit.runner.notification.RunNotifier$SafeNotifier.run(RunNotifier.java:61)
    at org.junit.runner.notification.RunNotifier.fireTestFailures(RunNotifier.java:134)
    at org.junit.runner.notification.RunNotifier.fireTestFailure(RunNotifier.java:128)
    at org.junit.internal.runners.model.EachTestNotifier.addFailure(EachTestNotifier.java:23)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:315)
    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)
Mimerr
  • 390
  • 1
  • 5
  • 14
  • I can't see much benefit in mocking a `URL` object. Why not just create a concrete one? If you get a grey line, then an exception must have occurred. Can you post that stack trace? In fact, can you post a compilable example to demonstrate the problem? – Duncan Jones Dec 12 '14 at 15:51
  • In general we try to mock every object that is not the system under test. The specific benefit to mocking the URL object would be when url.openConnection() is called, to return a mocked HttpURLConnection. When I run it I see no exceptions or stack traces, it is like the test just doesn't initialize, it doesn't fail. Due to time, I'm going to alter the way the connection is set up and use a concrete URL as you suggest though. I was curious as to why the mock would cause this though. I saw a forum post that said something about URL being initialized in JVM before JMockit gets a change to mock it – Mimerr Dec 12 '14 at 16:16
  • My mistake, I forgot about `openConnection()`. Can you post a quick example test that fails in the manner you describe? Supposing we can't fix this for some reason, you could encapsulate the `URL` object in a class that you could then mock. – Duncan Jones Dec 12 '14 at 16:17
  • Added a quick class/test and stacktrace. I could have sworn it wasn't throwing the error before. But it seems to basically be the same problem. If you comment out the mocking/stubbing of the url object, it passes. Probably not the greatest example... but all I could think of for now. – Mimerr Dec 12 '14 at 19:53

1 Answers1

0

I executed the test on Eclipse Kepler SR2, IntelliJ IDEA 13.1, and Netbeans 8.0.1, using JMockit 1.13, JUnit 4.11, and Oracle JDK 1.7.0_67.

The test passes in every case, it's all green! So, I don't know what could possibly be the problem in your environment. Are you sure the "newest version" of JMockit (1.13 at this time) was the one actually used?

Rogério
  • 16,171
  • 2
  • 50
  • 63
  • It is definitely 1.13. It must be something in my environment then I guess. I'll come back and comment with any updates if I figure anything out but I changed some stuff and worked around this, so am not sure when I'll get back to it. So I'm going to just accept this as the answer for now. Thanks. – Mimerr Jan 09 '15 at 15:51