• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Test Classes - newbie question

 
Amy Humphries
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On page 82 of the wonderful new HF Servlets & JSP book, we're supposed to write a test class, but we're "on your own here". I've never written a test class. Could someone give me more info?
 
Anthony Watson
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A test class can be just a regular Java class that creates an instance of a class you wrote and calls its methods using different arguments to determine if the methods are working as you expect them to. If you really get into testing, there's a free framework called JUnit (not at all related to SCWCD) that has a lot of advantages to writing test classes from scratch.
 
Jack Bercy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amy Humphries:
On page 82 of the wonderful new HF Servlets & JSP book, we're supposed to write a test class, but we're "on your own here". I've never written a test class. Could someone give me more info?


Amy ! Seems we're studying for the same exam, and we're both at the same page at the same time :-) Let's see who passes the exam first (just kidding).

Here's my test class. I have no idea if it works though, i just know that it compiles... (gurus, please see below for my question).

Here's my source anyway:




I have set up my directory tree just as the book says. I compiled the BeerExpert.java file (the "model" code), and its class file was saved in .../classes/com/example/model.

I did the same with my test class. Note that I put this class in the "com.example.model" package also. I don't know if you normally put the test classes in the tested class' package ? Can anyone comment ? I tried removing the package statement, it didn't resolve my problem (see below).

So now, my .../classes/com/example/model directory has two .class files, namely BeerExpert and TestBeerExpert.

I was at the /BeerV1 level when I compiled, and I'd like to stay there to actually run the test class.


So, I try to run the thing :

java TestBeerExpert
Exception in thread "main" java.lang.NoClassDefFoundError: TestBeerExpert


Oh, do I need to specify where it's at (I probably do):

java classes/com/example/model/TestBeerExpert
Exception in thread "main" java.lang.NoClassDefFoundError: classes/com/example/model/TestBeerExpert (wrong name: com/example/model/TestBeerExpert)
at java.lang.ClassLoader.defineClass0(Native Method)
... and the stack trace goes on for 10 lines or so...


Question:

Why can't I execute this test class ? Is this an environment issue ? I tried defining the CLASSPATH environment variable to .../BeerV1/classes/, but that didn't help. All it did was change the error message to a shorter one :

java classes/com/example/model/TestBeerExpert
Exception in thread "main" java.lang.NoClassDefFoundError: classes/com/example/model/TestBeerExpert

There's something VERY BASIC I'm missing here about Java development. This probably should be posted in the Java Beginner section, but since there will be other newbies reading the book and trying to write this test class, it'll probably be more helpful if it stays here.

Thanks in advance,

Jacques.
 
Anthony Watson
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if you normally put the test classes in the tested class' package ?


Most people separate test classes from regular classes by placing them in a different directory but in the same package. For example, the regular class could be in src/java and the test class could be in src/test. If both classes have the same package, the test class will be able to access the public, protected, and default methods and attributes of the regular class (assuming the same classpath).
 
Tobin Jackson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there. I'm 90% confident in what i'm about to add. Someone please correct me if I'm wrong.


I was at the /BeerV1 level when I compiled, and I'd like to stay there to actually run the test class.


Unlike 'javac', executing a class with 'java' requires you to be in the directory just above the package specified in the class. For example, if you have a class with 'package com.example.mypackage;' then you must run the class from the directory just above com.

tobin
 
Jack Bercy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tobin Jackson:
Hey there. I'm 90% confident in what i'm about to add. Someone please correct me if I'm wrong.



Unlike 'javac', executing a class with 'java' requires you to be in the directory just above the package specified in the class. For example, if you have a class with 'package com.example.mypackage;' then you must run the class from the directory just above com.

tobin



Yup, that did it for me, thanks !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic