Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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
  • 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

Maven Project: src/main vs src/test

 
Greenhorn
Posts: 16
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I am curious why classes in src/main are visible to the test classes under the src/test in a maven project.

Are they combined on compile time and test classes are just removed when packaging the application?

Thanks in advance.
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on what you mean by "visible."  In Java, one usually uses a package statement to allow the test and the main programs to interact.  It doesn't have to do with Maven.
 
Sheriff
Posts: 15733
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has to do with the package declarations and the classpath settings when you run your tests.

By convention, the package structure that you create under src/test mirrors the package structure under src/main. You also declare your *Test classes to be in the same package as the class under test. This way, you can even reference members in your production code that have package-private (default) access.

When running tests, the test class output directory is added to the classpath in addition to the production class output directory. As a result, even though the two may be entirely separate physical directories in the file system, to the Java Runtime as long as the classes are declared to be in the same package, they will be loaded and treated as being "together" in the same package.
 
Arnold Gatmaitan
Greenhorn
Posts: 16
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Junilu. Thanks you for your answer. This also means on compile time production and test classes are combined in their respective packages.

Junilu Lacar wrote:It has to do with the package declarations and the classpath settings when you run your tests.

By convention, the package structure that you create under src/test mirrors the package structure under src/main. You also declare your *Test classes to be in the same package as the class under test. This way, you can even reference members in your production code that have package-private (default) access.

When running tests, the test class output directory is added to the classpath in addition to the production class output directory. As a result, even though the two may be entirely separate physical directories in the file system, to the Java Runtime as long as the classes are declared to be in the same package, they will be loaded and treated as being "together" in the same package.

 
Junilu Lacar
Sheriff
Posts: 15733
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not exactly sure what you mean by that but it has a different connotation from what I said which is that the classes from test and main are seen as being in the same package. In a literal sense, they're really not combined but in effect, it seems like they are. In my dialect, we'd say "morag lang kay dili man"; it only "seems" because it really isn't.
 
Junilu Lacar
Sheriff
Posts: 15733
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I see what you're getting at now. Since at compile time, classes in the production area are seen as being in the same package as the classes that are in the test area, the compiler must also treat the two areas as a combined logical namespace. Yeah, it's like you compile with a -cp option that includes both test and main output directories.
 
What are you doing in my house? Get 'em tiny ad!
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