- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
测试代码为:
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();
}
}
}
运行测试时我得到:
org.mockito.exceptions.misusing.UnfinishedStubbingException: Unfinished stubbing detected here: -> at HelloWorldByHourTest.testMain(HelloWorldByHourTest.java:35)
E.g. thenReturn() may be missing. Examples of correct stubbing: when(mock.isOk()).thenReturn(true); when(mock.isOk()).thenThrow(exception); doThrow(exception).when(mock).someVoidMethod(); Hints: 1. missing thenReturn() 2. you are trying to stub a final method, you naughty developer! 3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed
我的pom.xml是
<?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);
}
}
知道哪里出了问题吗?
最佳答案
问题是您在 when
和 thenReturn
中调用了 LocalDate 的静态方法
尝试这样的事情:
LocalTime time = LocalTime.of(15,0);
mockStatic(LocalTime.class);
when(LocalTime.now()).thenReturn(time);
关于java - 为什么我在这里得到 UnfinishedStubbingException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38465464/
我已经 mock 了 couchbase 类,例如 @Mock CouchBaseRepository couchBaseRepository; 并尝试使用它: boolean ifExists =
您好,我有这个 PowerMockito 测试,它会抛出 UnfinishedStubbingException @RunWith(PowerMockRunner.class) @PrepareFor
测试代码为: import java.io.IOException; import java.io.OutputStream; import java.net.InetSocketAddress; i
@MockBean private RestTemplateBuilder restTemplateBuilder; @MockBean private RestTemplate restTempla
我是 Mockito 的新手,我曾尝试调查此异常,但我还没有找到具体的答案。当我一起使用两个模拟时,它会发生在我的代码中,这意味着我通过一个模拟的构造函数,另一个模拟。像这样: ... Operati
我已经阅读了很多有关此错误的内容,但找不到解决方案。 这是我的示例代码,您可以复制它并启动它以重现错误。 主类: package example; import org.junit.runner.Ru
我想在我的发票类中 stub 方法generateReferenceNumber(): public class Invoice { private String id; privat
我正在尝试模拟一个客户端响应不佳的服务进行测试。我仍然在 min eclass 中收到 UnfinishedStubbingException 而不是 ClientResponseFailure,而且
当另一个方法中的 spyAnotherService.getUrl(ID) 时,我试图进行 spy /模拟并返回一个虚拟 Url,该方法是 myService.deleteSomething(name
您好,我正在尝试使用 Mockito 测试方法并收到 UnfinishedStubbingException。我是 Mockito 的新手,不确定我是否在做一些了不起的事情:) 这些是我的 stub
我想测试一个内部使用静态方法的方法,这就是我使用 PowerMockito 的原因。我想模拟对 Request.Get 的调用来自 Http 客户端 Fluent API。我设置了一个简单的测试,但出
好吧,我显然不太明白 doReturn(...).when(...) 和 when(...).thenReturn(...) 之间的区别。 问题是,当我上课时,我用 @Mock 注释模拟,并在我想测试
我可以将模拟对象作为参数传递给 thenThrow() 方法吗?我有这样的东西: public class MyException extends Exception { public MyE
尝试让 Mockito 和 PowerMock 正常运行,但在尝试运行此代码时遇到 UnfinishedStubbingException: @RunWith(PowerMockRunner.clas
我正在尝试使用 Mockito 的 Spy 部分模拟服务,覆盖一个方法以使其返回一致的数据以进行测试,但说 spy 无缘无故抛出 UnfinishedStubbingException。 这是我的测试
运行以下代码时,我收到错误消息 Unfinished Stubbing here detected: import static org.mockito.Mockito.mock; import st
我正在尝试verify在FileChooser上调用方法。 我在Groovy中编码,这似乎是问题所在。 我正在使用“孵化”的Mockito功能,该功能甚至可以模拟final类。 代码是: Fi
我有一个 LoggerInterceptor 类,其参数为 InspirationContext。对于这门课,我试图编写一个单元测试,但我被困在第一行: public class LoggerInte
以下代码导致 UnfinishedStubbingException PowerMockito.doNothing().when(widgetHelper).invokeAuditService(Ma
我写了下面的代码: @RunWith(PowerMockRunner.class) @PrepareForTest(Integer.class) public class TestClass{
我是一名优秀的程序员,十分优秀!