jmockit / jmockit1

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

Window doesn't get mocked. #77

Closed evdzhan closed 10 years ago

evdzhan commented 10 years ago

Hi there. I am having difficulties mocking the java.awt.Window class.

I did try to mock it using the MockUp API as well as @Mocked API. Here are example tests attempting to mock the window :

import mockit.Deencapsulation;
import mockit.Mock;
import mockit.MockUp;
import mockit.Mocked;
import org.junit.Test;

import java.awt.*;
import java.lang.ref.WeakReference;

public class TheTest {
@Test
public void testWindow_1(@Mocked final Window window) {

    Deencapsulation.invoke(window, "addOwnedWindow", new WeakReference(null));

}

@Test
public void testWindow_2() {
    Window window = new MockUp<Window>() {
        @Mock
        void addOwnedWindow(WeakReference weakWindow) {
            System.out.println("Mocking successful");
        }
    }.getMockInstance();

    Deencapsulation.invoke(window, "addOwnedWindow", new WeakReference(null));
}

}

With JMockit 1.12 the first test fails, but the second one succeeds. With JMockit 1.9 it is vice versa - the first succeeds, while the second fails.

My environment is :
JDK 1.7.0_51 32 bit JMockit 1.12 and 1.9 Win7 64 bit

rliesenfeld commented 10 years ago

Same reason as issue #70 (package-private methods in JRE classes were excluded from mocking in JMockit 1.12).

evdzhan commented 10 years ago

But then why does the second test pass under JMockit 1.12 ?

rliesenfeld commented 10 years ago

I haven't checked, but if it passes then what's the problem? There were several improvements to MockUp since version 1.9, maybe that's why it passes now.

evdzhan commented 10 years ago

Yes, but I am mocking window, you just said I cannot do that. Am I missing something ?

rliesenfeld commented 10 years ago

My previous comment (on issue #70) was about the first test (with @Mocked), not the second one (with MockUp). I should have clarified that.