Why PowerMock is attempting to load the server.xml file if the static class is mocked out?

荒凉一梦 提交于 2019-12-11 09:21:13

问题


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:


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);
}


来源:https://stackoverflow.com/questions/23195015/why-powermock-is-attempting-to-load-the-server-xml-file-if-the-static-class-is-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!