K
kk_oop
Hi.
In Java, we have a class MyClass that privately contains an ArrayList
as an attribute called myList. We were looking for a way to enable a
JUnit test case class to call MyClass methods, then check the state of
myList.
Here's a what we are considering:
a. Make a protected method in MyClass called getMyList that returns
myList and performs lazy instantiation (it'll only instantiate myList
the first time it is called but will return myList everytime).
MyClass should only access myList through this getter.
b. Make a test version of MyClass called something like TestMyClass.
Give that a public version of the myList attribute called myTestList.
c. Override getMyList so it will use myTestList instead of myList.
d. The test case class will instantiate TestMyClass, and call its
methods polymorphically through an variable of type MyClass.
e. When the test case wants to check the list values, it will just
cast MyClass back to TestMyClass and access the public myTestList.
The upside of this is that myList gets to remain private in the
production code. The downside is that we are not actually using
myList in the test runs. My thought was that if myTestList is the
same type as myList and the override version of getMyList does exactly
the same thing as the production code's getMyList (except for acting
on the test version of myList), this downside becomes negligible.
Note that we would define TestMyClass as an inner class of the JUnit
test case so it will not have a .java file generated for it.
Any thoughts on this approach? Good? Bad? Recommend a different
approach?
Thanks for any feedback!
Ken
In Java, we have a class MyClass that privately contains an ArrayList
as an attribute called myList. We were looking for a way to enable a
JUnit test case class to call MyClass methods, then check the state of
myList.
Here's a what we are considering:
a. Make a protected method in MyClass called getMyList that returns
myList and performs lazy instantiation (it'll only instantiate myList
the first time it is called but will return myList everytime).
MyClass should only access myList through this getter.
b. Make a test version of MyClass called something like TestMyClass.
Give that a public version of the myList attribute called myTestList.
c. Override getMyList so it will use myTestList instead of myList.
d. The test case class will instantiate TestMyClass, and call its
methods polymorphically through an variable of type MyClass.
e. When the test case wants to check the list values, it will just
cast MyClass back to TestMyClass and access the public myTestList.
The upside of this is that myList gets to remain private in the
production code. The downside is that we are not actually using
myList in the test runs. My thought was that if myTestList is the
same type as myList and the override version of getMyList does exactly
the same thing as the production code's getMyList (except for acting
on the test version of myList), this downside becomes negligible.
Note that we would define TestMyClass as an inner class of the JUnit
test case so it will not have a .java file generated for it.
Any thoughts on this approach? Good? Bad? Recommend a different
approach?
Thanks for any feedback!
Ken