为什么我在这里得到UnfinishedStubbingException?

时间:2016-07-19 17:59:27

标签: java maven unit-testing junit powermockito

测试代码是:

<img src="x" title="What you want showed"/>

运行测试时,我得到:

  

org.mockito.exceptions.misusing.UnfinishedStubbingException:   这里检测到未完成的存根:    - &GT;在HelloWorldByHourTest.testMain(HelloWorldByHourTest.java:35)

     

E.g。 thenReturn()可能会丢失。   正确存根的例子:       当(mock.isOk())thenReturn(真)。       当(mock.isOk())thenThrow(例外)。       doThrow(例外)。当(模拟).someVoidMethod();   提示:    1.缺少返回()    2.你正试图找到最后一种方法,你顽皮的开发者!    3:你正在查看另一个模拟内部的行为然后返回&#39;然后返回&#39;指示完成后

我的pom.xml是

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>

知道什么是错的吗?

2 个答案:

答案 0 :(得分:1)

问题是您在whenthenReturn

中调用LocalDate上的静态方法

尝试类似的东西:

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

答案 1 :(得分:1)

通过此次致电when(LocalTime.now()).thenReturn(LocalTime.of(15, 0));,您可以选择以下方式:

  

3:你正在扼杀另一个模拟人员的行为之前&#39;然后返回&#39;指示完成。

基本上,您已静态嘲笑LocalTime课程,但您在定义LocalTime.of(15, 0)的行为时也会调用静态LocalTime.now()方法。

根据你想要做什么,我想一个解决方法是在模拟静态方法之前创建一个LocalTime的模拟,类似于:

@Test
public void testMain() throws Exception {
    // create a mock
    LocalTime mockLocalTime = mock(LocalTime.class);
    // TODO define behaviour of mockLocalTime

    // mock the static methods
    mockStatic(LocalTime.class);
    when(LocalTime.now()).thenReturn(mockLocalTime);

    // invoke object under test
    HelloWorldByHour.main(null);
    String response = sendRequest();

    // interaction verification and/or assersions
    Assert.assertEquals("Nap time", response);
}