Ussgin JUnit add ons to get the superclass  
Author Message
Joe Simon





PostPosted: 2004-6-4 3:30:00 Top

java-programmer, Ussgin JUnit add ons to get the superclass
Hi, I am using J-UNIT to test my code. I have some instances where
the class heirarchy is makeing my shake my head as to how to test.

Hope I can explain this so that someone can help...

Here Goes . I have a class 'A', with a private attribute 'attrib', and a
public accessor 'getAttrib'.

Now there is another class, 'B' which is derived from 'A', however, it
also has it's own private attribute 'attrib', and a public accessor
'getAttrib'.


We have software that loads A & B from XML Files, and i am trying to
verify that they are working correctly (later on these classes will be
persisted in a database, so I want to make sure that I can save and load
them correctly).

What i am trying to do is find out what is stored in 'A's attrib.

Sort of like this :


// The XML files holds teh data for both the super class and the sub
class
// The returned object is of Class 'B'
A myA = B.loadFromXMLFile ( "FileName.xml" ) ;

// Works Great, But how do i get to the getAttrib of the A object
(super class) ?
assertEquals ( "attrib", 123, myA.getAttrib () ) ;


Now is there a way to get the attribute for the attrib of 'A' ?

Using the NetBeans debugger I can se the value for attrib= 5

is there a way to do something like the following ?

// This does not work, but is there any way to acheive the
// Same results ?
A theRealA = PrivateAccessor.getField( myA, "super" ) ; // ??

// and then
assertEquals ( "attrib", 5, theRealA.getAttrib () ) ;


Thanks in Advance !
Joe



 
Derek Chen-Becker





PostPosted: 2004-6-4 4:33:00 Top

java-programmer >> Ussgin JUnit add ons to get the superclass Joe Simon wrote:
> Hi, I am using J-UNIT to test my code. I have some instances where
> the class heirarchy is makeing my shake my head as to how to test.
>
> Hope I can explain this so that someone can help...
>
> Here Goes . I have a class 'A', with a private attribute 'attrib', and a
> public accessor 'getAttrib'.
>
> Now there is another class, 'B' which is derived from 'A', however, it
> also has it's own private attribute 'attrib', and a public accessor
> 'getAttrib'.
>
>
> We have software that loads A & B from XML Files, and i am trying to
> verify that they are working correctly (later on these classes will be
> persisted in a database, so I want to make sure that I can save and load
> them correctly).
>
> What i am trying to do is find out what is stored in 'A's attrib.
>
> Sort of like this :
>
>
> // The XML files holds teh data for both the super class and the sub
> class
> // The returned object is of Class 'B'
> A myA = B.loadFromXMLFile ( "FileName.xml" ) ;
>
> // Works Great, But how do i get to the getAttrib of the A object
> (super class) ?
> assertEquals ( "attrib", 123, myA.getAttrib () ) ;
>
>
> Now is there a way to get the attribute for the attrib of 'A' ?
>
> Using the NetBeans debugger I can se the value for attrib= 5
>
> is there a way to do something like the following ?
>
> // This does not work, but is there any way to acheive the
> // Same results ?
> A theRealA = PrivateAccessor.getField( myA, "super" ) ; // ??
>
> // and then
> assertEquals ( "attrib", 5, theRealA.getAttrib () ) ;
>

What is the purpose of hiding the parent's member? To me this looks like
not such a good design. I think the only way you can do this is from
within the subclass, something like:

public class B
{
...

public Integer getParentAttrib()
{
return super.attrib;
}
}

I don't have the language spec in quick reach, but I did find this:

http://java.sun.com/docs/books/tutorial/java/javaOO/subclass.html

Derek