当前位置: 首页 > 知识库问答 >
问题:

为什么我在这里得到UnfinishedStubbingException?

徐文斌
2023-03-14

测试代码为:

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.time.LocalTime;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
public class HelloWorldByHour {

    private static LocalTime REST_START_TIME = LocalTime.of(14, 0);
    private static LocalTime REST_END_TIME = LocalTime.of(16, 0);

    public static void main(String[] args) throws Exception {
        HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
        server.createContext("/greeting", new MyHandler());
        server.setExecutor(null); // creates a default executor
        server.start();
    }

    private static class MyHandler implements HttpHandler {
        public void handle(HttpExchange t) throws IOException {
            LocalTime time = LocalTime.now();
            String response;

            if (time.isAfter(REST_START_TIME) && time.isBefore(REST_END_TIME)) {
                response = "Nap time";
            } else {
                response = "Hello World";
            }

            t.sendResponseHeaders(200, response.length());
            OutputStream os = t.getResponseBody();
            os.write(response.getBytes());
            os.close();
        }
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>server</groupId>
    <artifactId>server</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>(whatever version is current)</version>
                <configuration>
                    <!-- or whatever version you use -->
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito</artifactId>
            <version>1.6.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-core</artifactId>
            <version>1.6.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>1.6.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
    </dependencies>
</project>

测试代码为:

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.time.LocalTime;

import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest(LocalTime.class)
public class HelloWorldByHourTest {

    private String sendRequest() throws IOException {
        URL url = new URL("localhost:8080/greeting");
        BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
        String strTemp = "";
        StringBuilder sb = new StringBuilder();
        while (null != (strTemp = br.readLine())) {
            sb.append(strTemp);
        }
        return sb.toString();
    }

    @Test
    public void testMain() throws Exception {

        mockStatic(LocalTime.class);
        when(LocalTime.now()).thenReturn(LocalTime.of(15, 0));
        HelloWorldByHour.main(null);
        String response = sendRequest();
        Assert.assertEquals("Nap time", response);
    }
}

你知道怎么了吗?

共有1个答案

耿玄裳
2023-03-14

问题是在When和ThenReturn中调用了LocalDate上的静态方法

试试这样的方法:

    LocalTime time = LocalTime.of(15,0);
    mockStatic(LocalTime.class);
    when(LocalTime.now()).thenReturn(time);

 类似资料:
  • 我遇到JSON解析错误。我的代码如下: 我从我的检查中得到以下错误: 由于:com,无法分析JSON。谷歌。格森。JsonSyntaxException:java。lang.IllegalStateException:应为BEGIN\u对象,但在第1行第2列为BEGIN\u数组 对于我试图读取的JSON,如果成功,我的应该返回5。 我做错了什么?

  • 谁能告诉我为什么吗?类的构造函数是,但是编译器仍然声称找不到它的构造函数。

  • 问题内容: public class Category { 在正在生成。 问题答案: 当您执行时,您称呼孩子们的。这里没有问题,只不过您在这里调用了父对象。这将称呼孩子,等等。 不错的无限循环。 摆脱它的最好方法是将您的方法更改为: 这样,您将不打印parentCategory,而仅显示其名称,不显示无限循环,不显示StackOverflowError。 编辑: 正如博洛在下面说的那样,您将需要检

  • 我是新手,但我正在编写一个应用程序,我不断收到这个错误,导致应用程序在启动时崩溃。 导致它的代码在下面的类中,在问题的行旁边会有一个这里的注释。 这就是我调用方法的代码。 这是堆栈跟踪。 谢谢你的帮助。

  • 问题内容: 我创建了一个用于显示工具提示的指令: 对应功能: 应用于此: 这是我观点的一部分,由拥有者的控制器处理 为什么必须调用才能将更改应用到,该更改是早先声明和初始化的? 问题答案: 因为附加到事件的回调超出了angular的范围;angular不知道该函数何时运行/结束,因此摘要循环永远不会运行。 调用或告诉angular更新绑定并触发任何手表。

  • 我遇到了这个代码 我在字段上看到过@autowire,它的意思是按类型自动连接,使用这个字段的类将获得特定类型的bean。 但是在上面的代码中,我不确定是谁在使用这个RootResource bean? 这是spring-泽西Rest项目。 据我所知,spring将创建RootResource的bean,将使用这个bean来设置它的属性。(我看不到此bean的任何显式配置) 我的问题是, 1)这个