如果静态类被模拟,为什么PowerMock尝试加载server.xml文件? [英] Why PowerMock is attempting to load the server.xml file if the static class is mocked out?

查看:176
本文介绍了如果静态类被模拟,为什么PowerMock尝试加载server.xml文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对与消息记录相关的代码进行单元测试,这带来了很多环境依赖性.此消息记录器是团队中所有开发人员使用的框架.由于Logging类是静态类,因此我将PowerMock用于相同的情况.

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.

尽管Junit Unit测试在使用Power Mock后以绿色运行,但仍在尝试加载server.xml文件.

Class调用如下

单元测试类调用->静态记录器类调用->静态环境类.

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

此静态环境类负责加载和解析server.xml文件.

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

我也尝试了以下方法,但是即使如此,它仍然试图加载xml文件:

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

我是否需要做一些额外的工作以使其不再尝试加载该文件?

我想模拟一个静态的void类,并尝试使用doNothing和supress.但效果不佳

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

推荐答案

感谢您的答复.

我能够通过上面的解释来解决

I was able to solve it with the above explanation

它试图加载server.xml文件,因为Logger.class具有一个静态初始化程序块,该块正在调用另一个处理xml文件加载的静态方法.

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.

@SuppressStaticInitialization对于powermock的注解成功了

@SuppressStaticInitializationFor annototation for powermock did the trick

@RunWith(PowerMockRunner.class)

@PrepareForTest({Logger.class})

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

这篇关于如果静态类被模拟,为什么PowerMock尝试加载server.xml文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