Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 静态类模拟中的org.powermock.api.mockito.ClassNotPreparedException_Java_Junit_Mockito_Powermock_Vert.x - Fatal编程技术网

Java 静态类模拟中的org.powermock.api.mockito.ClassNotPreparedException

Java 静态类模拟中的org.powermock.api.mockito.ClassNotPreparedException,java,junit,mockito,powermock,vert.x,Java,Junit,Mockito,Powermock,Vert.x,我正在编写一个单元测试来模拟垂直中的静态方法,但总是得到ClassNotPreparedException。我认为只有在类是静态的情况下才有可能以这种方式进行模拟,但我有非静态类。我错过了什么 我尝试过各种解决方案,比如使用@rule或@PowerMockIgnore //myVerticleTest.java package com.blabla.me.verticles; import static com.google.common.truth.Truth.assertThat; imp

我正在编写一个单元测试来模拟垂直中的静态方法,但总是得到ClassNotPreparedException。我认为只有在类是静态的情况下才有可能以这种方式进行模拟,但我有非静态类。我错过了什么

我尝试过各种解决方案,比如使用@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);
    }
}
我希望它运行断言,但我总是得到以下例外: “org.powermock.api.mockito.ClassNotPreparedException: 类com.sap.me.verticles.st未准备好进行测试。 若要准备此类,请将类添加到“@PrepareForTest”注释中。 如果不使用此注释,请在类或方法级别添加注释。“

此处:

@PrepareForTest({ st.class })
那一个正好到了一个地方:在你的测试类
公共类myVerticleTest
前面

提示:不要在不起作用的代码中添加越来越多的“东西”:选择任何好的文档,并尽量遵循到最后;在示例代码中(而不是假设在这里或那里添加越来越多的东西会有所帮助)

一个很好的起点是:官方的静态模拟


当然,通常的警告是:首先不要考虑学习PrimeMoCK。相反,要专注于编写“易于测试”的代码。很多时候,人们认为PowerMock(ito)是解决他们问题的答案。实际上,他们的问题是无法编写“易于测试”的生产代码。

请注意:遵循java命名约定。类名是大写的,和往常一样:名称应该是有意义的。st和cla不是。我试过只在类中使用@PrepareForTest,无论哪种方法都不行work@MPMartis然后创建一个真实的。您应该使用的最小代码量。说真的:你在你的例子中塞了这么多东西,这根本没有意义。理想情况下:选择该示例代码。验证它是否有效。然后根据您的需要对其进行增强。每次进行更改时,都要运行测试以确保仍然看到预期的结果。PowerMock不是我喜欢的框架:但请放心:它可以工作。你做错了什么。所以从小事做起,检查每一步都会发生什么。
// cla.java
public class cla {
    public String createfromclass() {
        return st.createClient();
    }
}
@PrepareForTest({ st.class })