[英]org.powermock.api.mockito.ClassNotPreparedException in Static class mocking
I am writing a unit test to mock a static method in the verticle but getting ClassNotPreparedException always. 我正在编写一个单元测试来模拟verticle中的静态方法,但始终获取ClassNotPreparedException。 I think that its only possible to mock this way if only the class is static, but i have non static class.
我认为,只有类是静态的,才有可能以这种方式进行模拟,但是我有非静态的类。 What am i missing?
我想念什么?
I have tried various solutions like using @rule or @PowerMockIgnore 我尝试了各种解决方案,例如使用@rule或@PowerMockIgnore
//myVerticleTest.java
package com.blabla.me.verticles;
import static com.google.common.truth.Truth.assertThat;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import io.vertx.core.Vertx;
import io.vertx.junit5.VertxTestContext;
import io.vulpx.VulpxTestBase;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.junit.runner.RunWith;
import com.blabla.me.verticles.AdditionalInformationCardVerticle;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.junit.Rule;
import com.blabla.me.verticles.st;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ st.class })
@PowerMockIgnore({"org.mockito.*"})
public class myVerticleTest extends VulpxTestBase {
@Rule public PowerMockRule rule = new PowerMockRule();
private Vertx vertx;
private AdditionalInformationCardVerticle dummy;
@BeforeEach
@PrepareForTest({ st.class })
public void setUp(VertxTestContext testContext) throws Exception {
vertx = Vertx.vertx();
try {
PowerMockito.mockStatic(st.class);
PowerMockito.when(st.createClient()).thenReturn("kk");
//deploying verticle
dummy = new AdditionalInformationCardVerticle();
vertx.deployVerticle(dummy, testContext.completing());
} catch (Exception e) {
System.out.println("heyyy eroorrr : " + e);
}
}
@Test
@PrepareForTest({ st.class })
public void justnormaltest() {
cla ownclass = new cla();
String k = ownclass.createfromclass();
assertThat("kk").isEqualTo(k);
}
}
// st.java
public class st {
public static String createClient() {
return "kk";
}
}
// cla.java
public class cla {
public String createfromclass() {
return st.createClient();
}
}
I expect it to run the assertion but i always get below excpetion: "org.powermock.api.mockito.ClassNotPreparedException: The class com.sap.me.verticles.st not prepared for test. To prepare this class, add class to the '@PrepareForTest' annotation. In case if you don't use this annotation, add the annotation on class or method level. " 我希望它能运行该断言,但我总是会感到惊讶:“ org.powermock.api.mockito.ClassNotPreparedException:com.sap.me.verticles.st类未准备好进行测试。要准备此类,请将类添加到“ @PrepareForTest”注释。如果不使用此注释,请在类或方法级别添加注释。
Here: 这里:
@PrepareForTest({ st.class })
That one goes to exactly one place: in front of your test class public class myVerticleTest
. 那恰好到达了一个地方:在测试类
public class myVerticleTest
。
And hint: instead of adding more and more "things" to not working code: pick any good documentation, and try to follow that to the last ; 提示:不要在无法正常工作的代码中添加越来越多的“东西”:选择任何好的文档,并尝试将其遵循到最后; in the example code (instead of assuming that adding more and more things here or there would help).
在示例代码中(而不是假设在此处或此处添加越来越多的东西会有所帮助)。
One good starting point: the official documentation on static mocking. 一个很好的起点:静态模拟的官方文档 。
And of course, the usual caveat: consider not learning about PowerMock in the first place. 当然,这也是通常的警告:首先不要考虑学习PowerMock。 Instead focus on writing "easy to test" code.
而是专注于编写“易于测试”的代码。 Far too often, people think PowerMock(ito) is the answer to their problem.
人们常常认为PowerMock(ito)是他们问题的答案。 When their problem in reality is their inability to write "easy to test" production code.
当他们实际遇到的问题是他们无法编写“易于测试”的生产代码时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.