question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

java.lang.NullPointerException when I set timeout

See original GitHub issue

TestNG Version

7.4.0

Expected behavior

When I set time out, test runs fine and passes

Actual behavior

java.lang.NullPointerException

Is the issue reproductible on runner?

  • Shell
  • Maven
  • Gradle
  • Ant
  • Eclipse
  • IntelliJ
  • NetBeans

Test case sample

https://github.com/motivatedmind/testng-time-out-issue run mvn clean test or test.xml to reproduce the issue

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.motivatedmind</groupId>
  <artifactId>testng-time-out-issue</artifactId>
  <version>1.0.0-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.4.0</version>
    </dependency>
    <dependency>
      <groupId>io.github.bonigarcia</groupId>
      <artifactId>webdrivermanager</artifactId>
      <version>4.4.3</version>
    </dependency>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.0.0-beta-3</version>
    </dependency>
  </dependencies>
</project>

Selenium web driver manager:

package mypackage;

import org.openqa.selenium.WebDriver;

public class DriverManager {

  private static final ThreadLocal<WebDriver> WEB_DRIVER_THREAD_LOCAL = new ThreadLocal<>();

  private DriverManager() {
  }

  public static WebDriver getDriver() {
    return WEB_DRIVER_THREAD_LOCAL.get();
  }

  public static void setDriver(WebDriver driver) {
    WEB_DRIVER_THREAD_LOCAL.set(driver);
  }

  public static void quitDriver() {
    if (WEB_DRIVER_THREAD_LOCAL.get() != null) {
      WEB_DRIVER_THREAD_LOCAL.get().quit();
    }
  }

}

Test case :

package testcase;

import mypackage.DriverManager;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class MyTest {

  @BeforeMethod()
  public void beforeMethod() {
    WebDriverManager.chromedriver().setup();
    DriverManager.setDriver(new ChromeDriver());
  }

  @AfterMethod()
  public void closeBrowser() {
    DriverManager.quitDriver();
  }

  @Test(description = "This test passes")
  public void withoutTimeOut() {
    DriverManager.getDriver().navigate().to("https://www.google.com");
  }

  @Test(timeOut = 100000, description = "This test fails")
  public void withTimeOut() {
    DriverManager.getDriver().navigate().to("https://www.google.com");
  }
}

Error:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running testcase.MyTest
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@6e8dacdf
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Starting ChromeDriver 91.0.4472.101 (af52a90bf87030dd1523486a1cd3ae25c5d76c9b-refs/branch-heads/4472@{#1462}) on port 16614
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Jul 24, 2021 10:48:46 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Jul 24, 2021 10:48:47 AM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
INFO: Found exact CDP implementation for version 91
Starting ChromeDriver 91.0.4472.101 (af52a90bf87030dd1523486a1cd3ae25c5d76c9b-refs/branch-heads/4472@{#1462}) on port 20142
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Jul 24, 2021 10:48:49 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Jul 24, 2021 10:48:49 AM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
INFO: Found exact CDP implementation for version 91
Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 15.057 sec <<< FAILURE!
withTimeOut(testcase.MyTest)  Time elapsed: 0.018 sec  <<< FAILURE!
java.lang.NullPointerException
	at testcase.MyTest.withTimeOut(MyTest.java:30)


Results :

Failed tests:   withTimeOut(testcase.MyTest)

Tests run: 2, Failures: 1, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  40.550 s
[INFO] Finished at: 2021-07-24T10:48:52+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project testng-time-out-issue: There are test failures.
[ERROR] 

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
krmahadevancommented, Jul 24, 2021

@motivatedmind I think I know what is the problem.

In your DriverManager Can you please switch to using InheritableThreadLocal and see if that fixes the problem ?

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/InheritableThreadLocal.html

When you define timeouts Testng in some situations will end up running your test in a new thread.

Since you rely on ThreadLocal your code tries to query the new threads context but won’t find any.

Inheritable ThreadLocal fixes this problem by ensuring that the current threads context is available to it’s children.

That should fix the problem. Pls try and let us know if that helps.

1reaction
motivatedmindcommented, Jul 24, 2021

Hi @krmahadevan bingo, that worked !!! Thanks a ton for helping !!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nullpointerexception thrown when webpage kept open for ...
Sounds like your session is timing out. You need to either force the user to login again, or extend your session timeout.
Read more >
Session timeout causes null pointer exception — oracle-tech
Hi, I am using Jdev 12.1.2. I have the following code in ApplicationSessionExpiryFilter. I get the 'session expiry' popup after the timeout.
Read more >
error java.lang.NullPointerException when running a ... - IBM
NullPointerException when running a translation service. Map test hangs and time out with an error of "Timeout waiting for response A connection ......
Read more >
NullPointerException at test with timeOut · Issue #2251 - GitHub
Java spring boot test with timeout fails @Test(timeOut = 10000) , but without timeOut works. Note that timeout is never reached, setting timeOut...
Read more >
java.lang.NullPointerException when tried to access ... - Search
Scheduler service was experiencing java.lang.OutOfMemoryError and hence when tried to access schedules from Administrator console, java.lang.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found