Win a copy of Functional Design and Architecture this week in the Functional programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

JUnit error - IllegalArgumentException:Test class can only have one constructor

 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying out this jUnit example but I am getting the above error.  I hope someone can tell me if I have missed out anything that I get that error and what's the error trying to tell me. Thanks.


 
Saloon Keeper
Posts: 13244
291
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't write test code inside the code under test.

Test methods should be in a separate class in a separate source directory, and the class should only have a default or parameterless constructor. Here is an example:
 
tangara goh
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Don't write test code inside the code under test.

Test methods should be in a separate class in a separate source directory, and the class should only have a default or parameterless constructor. Here is an example:




I see.  Thank you for your teaching. Perhaps I really have to get a book about JUnit rather than see example here and there and try things out.

Could you let me know is there any special reason you are using var instead of my example which I followed in the book ?

Because I also tried to change to var but it is giving me error...so just want to know, just in case it is something I am not aware.

Again. Thanks.
 
Marshal
Posts: 16590
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ability to use var to declare local variables was introduced in Java 10. If you are getting an error with that syntax then the version of Java you're using is earlier than 10.
 
Junilu Lacar
Marshal
Posts: 16590
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tangara goh wrote:Perhaps I really have to get a book about JUnit rather than see example here and there and try things out.


A book on unit testing with JUnit would be good. You could also just search for reliable sources of information online rather than looking for examples "here and there."

A good place to start is the https://junit.org website.
reply
    Bookmark Topic Watch Topic
  • New Topic