spat1978 / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

Mockito fails to verify JavaFX objects #495

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hey Guys, I'm hoping you can help me out with this. I'm getting really bizarre 
performance arond mocking a javafx Label, it smells of concurrency.

Firstly, your template:

What steps will reproduce the problem?
1. setup a fixture that verify's calls to a javaFX Label's setText(String text) 
method (included below)
2. do not have the test call setText
2. run the test

What is the expected output? What do you see instead?
actual: green bar!
expected: a "wanted but not invoked" exception

What version of the product are you using? On what operating system?
Mockito 1.9.5, and a source-built 1.9.8 snapshot.

Please provide any additional information below.
Java 8 update 5.

secondly, the fixture:

import javafx.embed.swing.JFXPanel;
import javafx.scene.control.Label;
import org.junit.Test;

import javax.swing.*;

import static org.fest.assertions.Fail.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class WorkspaceProblemLineItemControllerFixture {

    @Test
    public void when_asserting_on_swing_everythings_dandy(){
        JLabel description = mock(JLabel.class);

        assertThrowsWantedButNotInvokedException(() -> verify(description).setText("description!!"));
    }

    @Test
    public void when_asserting_on_Jfx_everything_should_be_dandy(){
        new JFXPanel();
        Label description = mock(Label.class);

        assertThrowsWantedButNotInvokedException(() -> verify(description).setText("description!!"));
    }

    private void assertThrowsWantedButNotInvokedException(Runnable problemAction) {
        try {
            problemAction.run();
        }
        catch(AssertionError exception){
            if (exception.getMessage() != null && exception.getMessage().contains("Wanted but not invoked")) {
                return;
            }
            else {
                throw exception;
            }
        }

        fail("verification didnt throw :(");
    }
}

If you run the swing test on its own we're ok (so I'm sane), if you run the 
javaFX test on its own it doesn't fail the verification (as it should) and if I 
use IntelliJ to run them both it complains that there's an incomplete mock 
**from the verification line of the previous test**. 

I did a little debugging of my own, but this reaks of concurrency problems so I 
gave up at this point:

If I put a breakpoint on the line 117 of MockingProgressImpl,

    public void mockingStarted(Object mock, Class classToMock) {
        if (listener != null && listener instanceof MockingStartedListener) {
            ((MockingStartedListener) listener).mockingStarted(mock, classToMock);
        }
        /*here*/validateMostStuff();
    }

with the condition verificationMode != null, my IDE hit it, but when inspected 
under watches, verificationMode is null.

Thanks guys!

Original issue reported on code.google.com by Groostav on 9 Jun 2014 at 11:40

GoogleCodeExporter commented 8 years ago
Anybody here? 

:(

Original comment by Groostav on 17 Jun 2014 at 8:50

GoogleCodeExporter commented 8 years ago
If you look at 
<http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Labeled.html#se
tText-java.lang.String->, you'll see that "setText()" is a final method.  
Mockito can't do anything with final methods.  It would be nice if it would SAY 
that, but that's the situation.  You'll have to either compromise on your test, 
or use PowerMock.

Original comment by davidmic...@gmail.com on 17 Jun 2014 at 9:10

GoogleCodeExporter commented 8 years ago
Ohh of course, I'm sorry I didn't think to consider that.

Thanks, and sorry for wasting your time

Original comment by bettyt...@gmail.com on 19 Jun 2014 at 6:07

GoogleCodeExporter commented 8 years ago
Thanks David for watching the issues.

Original comment by brice.du...@gmail.com on 27 Jun 2014 at 3:07