Win a copy of Kotlin Cookbook this week in the Kotlin 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

JUnit test class placement

 
Ranch Hand
Posts: 119
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me start by saying that I'm by no means proficient with using JUnit, so if my question makes no sense, I appreciate all comments I can get!

When I use Junit I set up the test class in its own package different than the package that has the actual code being tested. Then I setup the class that has the runner to run the test class in a different package. Recently I was told that I was creating unnecessary packages and if need be I can have all the classes (source and test) in the same package, and even have the runner in the test class' main method and run the test that way.

I tried variations of the above suggestion, but all I get is "no test executed for project"!

So is there some best practices for using and running JUnit or what?

Thanks
 
Sheriff
Posts: 14622
243
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easiest way is to use Maven (or Gradle) and the Maven standard directory structure: https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

Basically, you put all your production code under src/main/java and all your test classes in parallel packages under src/test/java. Maven/Gradle know how to compile and run the tests so that even if the source files are in different physical locations, as long as you use the same package structure they (the .class files) will effectively be in the same package. That means that tests will have at least package-private access to the production code.
 
Jake Monhan
Ranch Hand
Posts: 119
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestion. I'll restructure a test using Maven and see how it turns out.
 
Don't sweat petty things, or pet sweaty things. But cuddle 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!