0

I know that method with the private access can be tested using 1. reflection or 2. powermockito but how do you test default class with private methods and the parent class is abstract?

class AirSpaceBuilder extends SynchronizedSpace {

    private Request request;
    private Status status;

public AirSpaceBuilder (Log log, SpaceTable st, Collection locks, SpaceRequest request) {
        super(log, st, locks, request.getAirSpace());
        this.request = request;
}
    private Status recursiveSignal() {
...
}

abstract class SynchronizedSpace {

    private Collection sync;
    private String requester;
    private SpaceTable st;
    private Log log;
...
}

My first thought was to call it from the higher level but then it becomes integrated test and defeated the purpose of unit testing.

Edit: Correct me if I am wrong but is this how you invoke the method?

Object airSpaceBuilder=Class.forName("com.airport.resource.AirSpaceBuilder").newInstance();
Method m = airSpaceBuilder.getClass().getDeclaredMethod("recursiveSignal");
m.setAccessible(true);
m.invoke(allocationFunction);
logger
  • 1,983
  • 5
  • 31
  • 57
  • Reflection will work. Test what you wish; no one will haul you off to jail for lack of purity. Personally I find these arguments tiresome. – duffymo Sep 15 '15 at 16:47
  • I tried reflection but it does not recognize the class – logger Sep 15 '15 at 16:47
  • What package are these classes in, and what package is your test suite running in? – Makoto Sep 15 '15 at 16:51
  • package for the class is in com.airport.resource and for test it was in com.airport.jtest.resource. – logger Sep 15 '15 at 16:53
  • The duplicate will get you the *rest* of the way there, but you need to have your tests in the same package as the package-private code you're trying to test. If you don't, you'll run into the same visibility problems that you're experiencing now. That is to say: ditch the `jtest` directory. – Makoto Sep 15 '15 at 17:17

0 Answers0