How to have 100% test coverage for function that calls invokeLater()?

108 Views Asked by At

I am using Junit4.
This is my test class:

import org.junit.Test;

public class UIUtilTest {

    @Test
    public void testMultiline() {
        var multiLineText = "one\ntwo";
        UIUtil.showError(multiLineText, "title");
        assert true;
    }
}

I have the following Jacoco coverage result: enter image description here

How can I test the lambda that Jacoco is complaining about in order to hit 100% coverage?

1

There are 1 best solutions below

1
On

Make test wait for dialog to pop. Having your implementation you can simply add another task to the EDT queue and wait for it to finish.

@Test
public void testMultiline() throws InterruptedException, InvocationTargetException {
    String multiLineText = "one\ntwo";
    UIUtil.showError(multiLineText, "title");
    SwingUtilities.invokeAndWait(() -> {
       //just wait, nothing more
    });
    assert true;
}