1
votes

I am trying to unit test a code that deals with Message Logging which brings in a lot of environmental dependency. This Message Logger is a framework used by all developers across teams. I have used PowerMock for the same since the Logging class is a static class.

Though the Junit Unit test runs Green after using Power Mock, it is still attempting to load the server.xml file.

The Class calls are as follows

Unit Test Class calls -> Static Logger Class calls-> Static Environment Class.

This Static Environment Class deals with loading and parsing the server.xml file.

I have tried the following as well but even then its tring to load the xml file:

@RunWith(PowerMockRunner.class)

@PrepareForTest({Logger.class,EnvFunctions.class})

PowerMockito.mockStatic(Logger.class);

PowerMockito.mockStatic(EnvFunctions.class);

Do i need to do some extra work for it to no longer attempt to load that file?

I am tyring to mock a static void class and have tried to use doNothing and supress as well. but its not working out

doNothing.when(Logger.class);

suppress(everythingDeclaredIn(Logger.class));
1

1 Answers

1
votes

Thank you for the response.

I was able to solve it with the above explanation

It was attempting to load server.xml file because the Logger.class has a static initializer block which was calling another static method dealing with loading of the xml file.

@SuppressStaticInitializationFor annototation for powermock did the trick

@RunWith(PowerMockRunner.class)

@PrepareForTest({Logger.class})

@SuppressStaticInitializationFor("org.mycompany.Logger")
public Class A{
PowerMockito.mockStatic(Logger.class);
}