“This Method Has a Constructor Name” Warning in Eclipse for Unit Tests

If you get annoyed when Eclipse alerts you about your unit tests that “This method has a constructor name”, you can set it to ignore these warnings in Window / Preferences / Java / Compiler / Error/Warnings - set “Methods with a Constructor Name” to “Ignore”.

It’s generally a bad idea to switch off warnings but in this case I think the JUnit syntax is confusing Eclipse so it’s easier to just switch them off.

5 Responses to ““This Method Has a Constructor Name” Warning in Eclipse for Unit Tests”

  1. Sudhi Umarji Says:

    Great posting on “This method has a constructor name” warning in Eclipse…It was really annoying and now I know I am not breaking some secret Java rule…

  2. Tim Hardy Says:

    Thanks Sudhi.

    I’m annoyed with myself that I didn’t make better notes when originally posting this - if you revisit would you mind posting a comment detailing the JUnit method that provoked this warning and the version of Eclipse you are using. I experienced this on Eclipse 3.1 and haven’t had the same problem with 3.2 so I’m guessing the problem has been fixed (although I can’t find a bug report describing it). Cheers.

  3. Tchal Says:

    I’m interested in what is missing that it feels it has to make that comment for, ya know? Usually Eclipse has a pretty good idea what’s going on, so it makes me feel like I’m missin’ something.

  4. Tchal Says:

    Ah, in my case it was because I was doin’ something that was syntactically correct, wouldn’t cause any errors, but wasn’t necessary. Which is to say, putting “void” before the return.

  5. Alan Says:

    Hi my name is Alan and I´m from Argentina
    I never post in a forum, this is my first time.
    Trying to resolve this problem I find this forum and I thought I´ll be a good idea post my experience.

    Tchal has reason. The problem with thw warning is about the standard of Oriented Object Programing. Eclipse alerts you, because you are using the same name of the class for a method wich is not a constructor. Eclipse knows that because the sign of the method is diferent from a constructors sign method. Then you must allways use the name of the class only for a constructor method without putting the return value on the sign.

Leave a Reply