2

I have a few private static final fields in the class I want to test. Like follows

public class ClassToTest{
    ....
    private static final Myclass myclass = MyClassFactory.getMyClass(type.firstType);
    ....
}

The type is a enum in the MyClassFactory. That factory do is it initialize object according to type passed and return.

My question is does powermock support this and if so how to do this.

Susitha Ravinda Senarath
  • 1,648
  • 2
  • 27
  • 49
  • possible duplicate of [How to test a class that has private methods, fields or inner classes](http://stackoverflow.com/questions/34571/how-to-test-a-class-that-has-private-methods-fields-or-inner-classes) – Raedwald Aug 01 '14 at 09:33

3 Answers3

4

You can use reflection also if any mock library works for you.

Field f = classToTest.getclass().getDeclaredField("myclass ");
f.setAccessible(true);
f.set(classToTest,/*NEW VALUE*/);
RamonBoza
  • 8,898
  • 6
  • 36
  • 48
2

PowerMock ( + a mocking framework ) will allow you to do this. Presumeably you're talking about mocking MyClassFactory.getMyClass() ?

See this question for an example

Community
  • 1
  • 1
DaveH
  • 7,187
  • 5
  • 32
  • 53
1

Why do you want to test this value? Shouldn't you test your enum, test if it returns the correct value when a given type is passed to it. If you want to test the assignment of the enum to the field you are doubting basic java assignment.

Tom Jonckheere
  • 1,630
  • 3
  • 20
  • 37
  • I don't want to test this. The issue is "MyClassFactory" is in another project. So when doing unit test it cannot find the factory and problems occur. I just want to get throught them. – Susitha Ravinda Senarath Oct 22 '13 at 12:06