Win a copy of Emmy in the Key of Code this week in the General Computing 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

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

 
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
  • 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: 10746
229
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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: 567
  • Mark post as helpful
  • send pies
  • 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: 14284
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 14284
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
It means our mission is in jeapordy! Quick, read this tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!