Junit 从类中声明的私有静态变量调用的模拟静态方法

Junit 从类中声明的私有静态变量调用的模拟静态方法,junit,mockito,powermockito,Junit,Mockito,Powermockito,从类中声明的私有静态变量调用的模拟静态方法 public class User{ private static int refresh = readConfig(); public static int readConfig(){ // db call } } 我尝试使用powermockito来模拟readConfig方法,但它不起作用。我需要在类加载时模拟readConfig() PowerMockito.mockStatic(User.class); Pow

从类中声明的私有静态变量调用的模拟静态方法

public class User{
   private static int refresh = readConfig();

   public static int readConfig(){
     // db call
   }
}
我尝试使用powermockito来模拟readConfig方法,但它不起作用。我需要在类加载时模拟readConfig()

PowerMockito.mockStatic(User.class);
PowerMockito.when(User.readConfig()).thenReturn(1);

请告诉我如何模拟readConfig方法。

当您无法模拟与
静态
块相关的任何内容时,
您可以告诉
PowerMockito
通过使用注释您的测试来抑制它

请注意,这样做将不会执行
readConfig()
方法,并且
refresh
变量将保留其默认值(在本例中为0)

但这对您来说似乎并不重要,因为根据您的评论判断,您主要是试图抑制相关的数据库错误。由于变量是私有的,您(必须)模拟所有相关的方法,因此在测试期间不太可能使用它

如果需要将其设置为某个特定值,则必须使用
反射

封装测试;
导入org.junit.Assert;
导入org.junit.Test;
导入org.junit.runner.RunWith;
导入org.powermock.api.mockito.PowerMockito;
导入org.powermock.core.classloader.annotations.PrepareForTest;
导入org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
导入org.powermock.modules.junit4.PowerMockRunner;
类用户{
受保护的静态int refresh=readConfig();
公共静态int readConfig(){
返回-1;
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest(StaticTest.class)
@({“test.User”})的SuppressStaticInitialization
公共类静态测试{
@试验
public void test()引发异常{
mockStatic(User.class);
PowerMockito.when(User.readConfig()).thenReturn(42);
Assert.assertEquals(0,User.refresh);
Assert.assertEquals(42,User.readConfig());
}
}

当您无法模拟与
静态
块相关的任何内容时,
您可以告诉
PowerMockito
通过使用注释您的测试来抑制它

请注意,这样做将不会执行
readConfig()
方法,并且
refresh
变量将保留其默认值(在本例中为0)

但这对您来说似乎并不重要,因为根据您的评论判断,您主要是试图抑制相关的数据库错误。由于变量是私有的,您(必须)模拟所有相关的方法,因此在测试期间不太可能使用它

如果需要将其设置为某个特定值,则必须使用
反射

封装测试;
导入org.junit.Assert;
导入org.junit.Test;
导入org.junit.runner.RunWith;
导入org.powermock.api.mockito.PowerMockito;
导入org.powermock.core.classloader.annotations.PrepareForTest;
导入org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
导入org.powermock.modules.junit4.PowerMockRunner;
类用户{
受保护的静态int refresh=readConfig();
公共静态int readConfig(){
返回-1;
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest(StaticTest.class)
@({“test.User”})的SuppressStaticInitialization
公共类静态测试{
@试验
public void test()引发异常{
mockStatic(User.class);
PowerMockito.when(User.readConfig()).thenReturn(42);
Assert.assertEquals(0,User.refresh);
Assert.assertEquals(42,User.readConfig());
}
}

在模拟从最终静态变量调用的静态方法时也存在同样的问题。 假设我们有这个类:

class Example {

public static final String name = "Example " + Post.getString();

   .
   .
   .
}
所以我需要类Post的模拟静态方法getString()。这应该在类加载时完成

PowerMockito.mockStatic(User.class);
PowerMockito.when(User.readConfig()).thenReturn(1);
所以我发现的最简单的方法是:

@BeforeClass
public static void setUpBeforeClass()
{
    PowerMockito.mockStatic(Post.class);
    PowerMockito.when(Post.getString(anyString(), anyString())).thenReturn("");
}
以及我在测试类顶部的注释:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Post.class)
@PowerMockIgnore({"jdk.internal.reflect.*", "javax.management.*"})

在模拟从最终静态变量调用的静态方法时也存在同样的问题。 假设我们有这个类:

class Example {

public static final String name = "Example " + Post.getString();

   .
   .
   .
}
所以我需要类Post的模拟静态方法getString()。这应该在类加载时完成

PowerMockito.mockStatic(User.class);
PowerMockito.when(User.readConfig()).thenReturn(1);
所以我发现的最简单的方法是:

@BeforeClass
public static void setUpBeforeClass()
{
    PowerMockito.mockStatic(Post.class);
    PowerMockito.when(Post.getString(anyString(), anyString())).thenReturn("");
}
以及我在测试类顶部的注释:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Post.class)
@PowerMockIgnore({"jdk.internal.reflect.*", "javax.management.*"})

它不起作用
是一个相当宽泛的说法。它没有告诉我们实际发生了什么,也没有告诉我们预期发生了什么。如果没有一个能够澄清您的具体问题或额外细节以准确强调所做的工作,就很难重现问题,从而更好地理解所问的问题。在PowerMockito.mockStatic(User.class)执行时,它也在执行readConfig方法&为与数据库语句相关的mock提供错误。在我们模拟类readConfig时,正在调用该类。但在下一个声明中,我们是在嘲笑它。这就是为什么我明确提到我必须在类加载时模拟readConfig()。
它不工作
是一个相当宽泛的说法。它没有告诉我们实际发生了什么,也没有告诉我们预期发生了什么。如果没有一个能够澄清您的具体问题或额外细节以准确强调所做的工作,就很难重现问题,从而更好地理解所问的问题。在PowerMockito.mockStatic(User.class)执行时,它也在执行readConfig方法&为与数据库语句相关的mock提供错误。在我们模拟类readConfig时,正在调用该类。但在下一个声明中,我们是在嘲笑它。这就是为什么我明确提到我必须在类加载时模拟readConfig()。