aspose file tools*
The moose likes Java in General and the fly likes Accessing protected method in test case using Java Reflection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Accessing protected method in test case using Java Reflection" Watch "Accessing protected method in test case using Java Reflection" New topic
Author

Accessing protected method in test case using Java Reflection

James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 215
I am trying to obtain and invoke a protected method residing in a different class and also different package using Java Reflection.

Class containing protected method:



Calling class:



The compiler throws this exception:



Thank you for taking the time to read this.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29249
    
139

James,
Not to be pedantic, but the JVM throws that error not the compiler. The compiler runs before you execute the code.

On to reflection. I see a few issues in that code. Compare it to this code:



1) I pass String.class. You are getting the exception because it is looking for retrieveItems() which doesn't exist. My example looks for retrieveItems(String) which does exist.
2) Since the method is not accessible, you need to specifically make it so.
3) I pass service instead of clazz as the object I want to run the method against. Since the method isn't static, it needs the actual instance of the object.

I'm going to move this to our Java in General forum since it is about reflection more than testing.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 215
Hi Jeanne,

Thank you so much for the response!

How can I assign the variable, "items", to the value that gets returned from the retrieveItems() method (after or while invoking it)?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Method.invoke returns Object. That's because it doesn't know what the method actually returns. Object is the catch-all type. This object will be the actual object returned by the method, in this case your List<String>*. All you need to do is cast it. That will give you a compiler warning because casting with generics is always an unsafe operation.

* If the return type is void then null is returned.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Why don't you make the test-class extend MyServiceImpl? Then you'll have direct access to it.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing protected method in test case using Java Reflection
 
Similar Threads
Regarding java Proxy object
Junit testing for Struts2
Inheritance in Spring
How to get the class name of the subclass from a static method in the superclass?
How the objects are constructed at the time of de-serialization?