Exception Org.testng.exception

3,042 views
Skip to first unread message

Sele nium

unread,
Aug 19, 2013, 8:18:08 AM8/19/13
to testng...@googlegroups.com
When i run the script with testng i am getting this Exception....
please help me to resolve this...



org.testng.TestNGException:
Cannot instantiate class demo.Date_Range
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:38)
    at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:387)
    at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:299)
    at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:110)
    at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:186)
    at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:120)
    at org.testng.TestRunner.initMethods(TestRunner.java:409)
    at org.testng.TestRunner.init(TestRunner.java:235)
    at org.testng.TestRunner.init(TestRunner.java:205)
    at org.testng.TestRunner.<init>(TestRunner.java:160)
    at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:141)
    at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:271)
    at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:577)
    at org.testng.SuiteRunner.init(SuiteRunner.java:157)
    at org.testng.SuiteRunner.<init>(SuiteRunner.java:111)
    at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
    at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
    ... 21 more
Caused by: java.lang.NullPointerException
    at org.openqa.selenium.interactions.Actions.<init>(Actions.java:44)
    at dashboard.Config.<init>(Config.java:42)
    at Date_Range.Date_Range.<init>(Date_Range.java:9)
    ... 26 more

Krishnan Mahadevan

unread,
Aug 20, 2013, 4:07:05 AM8/20/13
to testng...@googlegroups.com
You would need to share the code that is triggering this.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/groups/opt_out.

Umesh Nand

unread,
Oct 27, 2015, 10:24:30 AM10/27/15
to testng-users
Hi Krishnan,
 
Here is my problem.
After executing testNG script , I'm getting error stating org.testng.TestNGException: Cannot instantiate class TestNG.DemoTestNG. Can anybody please give me the solution for my problem. 

package TestNG;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class DemoTestNG {

   // public  void main(String[] args) {

// objects and variables instantiation
        WebDriver driver = new FirefoxDriver();
        String appUrl = "http://192.168.1.116:9090/soxaudit/login";

        
        
        // launch the firefox browser and open the application url
       @Test
        public void Sox_Login() {

        driver.get(appUrl);

// maximize the browser window
        driver.manage().window().maximize();

// declare and initialize the variable to store the expected title of the webpage.
        String expectedTitle = "SOX ADMIN - LOGIN";
       
// fetch the title of the web page and save it into a string variable
                String actualTitle = driver.getTitle();
                Assert.assertEquals(expectedTitle,actualTitle);

// compare the expected title of the page with the actual title of the page and print the result
       // if (expectedTitle.equals(actualTitle))
        //{
        //    System.out.println("Verification Successful - The correct title is displayed on the web page.");
        //}
       // else
       // {
          //  System.out.println("Verification Failed - An incorrect title is displayed on the web page.");

       // }
       
// enter a valid username in the email textbox
        WebElement username = driver.findElement(By.id("j_username"));
        username.clear();
        username.sendKeys(new String[]{"sox"});

// enter a valid password in the password textbox
        WebElement password = driver.findElement(By.id("j_password"));
        password.clear();
        password.sendKeys(new String[]{"welcome@123"});

// click on the Sign in button
        //WebElement SignInButton = driver.findElement(By.id("login_from_loginpage"));
        WebElement SignInButton = driver.findElement(By.xpath("//*[@id='login-page']/div/form/div/button"));
        SignInButton.click();

        
// close the web browser
        driver.close();
        System.out.println("Test script executed successfully.");

// terminate the program
        System.exit(0);
    }
}


        

        






walter kelt

unread,
Oct 27, 2015, 12:13:51 PM10/27/15
to testng-users
Umesh, seems dangerous to use capitalized package names. It could be confused with a Class or Interface name. For a quick test, could you
change the package name to com.umesh

I'd be curious if the problem still occurs.

Krishnan Mahadevan

unread,
Oct 28, 2015, 12:21:28 AM10/28/15
to testng...@googlegroups.com
Unmesh,
Can you please share the complete stack-trace ? Also please help share your test code again [ after removing off all the commented out code ]

Walter,
I don't think that the package name is a problem here. To the best of my knowledge Java uses a package name to just decide on the namespace of a class and to prevent class name clashes.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

Umesh Nand

unread,
Oct 28, 2015, 1:22:59 AM10/28/15
to testng...@googlegroups.com
Thanks for your reply Krishnan.
 Below is my code:

