Speaking of package private methods...
I posted this question last week but have not received any responses. So
I'm going to take this opertunity:
Hi All,
I'm getting the following error when running my cactus tests:
java.lang.IllegalAccessError: tried to access method
com.digitalreasoning.kafe.host.Host.getKafeMBeanServer() from class
com.digitalreasoning.kafe.host.HostObjectFactoryTest at
com.digitalreasoning.kafe.host.HostObjectFactoryTest.testDomainName(HostObjectFactoryTest.java:44)
The method getKafeMBeanServer() is a package private method in Host.
/**
* Returns the MBeanServer created by this Host.
* Keeping this package private for testing since the Host should provide
* any fuctionality that my be derived from the MBeanServer
* @return
*/
MBeanServer getKafeMBeanServer(){
return kafeMbeanServer_;
}
HostObjectFactoryTest is in the same package as you can tell. The method
testDomainName()
is:
/**
* Make sure that the MBeanServer is created with the expected
* DomainName.
*/
public void testDomainName(){
Host host = HostObjectFactory.createHost();
MBeanServer server = host.getKafeMBeanServer();
assertTrue("Asserting True that "+Host.DOMAIN + " == "+
server.getDefaultDomain(),
server.getDefaultDomain().equals(Host.DOMAIN));
}
Any ideas why I'm getting the IllegalAccessError?
Thanks
Thanks again.
-----Original Message-----
From: Christopher Lenz <[EMAIL PROTECTED]>
Sent: Nov 17, 2003 6:22 AM
To: Cactus Users List <[EMAIL PROTECTED]>
Subject: Re: invoking a private method of a servlet
Hello Ajay,
the purpose of making a method private is that it can not be called from
outside the declaring class. You might be able to get info about a private
method via reflection, but the JVM will not allow you to actually invoke it
(AFAIK). So there's no way you can test a private method. In general,
private methods are also considered too much of an implementation detail,
and too fragile, to be worth testing separately. A common compromise is to
make such methods package private, and put the test case class in the same
package as to class under test.
HTH,
-chris
Ajay Kumar wrote:
> I have a controller servlet with many private methods and want to write
> a testcase for these methods. I tried using reflection for invoking
> these methods, but got the following report:
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
Andrew Boyd
Software Architect
Sun Certified J2EE Architect
B&B Technical Services Inc.
205.422.2557
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]