2016-06-21 4 views
0

残りのテンプレートを使用するメソッドのJunitテストケースを作成しています。私が "https://dev.test.res.com/student?studId=100" に対するGET要求にmockitoフレームワークをJunitテストケースで不明なホストに対するGETリクエストでI/Oエラーが発生しました

import static org.junit.Assert.*; 

import java.io.IOException; 
import java.net.URI; 
import java.text.ParseException; 
import java.util.HashMap; 
import java.util.Map; 

import javax.xml.datatype.DatatypeConfigurationException; 

import junit.framework.Assert; 

import org.codehaus.jackson.JsonGenerationException; 
import org.codehaus.jackson.map.JsonMappingException; 
import org.codehaus.jackson.map.ObjectMapper; 
import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.mockito.InjectMocks; 
import org.mockito.Mock; 
import org.mockito.Mockito; 
import org.mockito.MockitoAnnotations; 
import org.mockito.runners.MockitoJUnitRunner; 
import org.springframework.boot.test.IntegrationTest; 
import org.springframework.http.HttpEntity; 
import org.springframework.http.HttpHeaders; 
import org.springframework.http.HttpMethod; 
import org.springframework.http.MediaType; 
import org.springframework.http.ResponseEntity; 
import org.springframework.http.client.ClientHttpRequest; 
import org.springframework.http.client.ClientHttpRequestFactory; 
import org.springframework.http.client.ClientHttpResponse; 
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; 
import org.springframework.mock.http.client.MockClientHttpRequest; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
import org.springframework.test.context.web.WebAppConfiguration; 
import org.springframework.test.web.client.MockRestServiceServer; 
import org.springframework.web.client.RestTemplate; 


@RunWith(MockitoJUnitRunner.class) 
@ContextConfiguration(classes = {ConfigData.class}) 
public class StudentClientTest { 

    @Mock 
    RestTemplate restTemplate; 

    @Mock 
    ClientHttpRequestFactory requestFactory; 

    @Mock 
    ClientHttpRequest request; 

    @Mock 
    MockClientHttpRequest mockClientHttpRequest; 

    @Mock 
    ClientHttpResponse response; 

    @Mock 
    ResponseEntity<String> responseEntory; 

    private MockRestServiceServer mockServer; 


    @InjectMocks 
    private StudentClient studentClient= new StudentClient(); 



    @Before 
    public void setUp() { 
     MockitoAnnotations.initMocks(this); 

    } 


    @Test 
    public void getStudentTest() throws Exception{ 



     String uri = "https://dev.test.res.com/student?"; 

     Integer studId=100; 
     uri=uri+studId; 

     Mockito.when(restTemplate.exchange(Mockito.contains(uri),Mockito.eq(HttpMethod.GET),Mockito.isA(HttpEntity.class), Mockito.eq(String.class),Mockito.isNotNull())).thenReturn(responseEntory); 
     assertNotNull(studentClient.getStudent(100)); 
} 


} 

I/Oエラーを使用して書かれた上記方法

public Student getStudent(Integer studId) throws Exception { 
     RestTemplate restTemplate = new RestTemplate(
         utility.setAuthenticationParameters(
           "test", "test")); 
     String url=https://dev.test.res.com/student; 
      HttpHeaders headers = new HttpHeaders(); 
     HttpEntity<String> entity = new HttpEntity<>(headers); 
     url =url+"?"+studId; 
     Map<String, String> params = new HashMap<>(); 
     params.put("studId", studId); 
     ResponseEntity<String> response = restTemplate.exchange(url, 
          HttpMethod.GET, entity, String.class, params); 
    } 

JUnitテストケース:ネストされた例外はjava.net.UnknownHostExceptionを発行ある:DEV。 test.res.com

私は、オブジェクトを嘲笑しているが、まだ例外で失敗している

+0

あなたはrestTemplateオブジェクトのモックましたか? Mockito.mock()メソッドを使用しましたか?あなたは嘲笑のためのコードを表示できますか? – Vijay

+0

@Vijay at Mock RestTemplate restTemplate; atBefore \t公共ボイドのsetUp(){ \t \t MockitoAnnotations.initMocks(この);} – shreekanth

+0

は、あなたの質問にコードを追加してくださいすることができますか?コメント欄ではあまり明確ではありません。 – Vijay

答えて

1

あなたのモックの問題はRestTemplateがプライベートフィールドインであるということですide StudentClientとStudentClientには、このプライベートフィールドRestTemplateのコンストラクター/セッターがありません。擬似注入は、現在黙って失敗します。

コンストラクタ/セッターでこのフィールドを公開すると、モックがStudentClientに正しく挿入されます。ここ

詳細:https://tedvinke.wordpress.com/2014/02/13/mockito-why-you-should-not-use-injectmocks-annotation-to-autowire-fields/

+0

restTemplateリファレンスが 'getStudent'メソッドで新しいインスタンスに割り当てられているので、私はモックオブジェクトの注入がここで重要ではないと思います。そしてモックのリファレンスは失われます。 –

関連する問題