• 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

JUnit test class placement

 
Ranch Hand
Posts: 121
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: 15801
264
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: 121
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.
 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic