javamavenjavafxmockitotestfx

JavaFX: Populating mock list view causes null pointer exception during Maven test build


The method lvSelected.setItems(selectedList) causes null pointer exception on following code, but only when I try to use Maven to clean test, and only when using a 32-bit JRE. What could be causing this? When I debug, both the ListView and the ObservableList seem to be not null.

Here's part of the code, with a test method that fails when using the setItems method:

public class CommunicationParametersControllerTest extends ApplicationTest {

    @InjectMocks
    public CommunicationParametersController cpc;
    @Mock
    public ListView<TableRowDisplayCodes> lvSelected, lvCodes;
    @Mock
    public TableRowDisplayCodes tableRowDisplayCodes;
    @Mock
    public ObservableList<TableRowDisplayCodes> codesList;
    @Mock
    public ObservableList<TableRowDisplayCodes> selectedList;

    public int MAX_OU_ITEMS = 30;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void checkIfIsAddingItemUsingAddListItemMethod() throws Exception {
        selectedList = FXCollections.observableArrayList();
        selectedList.add(new TableRowDisplayCodes(UOItems.UO_03));
        lvSelected.setItems(selectedList);
        invokeMethod(cpc, "addListItem", cpc.lvSelected, new TableRowDisplayCodes(UOItems.UO_04), MAX_OU_ITEMS);
        assertEquals(2, lvSelected.getItems().size());

}

Here's the stack trace:

[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running main.java.controllers.commands.GControllerTest
[ERROR] Tests run: 7, Failures: 0, Errors: 7, Skipped: 0, Time elapsed: 2.434 s <<< FAILURE! - in main.java.controllers.commands.GControllerTest
[ERROR] checThatTheNamesOfTheAddedItemsAreCorrect(main.java.controllers.commands.GControllerTest)  Time elapsed: 0.791 s  <<< ERROR!
java.lang.NullPointerException
    at main.java.controllers.commands.GControllerTest.checThatTheNamesOfTheAddedItemsAreCorrect(CommunicationParametersControllerTest.java:101)

[ERROR] mustNotInsertRepeateItemInAddListItem(main.java.controllers.commands.GControllerTest)  Time elapsed: 0.258 s  <<< ERROR!
java.lang.NullPointerException
    at main.java.controllers.commands.GControllerTest.mustNotInsertRepeateItemInAddListItem(CommunicationParametersControllerTest.java:121)

[ERROR] addTheMAximumNumberOfItemsInAddListItemWithIndex(main.java.controllers.commands.GControllerTest)  Time elapsed: 0.256 s  <<< ERROR!
java.lang.NullPointerException
    at main.java.controllers.commands.GControllerTest.addTheMAximumNumberOfItemsInAddListItemWithIndex(CommunicationParametersControllerTest.java:184)

[ERROR] checkIfIsAddingItemUsingAddListItemMethod(main.java.controllers.commands.GControllerTest)  Time elapsed: 0.23 s  <<< ERROR!
java.lang.NullPointerException
    at main.java.controllers.commands.GControllerTest.checkIfIsAddingItemUsingAddListItemMethod(CommunicationParametersControllerTest.java:45)

[ERROR] shouldThrowErrorWhenAddingItemWithWrongIndex(main.java.controllers.commands.GControllerTest)  Time elapsed: 0.237 s  <<< ERROR!
java.lang.NullPointerException
    at main.java.controllers.commands.GControllerTest.shouldThrowErrorWhenAddingItemWithWrongIndex(CommunicationParametersControllerTest.java:141)

[ERROR] addItemByIndexAndCHeckName(main.java.controllers.commands.GControllerTest)  Time elapsed: 0.251 s  <<< ERROR!
java.lang.NullPointerException
    at main.java.controllers.commands.GControllerTest.addItemByIndexAndCHeckName(CommunicationParametersControllerTest.java:132)

[ERROR] addTheMaximumNumberOfItemsInAddListItem(main.java.controllers.commands.GControllerTest)  Time elapsed: 0.251 s  <<< ERROR!
java.lang.NullPointerException
    at main.java.controllers.commands.GControllerTest.addTheMaximumNumberOfItemsInAddListItem(CommunicationParametersControllerTest.java:84)

[INFO] Running main.java.controllers.commands.GControllerTest
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.41 s - in main.java.controllers.commands.GControllerTest
[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Errors: 
[ERROR]   CommunicationParametersControllerTest.addItemByIndexAndCHeckName:132 » NullPointer
[ERROR]   CommunicationParametersControllerTest.addTheMAximumNumberOfItemsInAddListItemWithIndex:184 » NullPointer
[ERROR]   CommunicationParametersControllerTest.addTheMaximumNumberOfItemsInAddListItem:84 » NullPointer
[ERROR]   CommunicationParametersControllerTest.checThatTheNamesOfTheAddedItemsAreCorrect:101 » NullPointer
[ERROR]   CommunicationParametersControllerTest.checkIfIsAddingItemUsingAddListItemMethod:45 » NullPointer
[ERROR]   CommunicationParametersControllerTest.mustNotInsertRepeateItemInAddListItem:121 » NullPointer
[ERROR]   CommunicationParametersControllerTest.shouldThrowErrorWhenAddingItemWithWrongIndex:141 » NullPointer
[INFO] 
[ERROR] Tests run: 11, Failures: 0, Errors: 7, Skipped: 0
[INFO] 
[INFO] parallel='none', perCoreThreadCount=true, threadCount=0, useUnlimitedThreads=false, threadCountSuites=0, threadCountClasses=0, threadCountMethods=0, parallelOptimized=true
[INFO] 

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  02:35 min
[INFO] Finished at: 2020-02-08T13:04:57-02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test (default-test) on project ConfigFrontEnd: There are test failures.
[ERROR] 
[ERROR] Please refer to C:\dev\ConfigFrontEnd\target\surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test (default-test) on project ConfigFrontEnd: There are test failures.

Please refer to C:\dev\ConfigFrontEnd\target\surefire-reports for the individual test results.
Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:193)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
Caused by: org.apache.maven.plugin.MojoFailureException: There are test failures.

Please refer to C:\dev\ConfigFrontEnd\target\surefire-reports for the individual test results.
Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
    at org.apache.maven.plugin.surefire.SurefireHelper.throwException(SurefireHelper.java:289)
    at org.apache.maven.plugin.surefire.SurefireHelper.reportExecution(SurefireHelper.java:161)
    at org.apache.maven.plugin.surefire.SurefirePlugin.handleSummary(SurefirePlugin.java:364)
    at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked(AbstractSurefireMojo.java:1041)
    at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute(AbstractSurefireMojo.java:857)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210)
    ... 20 more
[ERROR] 
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Solution

  • I did this same application, but I did not use mock in ListView, I created a local ListView inside the test method using spy method.

    @Test
        public void test() throws Exception {
    
            @SuppressWarnings("unchecked")
            ListView<TableRowDisplayCodes> lvSelected = spy(ListView.class);
    
            codesList = FXCollections.observableArrayList();
            selectedList = FXCollections.observableArrayList();
            lvSelected.setItems(selectedList);
    
            selectedList.add(new TableRowDisplayCodes(UOItems.UO_03));
            invokeMethod(cpc, "addListItem", lvSelected, new TableRowDisplayCodes(UOItems.UO_04), MAX_OU_ITEMS);
            assertEquals(2, lvSelected.getItems().size());
        }