• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 1021
  • 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: 15510
363
  • 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: 1021
  • 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.
 
Sheriff
Posts: 17644
300
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
Sheriff
Posts: 17644
300
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.
 
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic