title: Why do I get UnfinishedStubbingException here?
body: <p>The test code is:</p>
  
  <pre><code>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) &amp;&amp; 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();
          }
      }
  
  }
  </code></pre>
  
  <p>When running the test I get:</p>
  
  <blockquote>
    <p>org.mockito.exceptions.misusing.UnfinishedStubbingException: 
    Unfinished stubbing detected here:
    -> at HelloWorldByHourTest.testMain(HelloWorldByHourTest.java:35)</p>
    
    <p>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</p>
  </blockquote>
  
  <p>my pom.xml is</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;server&lt;/groupId&gt;
      &lt;artifactId&gt;server&lt;/artifactId&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;(whatever version is current)&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;!-- or whatever version you use --&gt;
                      &lt;source&gt;1.8&lt;/source&gt;
                      &lt;target&gt;1.8&lt;/target&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;version&gt;4.12&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.powermock&lt;/groupId&gt;
              &lt;artifactId&gt;powermock-api-mockito&lt;/artifactId&gt;
              &lt;version&gt;1.6.5&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.powermock&lt;/groupId&gt;
              &lt;artifactId&gt;powermock-core&lt;/artifactId&gt;
              &lt;version&gt;1.6.5&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.powermock&lt;/groupId&gt;
              &lt;artifactId&gt;powermock-module-junit4&lt;/artifactId&gt;
              &lt;version&gt;1.6.5&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.powermock&lt;/groupId&gt;
              &lt;artifactId&gt;powermock-module-junit4&lt;/artifactId&gt;
              &lt;version&gt;RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;version&gt;4.11&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>The test code is:</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>Any idea what's wrong?</p>