天天看點

Junit中如何擷取class的private屬性

public class SubObject {

    @Inject

    private TradeService tradeService;

    public void onload(){

        tradeService.test();

    }

}

在JunitTest時如何擷取advanceservice和tradeService 屬性

SubObject subObject = new SubObject();

TradeService tradeService = mock(TradeService.class);

Field tradeServiceField = subObject.getClass().getDeclaredField("tradeService");

tradeServiceField.setAccessible(true);

tradeServiceField.set(subObject , tradeService );

轉載于:https://blog.51cto.com/angelgirl/1672582