I'm writing JUnit tests to test code in another package which must match a pre-defined specification. The code under test must contains a nested class which I want to instantiate inside the test package -- it's not possible to import this class because it's nested (and so not visible outside the original package).
E.g. package structure:
packageA
-> ClassA
-> packageA.ClassA$ClassB
tests
-> ClassATest
I need to test a method in ClassA that takes an instance of ClassB as an argument.
I'm currently using reflection to get from ClassA
(which I can import) to ClassB
[cls.getDeclaredClasses
]. I can then get a constructor for ClassB
[cls.getDeclaredConstructor
] and thus create a new instance of ClassB
using the found constructor [constructor.newInstance
]. This call to constructor.newInstance
returns an Object
.
How do I get from the returned Object
type to something of the correct ClassB
type so that I can pass it into the ClassA
method under test?
Aucun commentaire:
Enregistrer un commentaire