Issue
When running JUnit tests from the command line, I get a NullPointerException, but when run through IntelliJ's debugger, no exception is thrown.
Here is the relevant exception info:
java.lang.NullPointerException
at org.ecx.test.models.AbstractPage.open(AbstractPage.java:98)
at org.ecx.test.UserContext.login(UserContext.java:64)
at org.ecx.test.TabMemory.TabMemoryTest(TabMemory.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runners.Suite.runChild(Suite.java:127)
at org.junit.runners.Suite.runChild(Suite.java:26)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at org.junit.runner.JUnitCore.run(JUnitCore.java:138)
at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:96)
at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:47)
at org.junit.runner.JUnitCore.main(JUnitCore.java:40)
I'm not sure what could be causing the problem, as the line in question is: driver.navigate().to(url);
And url
is a string that's hardcoded to an overridden getPageUrl()
method. The WebDriver inits fine, as the specified browser opens, and is functional in other scripts (the utils, test data housekeeping, etc), which is interesting as the same code is used to manage driver/selenium instances.
No other arguments are being specified by IntelliJ's debug configurations, except passing -ea
to the Java VM, which is default.
AbstractTest.open:
public void open() {
if (!isInitialized) {
throw new IllegalStateException("Page not initialized.");
}
String url = Environment.getBaseUrl() + getPageUrl();
Log.debug("Opening url: %s", url);
WebDriver.Navigation navigation = driver.navigate(); //Error is thrown here.
navigation.to(url);
//driver.navigate().to(url);
PageFactory.initElements(driver, this);
isLoaded = true;
}
AbstractTest(constructor):
public AbstractPage(Selenium selenium) {
this.selenium = selenium;
if (WebDriverBackedSelenium.class.isInstance(selenium)) {
this.driver = ((WebDriverBackedSelenium) selenium).getWrappedDriver();
}
PageFactory.initElements(driver, this);
uiMap = new Properties();
initialize();
}
Environment.openSelenium:
public static void openSelenium() {
if (!isSelenium) {
Log.info("Opening Selenium...");
driver = WebDriverFactory.getWebDriver();
selenium = new WebDriverBackedSelenium(driver, Environment.getBaseUrl());
Environment.setIsSelenium(true);
seleniumExtension = SeleniumExtension.getInstance();
}
}
Solution
@Jerry101 suggestion is a good one. However, if you want a way to do this without changing the source code (or in case you can't change the code), you could also create a remote debug configuration in IDEA. This would allow you to run it in the environment when the null occurs, but use the IDEA debugger to see what the issue is.
In the Run/Debug Configurations
dialog, click the add button and select
Remote
. You may want to use the Listen
debugger mode so you can start the debugger first. You can use the Attach
mode, but would need to make sure you can start the IDEA debug configuration quickly after starting the test from the command line before it got to the code in question. For a non-production system, Listen
is fine.
When you go to run your test via the command line, add the necessary configuration options -- as shown in the IDEA remote debug configuration dialog -- to your command line. When the breakpoint is hit, you can see what is null
.
Answered By - Javaru
Answer Checked By - Mildred Charles (JavaFixing Admin)