Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot inject @Test annotated Method [main] with [class [Ljava.lang.String;]. #1405

Closed
ben-manes opened this issue Apr 2, 2017 · 14 comments · Fixed by #1479
Closed

Cannot inject @Test annotated Method [main] with [class [Ljava.lang.String;]. #1405

ben-manes opened this issue Apr 2, 2017 · 14 comments · Fixed by #1479

Comments

@ben-manes
Copy link
Contributor

@Test(singleThreaded = true)
public class MainTest {

  @Test public void foo() {}

  public static void main(String[] args) {}
}
[Utils] [ERROR] [Error] org.testng.TestNGException: 
Cannot inject @Test annotated Method [main] with [class [Ljava.lang.String;].

While understandable, I'd argue that the class annotation shouldn't apply to static methods or, at least, to the main method. It is easy to work around by configuring each method, but was surprising.

@krmahadevan
Copy link
Member

krmahadevan commented Apr 2, 2017 via email

@ben-manes
Copy link
Contributor Author

That was my analysis as well. I think either static methods or, at least the main method, shouldn't be opted into this. It was surprising but made sense when I thought about it.

@juherr
Copy link
Member

juherr commented Apr 6, 2017

True, a static main(String[]) could be excluded from detected test methods.

@cbeust WDYT?

@cbeust
Copy link
Collaborator

cbeust commented Apr 6, 2017

Sounds good to me. I often have to add @Test(enabled = false) to my main methods, which is a bit silly.

@ben-manes
Copy link
Contributor Author

I'd be happy using that approach if added to the exception message. I was debugging with a main method, so not a very common occurrence.

@Pavithranandhini
Copy link

Cannot inject @test annotated Method [main] with [class [Ljava.lang.String;]

what is the solution for this issue

@krmahadevan
Copy link
Member

krmahadevan commented Jan 31, 2020

@Pavithranandhini You haven't told us a lot of things.

  1. What version of TestNG are you working with ? [ Please use latest released version of 7.1.0 ]
  2. What does your code look like ?
  3. How are you running your tests ?

All said and done, please check if this comment helps you fix it.

what is the solution for this issue

Upgrade to the latest version and this should be taken care of, because it was a bug that got fixed.

@umeshkarkar
Copy link

I am also facing the same issue. please help here.
please find attached screenshot:

cannot inject

@krmahadevan
Copy link
Member

@umeshkarkar the exception clearly gives you a link that you can consider referring to understand how to get past the problem.

You have a test method that is accepting a string parameter but you have neither used a data provider nor used the @Parameter annotation.

TestNG is working fine.

@umeshkarkar
Copy link

Yeah, got it but again im getting nullpointerException at line no 23 after removing string parameter from the test case.

@krmahadevan
Copy link
Member

krmahadevan commented Sep 2, 2020

@umeshkarkar - Please use the Google forums or stackoverflow to seek help on user issues for TestNG. WE would like to confine the issues page to tracking actual TestNG bugs or new features.

So I would suggest that you post on either of these two forums with a simple full fledged example that can be used to reproduce the problem and someone in the forum should be able to help you out.
Dont forget to use the latest released version of TestNG viz., 7.3.0

@umeshkarkar
Copy link

Sure. Thanks!!

@Daljeetkaur320
Copy link

image
Screenshot 2024-01-30 144558

Could anyone tell me how i cam resolve this issue

@juherr
Copy link
Member

juherr commented Jan 31, 2024

Your test method has parameters but doesn't use parameters. See the documentation: https://testng.org/#_parameters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants