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

Partial mocking when Injecting? #81

Closed
ColinHebert opened this Issue Sep 28, 2014 · 3 comments

Comments

2 participants
@ColinHebert

ColinHebert commented Sep 28, 2014

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

This comment has been minimized.

Show comment
Hide comment
@ColinHebert

ColinHebert Sep 28, 2014

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

    new NonStrictExpectations(){{
        mockUrlConnection.getOutputStream();
        result = mockOutputStream;
        mockUrl.openConnection();
        result = mockUrlConnection;
    }};

ColinHebert commented Sep 28, 2014

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

    new NonStrictExpectations(){{
        mockUrlConnection.getOutputStream();
        result = mockOutputStream;
        mockUrl.openConnection();
        result = mockUrlConnection;
    }};
@ColinHebert

This comment has been minimized.

Show comment
Hide comment
@ColinHebert

ColinHebert Sep 28, 2014

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)

ColinHebert commented Sep 28, 2014

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 rliesenfeld added the bug label Sep 29, 2014

@rliesenfeld rliesenfeld self-assigned this Sep 29, 2014

@rliesenfeld

This comment has been minimized.

Show comment
Hide comment
@rliesenfeld

rliesenfeld Sep 29, 2014

Member

It's a bug, thanks for the report.

Member

rliesenfeld commented Sep 29, 2014

It's a bug, thanks for the report.

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