package TestNG;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class DemoTestNG {

        WebDriver driver = new FirefoxDriver();
        String appUrl = "http://192.168.1.116:9090/soxaudit/login";

       @Test
        public void Sox_Login() {

        driver.get(appUrl);

        driver.manage().window().maximize();

        String expectedTitle = "SOX ADMIN - LOGIN";
       
                String actualTitle = driver.getTitle();
                Assert.assertEquals(expectedTitle,actualTitle);

        WebElement username = driver.findElement(By.id("j_username"));
        username.clear();
        username.sendKeys(new String[]{"sox"});

        WebElement password = driver.findElement(By.id("j_password"));
        password.clear();
        password.sendKeys(new String[]{"welcome@123"});

        //WebElement SignInButton = driver.findElement(By.id("login_from_loginpage"));
        WebElement SignInButton = driver.findElement(By.xpath("//*[@id='login-page']/div/form/div/button"));
        SignInButton.click();

        driver.close();
        System.out.println("Test script executed successfully.");

        System.exit(0);
    }
}


        

        








--
You received this message because you are subscribed to a topic in the Google Groups "testng-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/testng-users/8Xiv3fHyjtY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to testng-users...@googlegroups.com.

To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.



--
Thanks & Regards,
Umesh Nand
09035604047

Krishnan Mahadevan

unread,
Oct 28, 2015, 1:35:08 AM10/28/15
to testng...@googlegroups.com
And the complete stacktrace ?

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

Umesh Nand

unread,
Oct 28, 2015, 1:41:03 AM10/28/15
to testng...@googlegroups.com
I think you are asking for this:

org.testng.TestNGException: 
Cannot instantiate class TestNG.DemoTestNG
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:38)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:382)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:295)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:117)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:183)
at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:128)
at org.testng.TestRunner.initMethods(TestRunner.java:415)
at org.testng.TestRunner.init(TestRunner.java:241)
at org.testng.TestRunner.init(TestRunner.java:211)
at org.testng.TestRunner.<init>(TestRunner.java:165)
at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:142)
at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:272)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:587)
at org.testng.SuiteRunner.init(SuiteRunner.java:161)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:114)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1260)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1247)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1101)
at org.testng.TestNG.run(TestNG.java:1018)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:112)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:176)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
... 21 more
Caused by: java.lang.NoClassDefFoundError: com/google/common/base/Function
at TestNG.DemoTestNG.<init>(DemoTestNG.java:14)
... 26 more
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 27 more

Krishnan Mahadevan

unread,
Oct 28, 2015, 1:48:37 AM10/28/15
to testng...@googlegroups.com
Yes finally !

Well at the bottom of the stacktrace lies your problem. You are having classpath issues and missing some JARS.



Caused by: java.lang.NoClassDefFoundError: com/google/common/base/Function
at TestNG.DemoTestNG.<init>(DemoTestNG.java:14)
... 26 more
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 27 more

If you are working with a Maven project, try adding the below as a dependency and try again.

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>


On the whole, this no longer seems to be a TestNG relevant query. 





Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

Umesh Nand

unread,
Oct 28, 2015, 1:52:09 AM10/28/15
to testng...@googlegroups.com
But i m not working with Maven Krishnan!!

Krishnan Mahadevan

unread,
Oct 28, 2015, 1:55:07 AM10/28/15
to testng...@googlegroups.com
No issues. I have pointed out the problem. Please fix your CLASSPATH and add up the required jars and try again. You should be good.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

Umesh Nand

unread,
Oct 28, 2015, 2:14:06 AM10/28/15
to testng...@googlegroups.com
If you don't mind, could you please tell how to fix CLASSPATH and what are the jars required to add, bcoz im completely new to eclipse and TestNG..

Thanks in advance.

Krishnan Mahadevan

unread,
Oct 28, 2015, 2:20:17 AM10/28/15
to testng...@googlegroups.com
Umesh,

Download the jar from here : https://code.google.com/p/guava-libraries/

PS :

I will strongly encourage you to get yourself familiar with these basic things before delving into using TestNG so that you have a smooth experience learning TestNG. 



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

Umesh Nand

unread,
Oct 28, 2015, 7:46:11 AM10/28/15
to testng...@googlegroups.com
Thanks for help Krishnan,

Now i am able to execue TestNG script but the result windows are not displaying output in proper fashion

Console should display result in this manner                                         But displaying in this way
 Selenium TestNG tutorial 18                       Inline image 1

Expected TestNG Result window                                                           Actual TestNG Result window what I am getting

Selenium TestNG tutorial 19          Inline image 2


Did i make any changes to get the desired result window..

Thanks..
















Krishnan Mahadevan

