Reputation: 1396
As we know PowerMockito can prepare for test only one final, static, or private class like following:
@RunWith(PowerMockRunner.class)
@PrepareForTest(SomeClassNumber1.class)
public class SomeClassUnderTest{
@Before
public void setUp() throws Exception {
}
@Test
public void testSomeMethod() throws Exception {
}
}
But the method I am going to test uses more than one static classes. The thing I want to do looks like:
@RunWith(PowerMockRunner.class)
@PrepareForTest(SomeClassNumber1.class, SomeClassNumber2.class)
public class SomeClassUnderTest{
But @PrepareForTest has only one argument.
Edit: For instance the method will use singleton, factory or some static methods of different classes.
public class SomeClass {
private Session session;
private Object object;
public void someMethod(int userId) {
Session session = Session.getSession(userId);
Object object = Singleton.getInstance();
}
}
Note: The problem could be solved by using single responsibility principle. e.g. instead of using singleton inside the method that is going to be tested, I can extract it to another method like following, and mock it:
public class SomeClass {
private Session session;
private Object object;
public void someMethod(int userId) {
session = getSession(userId);
object = getInstance();
}
private getSession(userId) {
return Session.getSession(userId);
}
private Object getInstance(){
return Singleton.getInstance();
}
}
But this too doesn't seem good solution for me. It is better if PowerMockito or other testing tools have the feature that can mock several static classes at the same time.
Upvotes: 0
Views: 1497
Reputation: 15622
Edit: For instance the method will use singleton, factory or some static methods of different classes.
You should not use static access to class members and methods in the first place, you should also avoid the Java singelton pattern, There are other possibilities to make sure you have only one instance of a class at runtime.
Having written this lets answer your question:
@PrepareForTest has only one argument.
This single argument may be an array:
@PrepareForTest({Class1.class,Class2.class})
Upvotes: 1