• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

JUnit test class placement

 
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17323
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 121
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the suggestion. I'll restructure a test using Maven and see how it turns out.
 
The first person to drink cow's milk. That started off as a dare from this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic