jmockit / jmockit1

Advanced Java library for integration testing, mocking, faking, and code coverage
Other
465 stars 240 forks source link

Partial mocking when Injecting? #81

Closed ColinHebert closed 10 years ago

ColinHebert commented 10 years ago

I have this code which was working with jmockit 1.10, and has a strange behaviour since 1.12:

package test;

import mockit.Injectable;
import mockit.NonStrictExpectations;
import mockit.Tested;
import org.testng.annotations.Test;

import javax.net.ssl.HttpsURLConnection;
import java.io.OutputStream;
import java.net.URL;

import static org.testng.Assert.assertTrue;

class A {
    private URL url;
}

public class ATest {
    @Tested
    private A a;
    @Injectable
    private URL mockUrl = null;
    @Injectable
    private HttpsURLConnection mockUrlConnection = null;
    @Injectable
    private OutputStream mockOutputStream = null;

    @Test
    public void test1() throws Exception {
        new NonStrictExpectations(){{ // L30
            mockUrl.openConnection();
            result = mockUrlConnection;
            mockUrlConnection.getOutputStream(); // L33
            result = mockOutputStream;
        }};

        assertTrue(true);
    }
}

Results in

java.net.UnknownServiceException: protocol doesn't support output at java.net.URLConnection.getOutputStream(URLConnection.java:843) at test.ATest$1.(ATest.java:33) at test.ATest.test1(ATest.java:30)

ColinHebert commented 10 years ago

The test is successful with this (inverted the definition of the mocks):

    new NonStrictExpectations(){{
        mockUrlConnection.getOutputStream();
        result = mockOutputStream;
        mockUrl.openConnection();
        result = mockUrlConnection;
    }};
ColinHebert commented 10 years ago

More weird behaviour:

package test;

import mockit.Injectable;
import mockit.NonStrictExpectations;
import mockit.Tested;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import javax.net.ssl.HttpsURLConnection;
import java.io.OutputStream;
import java.net.URL;

import static org.testng.Assert.assertTrue;

class A {
    private URL url;
}

public class ATest {
    @Tested
    private A a;
    @Injectable
    private URL mockUrl = null;
    @Injectable
    private HttpsURLConnection mockUrlConnection = null;
    @Injectable
    private OutputStream mockOutputStream = null;

    @BeforeMethod
    public void setUp() throws Exception {
        new NonStrictExpectations(){{
            mockUrlConnection.getOutputStream();
            result = mockOutputStream;
            mockUrl.openConnection();
            result = mockUrlConnection;
        }};
    }

    @Test
    public void test1() throws Exception {
        assertTrue(true);
    }

    @Test
    public void test2() throws Exception {
        assertTrue(true);
    }
}

First test is a success, second results in

java.net.UnknownServiceException: protocol doesn't support output at java.net.URLConnection.getOutputStream(URLConnection.java:843) at test.ATest$1.(ATest.java:32) at test.ATest.setUp(ATest.java:31)

rliesenfeld commented 10 years ago

It's a bug, thanks for the report.