2016-07-19 9 views
2

テストコードは次のとおりです。ここでUnfinishedStubbingExceptionを取得するのはなぜですか?

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(); 
     } 
    } 

} 

私が手にテスト実行する場合:

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); 
    } 
} 

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> 

テストコードがあります

アイデア何が間違っている?

+0

はあなたが右のコードを掲載している:あなたが何をしたいかに応じて、

、私はこの問題を回避するには、静的メソッドをあざける前の線に沿って何かをLocalTimeのモックを作成することですと仮定しますか?あなたの例外は 'HelloWorldByHourTest'にあり、投稿したものが' HelloWorldByHour'から来ているようです。 – Sam

+0

ええ、間違ったコードを投稿しました。テストコードを追加しました。 – traveh

答えて

1

問題は、あなたがそのような何かを試してみて、あなたのwhenthenReturn

にLOCALDATE上の静的メソッドを呼び出すことです。この呼び出しwhen(LocalTime.now()).thenReturn(LocalTime.of(15, 0));

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

これを試してみましたが、これはうまくいくようですが、現在はjava.lang.VerifyError:オペランドスタックの型が無効です 例外の詳細: 場所: com/sun/net/httpserver/spi/HttpServerProvider $ 1。 run()Ljava/lang/Object; (現在のフレーム、スタック[0])は 'com/sun/net/httpserver/spi/HttpServerProvider'に割り当てられません。 – traveh

1

あなたは一種のフィッティングの下だ:

3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed.

基本的には、LocalTimeクラスを静的に嘲笑しましたが、あなたもインフォームされています静的LocalTime.of(15, 0)メソッドを呼び出して、LocalTime.now()の動作を定義します。

@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); 
} 
関連する問題