views:

526

answers:

3

Hi,

I couldnt able to mock the protected varibale defined in the superclass.i could able to mock the protected method in superclass but couldnt to mock the protected variable in to the subclass ,wherein am writing the testcase for subclass,Please if anybody out there has any soluton for it .please reply.

Thanks Shashi

A: 

jmockit can only mock methods, if you try to mock singleton you could try to mock getInstance method.

You might try PowerMock's Bypass encapsulation

http://code.google.com/p/powermock/wiki/BypassEncapsulation

01
A: 

All mocking tools only mock methods.

JMockit also has utility methods that provide access to non-accessible fields, methods, and constructors from a given test. They are directly available to subclasses of mockit.Expectations and mockit.Verifications. The same methods are also exposed in the static mockit.Deencapsulation class.

Rogerio
A: 

You can do this using the Deencapsulation.setField method. For example:

@Test public void staticVarOverride() {
    MyClass myClass = new MyClass();
    Deencapsulation.setField(MyClass.class, "myStatic", 2);
    // Assertions, verifications go here.
}

This will set the value of the static variable myStatic to 2. Note that while this will work for statics, it will not work for static finals. AFAIK there is no way to override static finals.

jchilders