2016-08-29 12 views
21

इंजेक्ट करने में असमर्थ है मैं वसंत बूट 1.4.0.RELEASE का उपयोग कर रहा हूं। मैं अपने नियंत्रक वर्ग के लिए परीक्षण लिख रहा हूं। मुझे निम्नलिखित अपवाद मिलता है।वसंत बूट परीक्षण TestRestTemplate और MockMvc

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.concur.cognos.authentication.service.ServiceControllerITTest': Unsatisfied dependency expressed through field 'restTemplate': No qualifying bean of type [org.springframework.boot.test.web.client.TestRestTemplate] found for dependency [org.springframework.boot.test.web.client.TestRestTemplate]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.boot.test.web.client.TestRestTemplate] found for dependency [org.springframework.boot.test.web.client.TestRestTemplate]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 

यहाँ

public class ServiceControllerITTest extends ApplicationTests { 

    @Autowired 
    private TestRestTemplate restTemplate; 

    @Autowired 
    private MockMvc mvc; 

    @Test 
    public void exampleTest() throws Exception { 
     // test 
    } 
} 

ApplicationTests.java

@RunWith(SpringRunner.class) 
@SpringBootTest 
@WebAppConfiguration 
//@DirtiesContext 
public class ApplicationTests { 

    @Autowired 
    Environment env; 

    @Test 
    public void contextLoads() { 

    } 

} 

उत्तर

40

TestRestTemplate केवल अपने परीक्षण वर्ग है ऑटो कॉन्फ़िगर किया गया जब @SpringBootTest एक webEnvironment मतलब है कि यह वेब कंटेनर शुरू होता है और सुनता है के साथ विन्यस्त किया गया है HTTP अनुरोधों के लिए। उदाहरण के लिए:

@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT) 
+0

मैंने जोड़ा। अब मैं करने के 'java.lang.IllegalStateException: org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext' –

+1

में ApplicationContext \t लोड करने में असफल तुम अब भी' TestRestTemplate' और 'MockMvc' autowire करने की कोशिश कर रहे हैं? ऐसा करने से समझ में नहीं आता है। आपको एक या दूसरे का उपयोग करना चाहिए। –

+0

यह या तो मदद नहीं करता है। मैंने 'मॉकएमवीसी' पर टिप्पणी की और केवल 'testRestTemplate' को स्वचालित किया। अभी भी मुझे उपरोक्त टिप्पणी –

3

इसके साथ काम करने के लिए, बहिष्कृत TestRestTemplate का उपयोग न करें।

पदावनत:

import org.springframework.boot.test.TestRestTemplate; 

सही:

import org.springframework.boot.test.web.client.TestRestTemplate; 

तो फिर तुम @Autowired एनोटेशन अपनी कक्षा में उपयोग कर सकते हैं:

@Autowired 
private TestRestTemplate restTemplate; 

और उपयोग नहीं करते हैं:

@Autowired 
private MockMvc mvc; 

दोनों एक साथ काम नहीं करते हैं। में एक गैर @WebMvcTest (जैसे SpringBootTest) @AutoConfigureMockMvc साथ यह व्याख्या द्वारा

तुम भी ऑटो कॉन्फ़िगर कर सकते हैं MockMvc:

संबंधित मुद्दे