简体   繁体   English

Mocking Java 使用 Mockito/JUnit 的常量变量(公共静态)

[英]Mocking Java Constant variables (public static) using Mockito/JUnit

I need to mock the constant variable in order to test one of my method.我需要模拟常量变量以测试我的一种方法。 How can I do it with Mokito and Junit.如何使用 Mokito 和 Junit 来做到这一点。

  @Component( "mybean" )
  @org.springframework.context.annotation.Scope( value="session" )  
  public class MyBean {

    Public void methodToBeTested() {
       if (!AppConst.SOME_CONST.equals(obj.getCostCode())) {
         // some logic
       }
    }
 }

AppConst class AppConst class

@Configuration
public class AppConst
{
   public static  String  SOME_CONST;
   public static  String  HOST_URL;

   @PostConstruct
   public void postConstruct()
   {
     SOME_CONST = "My Code";
     HOST_URL  = "Some URL";

   }
}

So, from my junit test class, how can I mock the AppConst and it's variables?那么,从我的 junit 测试 class 中,我如何模拟 AppConst 及其变量? Now, when I run it, i hit a nullpointer error.现在,当我运行它时,我遇到了一个空指针错误。

Can this be done with powermock?这可以用powermock完成吗? if yes please give some sample如果是,请提供一些样品

Mockito version I use.我用的 Mockito 版本。

compile "org.mockito:mockito-all:1.9.5" compile "org.powermock:powermock-mockito-release-full:1.6.1"

Instead of mocking there would be another solution to be able to test it:除了 mocking 之外,还有另一种解决方案可以对其进行测试:

public void methodToBeTested(SomeObject obj) {
   performLogic(AppConst.SOME_CONST, obj);
}

boolean performLogic(String check, SomeObject testObj) {
   if (!check.equals(obj.getCostCode())) {
     // some logic
     return true;
   }
   return false;
}

That way you can test two things, both combined show you that your code works as intended:这样您就可以测试两件事,两者结合起来表明您的代码按预期工作:

public void testMethodToBeTested() {
    MyBean mb = new MyBean() {
        @Override
        void performLogic(String check, SomeObject testObj) {
            assertSame("check constant is passed", AppConst.SOME_CONST, check);
        }
    }
    mb.methodToBeTested(new SomeObject());

    mb = new MyBean();
    SomeObject so = createSomeTestObject("My Code"); // not the actual constant but an equal String
    assertFalse("check some logic not occurred", mb.performLogic("My Code", so));
    so = createSomeTestObject("Not the constant");
    assertFalse("check some logic not occurred", mb.performLogic("Not the constant", so));
    assertTrue("check some logic occurred", mb.performLogic("My Code", so));
    // additional tests covering the actual logic
}

Another solution could be putting the condition of the if-statement into its own method, eg shouldLogicOccurr(String check) and test that method individually.另一种解决方案是将 if 语句的条件放入它自己的方法中,例如shouldLogicOccurr(String check)并单独测试该方法。

In other words: Sometimes it's necessary to refactor your code to make tests easier or sometimes even possible at all.换句话说:有时有必要重构您的代码以使测试更容易,有时甚至根本不可能。 A good side effect is the next time you implement something you already have testability in mind and create your code suitable for this in the first place.一个好的副作用是下一次实现某些东西时,您已经考虑到可测试性并首先创建适合于此的代码。

Mocking is a good way to get things under test that use third party libraries that can't be changed and have too many dependencies to be set up but if you end up using that for your own code you've got a design issue. Mocking 是一种很好的方法来测试使用第三方库的东西,这些第三方库无法更改并且需要设置太多依赖项,但如果您最终将其用于自己的代码,那么您就会遇到设计问题。

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

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