Error org.testng.TestNGException: Cannot inject @Test annotated Method main with class Ljava.lang.String;

CJ2K picture CJ2K · Mar 6, 2017 · Viewed 12.4k times · Source

I have created a testng.xml file to run a suite of my selenium webdriver test. From eclipse I can run this just fine as a testNG Suite, but from the command like I get the following error.

testng.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "testng.org/testng-1.0.dtd">; 
  <suite name="CATS Regression Suite"> 
    <test name="WEB UI"> 
      <parameter name="uid" value="TestAccount"/> 
      <parameter name="pid" value="Test123"/> 
      <classes> 
         <class name="VerifyLogin"/> 
      </classes> 
     </test> 
    <!-- Test --> 
  </suite> 
  <!-- Suite -->

command ran:

java org.testng.TestNG testng.xml

Exception trace:

[Error] org.testng.TestNGException: 
Cannot inject @Test annotated Method [main] with [class [Ljava.lang.String;].
For more information on native dependency injection please refer to http://testng.org/doc/documentation-main.html#native-dependency-injection
    at org.testng.internal.Parameters.checkParameterTypes(Parameters.java:244)
    at org.testng.internal.Parameters.createParameters(Parameters.java:172)
    at org.testng.internal.Parameters.createParameters(Parameters.java:458)
    at org.testng.internal.Parameters.handleParameters(Parameters.java:568)
    at org.testng.internal.Invoker.handleParameters(Invoker.java:1293)
    at org.testng.internal.Invoker.createParameters(Invoker.java:1020)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1110)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:756)

at org.testng.TestRunner.run(TestRunner.java:610)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
    at org.testng.TestNG.runSuites(TestNG.java:1133)
    at org.testng.TestNG.run(TestNG.java:1104)
    at org.testng.TestNG.privateMain(TestNG.java:1434)
    at org.testng.TestNG.main(TestNG.java:1403)

Code (As shared in the comments)

public class VerifyLogin extends TestBase {
   @Parameters({"uid","pid"}) 
   @Test 
   public void verifyLogin(String uid, String pid) throws IOException, Exception {
      String userName = uid;
      String passWord = pid; 
      // new instance 
      ExtentReports extent = new ExtentReports("C:\\Users\\AdvancedReport.html", true); 
      //starting test 
      ExtentTest test = extent.startTest("Login Test", "Regression"); 
      // log step 
      test.log(LogStatus.PASS, "Browser Running"); 
      TestBase login=new TestBase(); 
      login.testLogin(userName, passWord);
   }
}

Answer

user8120115 picture user8120115 · Jun 6, 2017

Just remove String uid, String pid in main method and run once again as

public void verifyLogin(String uid, String pid)

to

public void verifyLogin()