views:

184

answers:

2

I have a NetBeans project set up with a bunch of source classes and about 10 jUnit test classes in a separate Test folder.

From within the Test files I can import any other test file or source class. However, from within the normal source files, NetBeans acts as if the Test classes don't exist. Autocomplete on them won't work, and if I attempt to use them I get a compile error.

I understand that normally it wouldn't make sense to use a Test class from the regular source, but I have a good reason in this case. Part of my program accepts a class name as a string and creates and instance of it with reflection. One of my jUnit tests calls this method to test it, and passes it the name of a Test class. This always fails because the normal program code can't find any of the classes from the Test folder.

+6  A: 

I'm not sure if this is going to be the majority opinion but here's my take: if your program is failing because you can't access the test classes from your source classes, you're doing something wrong. I don't care if you think you have a good reason, you don't. (Well I think you don't.)

Maybe you need to move a test class into the project proper. Or in your case, you might need to configure the classpath for testing... I can't say definitively how I'd recommend solving the situation without having access to your code. From the point of view of the source classes, the test classes really shouldn't seem to exist, unless the project is being tested.

David Zaslavsky
I guess I wasn't completely clear. This problem does occur during testing. I have a jUnit test which is testing a method to instantiate classes based on a class name passed as a string. The class I want it to instantiate during testing is another Test class which shouldn't be included in the regular source.
takteek
Agreed. If your using reflection to access the class, then you should have no specific reference to whatever class your loading, in WHICH case, you should not get a compile error, you should get a runtime error instead.
Zoidberg
That is Agreed with David...
Zoidberg
I don't have a reference to it. In fact, I get no error at compile time. I was just using that as an example to show that the Test classes can't be accessed even when hardcoding it.
takteek
@takteek: I understand your situation. Your question wasn't phrased in the best way, I think (titling it "Source classes don't have access to test classes" makes us think "well duh, what's wrong with that?"). I'd suggest focusing on the class loader, specifically why it's not finding your test class during the test you're running.
David Zaslavsky
+2  A: 

While running a test the classpath will include the source and test classes. They must, and I don't think it would really be possible to prevent one from accessing the other at runtime, even with class loaders. I certainly highly doubt that Netbeans is doing it, even if it is theoretically possible.

There are two possibilities that I could guess at. One is that the error is not what you think it is. If you post a stack trace, we could help you with that.

The other is that you are using the wrong class loader to load the class (i.e. not just using Class.forName()). Can you post the code snippit doing the class loading?

Yishai
+1 for being more helpful than my answer.
David Zaslavsky