Saturday, 15 August 2015

java - Super class access to a default class from another package -



java - Super class access to a default class from another package -

i trying create reusable template class unit test cases. suppose classes sample, , sampletest belong same package, while template belongs different package. error instantiating sample class. expect method executed if method of sampletest class have access sample. there ways create work?

public sampletest extends template<sample> { public void testmethod() { gettarget().method(); } } public class template<t> extends testcase { /** spied, object instance test. */ private transient t targetobject; /** test class type derived parameter type of test subclass. */ private transient final class<t> targettype; @override public void setup() throws exception { super.setup(); setuptargetobject(); } protected void setuptargetobject() { final t realobject = targettype.newinstance(); //error here if class under test non-public targetobject = mockito.spy(realobject); } protected template() { final parameterizedtype paramedtype = (parameterizedtype) getclass().getgenericsuperclass(); targettype = (class<t>) paramedtype.getactualtypearguments()[0]; } protected t gettarget() { homecoming targetobject; } }

java prevents calling non-public method (and thus, constructor) of class in different package. prevented compiler and jvm. hence can't phone call these methods directly, and, in case, can't phone call these methods via java reflection api.

a solution be, suggested, utilize setaccessible(true) on constructor.

java unit-testing inheritance reflection

No comments:

Post a Comment