2014-10-10 64 views
0

我正在使用一個開源項目petclininc,它遵循Spring MVC模式使用spring mocking服務編寫Junit測試用例。面向控制器的Junit正在編譯,但顯示錯誤

我開始用一個獨立的控制器

@Controller 
@SessionAttributes("visit") 
public class VisitController { 

    private final ClinicService clinicService; 


    @Autowired 
    public VisitController(ClinicService clinicService) { 
     this.clinicService = clinicService; 
    } 

    @InitBinder 
    public void setAllowedFields(WebDataBinder dataBinder) { 
     dataBinder.setDisallowedFields("id"); 
    } 

    @RequestMapping(value = "/owners/*/pets/{petId}/visits/new", method = RequestMethod.GET) 
    public String initNewVisitForm(@PathVariable("petId") int petId, Map<String, Object> model) { 
     Pet pet = this.clinicService.findPetById(petId); 
     Visit visit = new Visit(); 
     pet.addVisit(visit); 
     model.put("visit", visit); 
     return "pets/createOrUpdateVisitForm"; 
    } 

    @RequestMapping(value = "/owners/{ownerId}/pets/{petId}/visits/new", method = RequestMethod.POST) 
    public String processNewVisitForm(@Valid Visit visit, BindingResult result, SessionStatus status) { 
     if (result.hasErrors()) { 
      return "pets/createOrUpdateVisitForm"; 
     } else { 
      this.clinicService.saveVisit(visit); 
      status.setComplete(); 
      return "redirect:/owners/{ownerId}"; 
     } 
    } 

    @RequestMapping(value = "/owners/*/pets/{petId}/visits", method = RequestMethod.GET) 
    public ModelAndView showVisits(@PathVariable int petId) { 
     ModelAndView mav = new ModelAndView("visitList"); 
     mav.addObject("visits", this.clinicService.findPetById(petId).getVisits()); 
     return mav; 
    } 

} 

和我的JUnit測試案例:

package org.springframework.samples.petclinic.web; 

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; 
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; 
import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.mockito.InjectMocks; 
import org.mockito.Mock; 
import org.mockito.MockitoAnnotations; 
import org.springframework.samples.petclinic.service.ClinicService; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
import org.springframework.test.web.servlet.MockMvc; 
import org.springframework.test.web.servlet.setup.MockMvcBuilders; 


@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration("mvc-core-config.xml") 

public class VisitControllerTest { 


    private MockMvc mockMvc; 

    @Mock 
    private ClinicService clinicService; 

    @InjectMocks  
    private VisitController visitController; 



    @Before 
    public void setUp() throws Exception{ 

     MockitoAnnotations.initMocks(this); 

     this.mockMvc = MockMvcBuilders.standaloneSetup(new VetController(clinicService)).build(); 

    } 


    @Test 
    public void processNewVisitFormTest() throws Exception{ 



      this.mockMvc.perform(post("/owners/1/pets/1/visits/new")) 
        .andExpect(status().isOk()) 
        .andExpect(view() 
        .name("redirect:/owners/1")); 
    } 


} 

當我想這種測試情況下,我得到 我改成了春潤/ mvc-core-config.xml和

fullstacktrace:INFO XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [spring/mvc-core-config.xml] 
INFO XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [spring/mvc-view-config.xml] 
INFO GenericApplicationContext - Refreshing [email protected]6b998f: startup date [Fri Oct 10 13:42:21 IST 2014]; root of context hierarchy 
ERROR TestContextManager - Caught exception while allowing TestExecutionListener [org.springframewor[email protected]28a6683d] to prepare test instance [[email protected]0591a4] 
java.lang.IllegalStateException: Failed to load ApplicationContext 
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99) ~[spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101) ~[spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109) ~[spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75) ~[spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:331) ~[spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:213) ~[spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:290) ~[spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) ~[junit-4.11.jar:na] 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:292) ~[spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233) ~[spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87) ~[spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) ~[junit-4.11.jar:na] 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) ~[junit-4.11.jar:na] 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) ~[junit-4.11.jar:na] 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) ~[junit-4.11.jar:na] 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) ~[junit-4.11.jar:na] 
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) ~[spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) ~[spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309) ~[junit-4.11.jar:na] 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176) ~[spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) ~[.cp/:na] 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) ~[.cp/:na] 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) ~[.cp/:na] 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) ~[.cp/:na] 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) ~[.cp/:na] 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) ~[.cp/:na] 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'conversionService' defined in class path resource [spring/mvc-core-config.xml]: Cannot create inner bean 'org.springframework.samples.petclinic.web.PetTypeFormatter#7f95a585' of type [org.springframework.samples.petclinic.web.PetTypeFormatter] while setting bean property 'formatters' with key [0]; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.samples.petclinic.web.PetTypeFormatter#7f95a585' defined in class path resource [spring/mvc-core-config.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.samples.petclinic.service.ClinicService]: : No qualifying bean of type [org.springframework.samples.petclinic.service.ClinicService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.samples.petclinic.service.ClinicService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:290) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:122) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedSet(BeanDefinitionValueResolver.java:371) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:161) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1456) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:744) ~[spring-context-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) ~[spring-context-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:125) ~[spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60) ~[spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100) ~[spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:250) ~[spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64) ~[spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91) ~[spring-test-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    ... 25 common frames omitted 
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.samples.petclinic.web.PetTypeFormatter#7f95a585' defined in class path resource [spring/mvc-core-config.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.samples.petclinic.service.ClinicService]: : No qualifying bean of type [org.springframework.samples.petclinic.service.ClinicService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.samples.petclinic.service.ClinicService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:747) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1114) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1017) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:276) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    ... 44 common frames omitted 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.samples.petclinic.service.ClinicService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:811) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:739) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE] 
    ... 50 common frames omitted 
+0

*在@ @ RequestMapping中如何處理? – 2014-10-10 07:11:30

+0

標題有誤導性。它編譯但不起作用。 – Victor 2014-10-10 07:12:17

+0

顯示完整的堆棧跟蹤。 – axtavt 2014-10-10 07:18:39

回答

0

幾乎在最後一行stacktrace Caused by: java.io.FileNotFoundException: class path resource [mvc-core-config.xml] cannot be opened because it does not exist at ..說,那春天找不到你的配置文件。 您的spring配置位於「src/main/resources /」或「src/test/resources」下(如果使用maven)?然後嘗試使用此配置您的測試:@ContextConfiguration("classpath:mvc-core-config.xml")

+0

我已經試過了,它的工作方式也不太好./spring-petclinic/src/main/resources/spring/mvc-core-config.xml是mvc-core-config.xml的實際路徑。我是否正確地嘲弄? – user2900150 2014-10-10 08:10:35

+0

如果您編輯原始帖子,請將其更改爲spring/mvc-core-config.xml和fullstacktrace: – user2900150 2014-10-10 08:14:49

+0

,請在下次提及它。我在想你的堆棧跟蹤。 它看起來像clinicService不能作爲依賴項被發現。你必須在你的mvc配置中定義它,或者導入service-config。 – 2014-10-10 09:21:27

相關問題