unread,
Oct 28, 2015, 7:50:31 AM10/28/15
to testng...@googlegroups.com
Get rid of the call to "System.exit(0);" and try again. You are not supposed to be triggering an exit call to the JVM. 
After TestNG runs the test, it still triggers a few listener calls which are responsible for showing test results.

But since you are killing the JVM with that System.exit() call, I am guessing TestNG never manages to get any of that done.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

Umesh Nand

unread,
Oct 28, 2015, 8:15:30 AM10/28/15
to testng...@googlegroups.com
Now this is what i got after making changes suggested by you:

Inline image 1Inline image 3

Everything is working fine and its generating the html report also.

Now can i say that the basic TestNG set up is ready to use..?

Krishnan Mahadevan

unread,
Oct 28, 2015, 9:05:57 AM10/28/15
to testng...@googlegroups.com
Yep. You are good to start exploring TestNG ! Have fun :)

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

Umesh Nand

unread,
Oct 28, 2015, 9:09:38 AM10/28/15
to testng...@googlegroups.com
Thanks a lot for your kind help Krishnan!!!!!

Can I contact you in case i wont be able to resolve any issue while implementing TestNG Framework??

Regards,
Umesh

Krishnan Mahadevan

unread,
Oct 28, 2015, 9:22:14 AM10/28/15
to testng...@googlegroups.com
You will find me and even bigger experts on this forum. So feel free to post new queries on the forum than individually reaching out to people.
Your queries on the forum will serve as a knowledge base for future users, you will stand a better chance of getting faster responses from the experts in this group and also have the liberty of picking and choosing the best of solutions from the different alternatives that folks here may have to offer for you.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

vinayak hurkadli

unread,
Apr 20, 2017, 9:08:02 AM4/20/17
to testng-users
hi krishnan,

i am also facing the same issue 
code and the respective stacktrace is as trailing 
plz do help out to figure out the issue

================================================

package testNG_tutorial;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class myapplication {
System.setProperty("webdriver.gecko.driver","C:\\SeleniumGecko\\geckodriver.exe");
//System.setProperty("webdriver.gecko.driver","C:\\SeleniumGecko\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
@Test
public void startApp()
{
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS );
String currentURL=driver.getCurrentUrl();
Assert.assertTrue(currentURL.contains("auth/login"));
}
@Test(dependsOnMethods="startApp")
public void loginApp()
{
driver.findElement(By.xpath(".//*[@id='divUsername']/span")).sendKeys("Admin");
driver.findElement(By.xpath(".//*[@id='divPassword']/span")).sendKeys("admin");
driver.findElement(By.xpath(".//*[@id='btnLogin']")).click();
boolean status=driver.findElement(By.xpath(".//*[@id='menu_admin_viewAdminModule']/b")).isDisplayed();
  Assert.assertTrue(status);
}
@Test(dependsOnMethods="loginApp")
public void logoutApp()
{
driver.findElement(By.xpath(".//*[@id='welcome']")).click();
driver.findElement(By.xpath("//a[text()='Logout']")).click();
Assert.assertTrue(driver.findElement(By.xpath(".//*[@id='txtUsername']")).isDisplayed());
}
}

==============================
and the stacktrace for the above program is as below

==========================================
org.testng.TestNGException: 
Cannot instantiate class testNG_tutorial.myapplication
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:31)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:410)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:323)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:126)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:191)
at org.testng.TestClass.getInstances(TestClass.java:99)
at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:85)
at org.testng.TestClass.init(TestClass.java:77)
at org.testng.TestClass.<init>(TestClass.java:42)
at org.testng.TestRunner.initMethods(TestRunner.java:423)
at org.testng.TestRunner.init(TestRunner.java:250)
at org.testng.TestRunner.init(TestRunner.java:220)
at org.testng.TestRunner.<init>(TestRunner.java:169)
at org.testng.remote.support.RemoteTestNG6_10$1.newTestRunner(RemoteTestNG6_10.java:28)
at org.testng.remote.support.RemoteTestNG6_10$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_10.java:61)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:616)
at org.testng.SuiteRunner.init(SuiteRunner.java:185)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:131)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1383)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1363)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1217)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:23)
... 25 more
Caused by: java.lang.Error: Unresolved compilation problems: 
Syntax error on token ".", @ expected after this token
Syntax error on token ",", < expected
Syntax error, insert "SimpleName" to complete QualifiedName
Syntax error, insert "Identifier (" to complete MethodHeaderName
Syntax error, insert ")" to complete MethodDeclaration

at testNG_tutorial.myapplication.<init>(myapplication.java:13)
... 30 more
=========================================================
Reply all
Reply to author
Forward
0 new messages