简体   繁体   English

如何模拟私人静态决赛

[英]How to mock a private static final

With the new error popping up with java 7 & 8 when using Mockito and PowerMockRunner, Java will throw an Error in byte code exception when there is a static final variable involved. 使用Mockito和PowerMockRunner时,随着Java 7和8弹出新错误,当涉及到静态最终变量时,Java将在字节码异常中引发错误。 This is due to the now stricter byte code verification and mocking static final objects editing the byte code in order to successfully mock. 这是由于现在更加严格的字节码验证和模拟静态最终对象编辑了字节码以便成功进行模拟。

I have the following class that I am trying to mock: 我尝试模仿以下课程:

public class ClassToBeMocked {
    private static final int LIMIT_FROM_PROPERTIES = AnotherClazz.methodToRetrieveFromMap("String being called")

    //more stuff
}

I have seen that you can get around this by by using reflection, seen here How to mock a static final variable using JUnit, EasyMock or PowerMock and here PowerMock: mock out private static final variable, a concrete example (not a great solution but it should work). 我已经看到您可以通过使用反射来解决此问题,请参见此处如何使用JUnit,EasyMock或PowerMock 模拟静态最终变量,以及此处的PowerMock:模拟出私有静态最终变量,这是一个具体示例 (不是很好的解决方案,但是应该管用)。 However, using reflection requires that the object already be instantiated, and I am getting the bytecode exception when trying to instantiate ClassToBeMocked. 但是,使用反射要求对象已被实例化,并且在尝试实例化ClassToBeMocked时出现字节码异常。

I have also tried mocking the AnotherClazz.methodToRetrieveFromMap(String) in the unit test (using correct syntax): 我还尝试在单元测试中模拟AnotherClazz.methodToRetrieveFromMap(String)(使用正确的语法):

Mockito.when( AnotherClazz.methodToRetrieveFromMap("String being called") ).thenReturn(10);

However, This results in the byte code error again. 但是,这再次导致字节码错误。

Is there a way around this catch-22 or a different framework or unit runner that would be better to use? 是否有办法解决这个问题22或使用更好的其他框架或单元运行器?

I think there is no way to do this without reflection. 我认为没有反思就没有办法。 Anyway, I consider there is probably something 'wrong' in your design if you need to change static final constants, although you need it only for test scope. 无论如何,我认为如果您需要更改static final常量,则您的设计中可能存在“错误”,尽管您只需要在测试范围内使用即可。

As you say in your question, there are some ways to do it with PowerMock / EasyMock , but they still remain reflection anyway. 正如您在问题中所说的那样,可以使用PowerMock / EasyMock进行某些操作,但是无论如何它们仍然会反映出来。

I'll be waiting for possible alternatives in other answers. 我将在其他答案中等待其他可能的选择。

I would suggest you to consider changing of your production code to avid usage static and final. 我建议您考虑将生产代码更改为静态和最终用法。 These are well known testability killers. 这些是众所周知的可测试性杀手。 Bytecode manipulation problems you are experiencing are known issues when you try to fake these constructs. 当您尝试伪造这些构造时,您遇到的字节码操作问题是已知问题。

BTW, Make sure that your PowerMock version is up to date. 顺便说一句,请确保您的PowerMock版本是最新的。 Also make sure that your Mockito version match with PowerMock. 还要确保您的Mockito版本与PowerMock匹配。 You can find PowerMock version matrix here. 您可以在此处找到PowerMock版本矩阵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM