• 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

JUNIT testing: confusion with the package

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am following a tutorial for JUNIT testing using Netbeans 8.2 at:
Junit Testing using Netbeans 8.2

They have used the package:


and asked us to write the code. Should I use the same package name.

Somebody please guide me.

Zulfi.
 
Marshal
Posts: 26098
71
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Why wouldn't you?
 
Zulfi Khan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for your reply. I have a confusion. Package means that I have to create a specific directory structure. Also the tutorial does not provide any specific reason for specifying that package. I mean if instead of oracle, I use google or instead of word example I use program then what would happen. I don't have this specific type of folder structure on my computer. Does this package refer to the internal organization of classes related to java software, please guide me.



Zulfi.
 
Paul Clapham
Marshal
Posts: 26098
71
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Zulfi Khan wrote:Package means that I have to create a specific directory structure.



Yes, that's true.

Also the tutorial does not provide any specific reason for specifying that package.



Yes, that's true.

I don't have this specific type of folder structure on my computer.



Of course you don't. So you would have to create it.

And no matter what package you chose to use, you would have to create folders to support that package. So why don't you make things easier for yourself and use the package which the tutorial uses? Sure, it's totally arbitrary so why put yourself through the unnecessary work of changing the package name every time you copy a class from the tutorial source?
 
Saloon Keeper
Posts: 22779
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use Maven to build a project, there are 2 source directories. The src/main/java directory is the root of your application code and the src/test/java directory is where you put your junit test source code.

By mirroring the package structure under src/main/java and src/test/java, you create an environment where you (maven) can build a deployable artefact solely from src/main/java, but Maven can run junit tests by using both src/main/java plus src/test/java, which look to the test-building environment like they're all the same directory.

The reason you'd want to put your tests in the same package as their corresponding application source classes is that Java has package scope. That is, any class in a package can see any field in any other class in the same package unless that field has been given a protected attribute (such as private). Since classes outside that package cannot see fields in package scope, that provides you with a certain (wonky) amount of protection against unauthorized (or at least unexpected) access to fields. If the test is in the same package, it can see - and test - values that would be invisible to extra-package classes, even though the tests are physically located in a separate source directory tree.
 
Zulfi Khan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks Paul and Tim. I would try Maven once I am done with the tutorial.

Zulfi.
 
Zulfi Khan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have one confusion. In normal programming using NetBeans 8.2, I don't use the package:



Please guide me, will it be a problem to test a class for which I don't write the above package statement.


Zulfi.
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is good practice to write a package statement for all but the most trivial programs.  That said, I don't see why a class and its test couldn't both be in the default package (that's the package you get if you don't write a package statement).
 
Tim Holloway
Saloon Keeper
Posts: 22779
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, it's more than just good practice. Some tools won't work properly if you don't adhere to the standard Java conventions regarding package definitions and proper use of upper/lower case.

There's no regulating body for package names, so you can use anything that's syntactically valid and that conforms to standard Java (in other words, avoid stuff like upper-case beginning a package name component).

For educational use, a good guideline might be your school, course, assignment and optionally your own name. So, for example, edu.ucf.bcp1101.timh.exercise1. If you're not in school, the basis of your email address might be good - like com.google.johnsmith1234. If you have a domain of your own, use that. Or that of your employer (they may have specific standards). Or, just name the package "fred". Just avoid the default package.
 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy 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
reply
    Bookmark Topic Watch Topic
  • New Topic