unitTest测试期间出错使用mock的Spring mvc控制器

时间:2014-06-26 06:19:09

标签: java spring junit mockito

这是我第一次使用mockito来测试我的弹簧mvc控制器。我发现错误我不知道为什么。我的项目使用的是spring3.2和Mybatis。 我的控制器代码如下:

@Controller
@RequestMapping(value = "/person")
public class MockTestController {

private AppUserModel appUserModel;


@Autowired
public MockTestController(AppUserModel appUserModel) {
    this.appUserModel = appUserModel;
}

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String getPerson(@PathVariable Long id, Model model) {
    model.addAttribute("person", appUserModel.getMemberById(id));
    return "test/person";
}
}

这是我的unitTest代码:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:/spring.xml",
    "file:web/WEB-INF/spring/operator-servlet.xml" })
public class MockControllerTest {

@Mock
private AppUserModel appUserModel;

@InjectMocks
private MockTestController mockController;

private MockMvc mockMvc;

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    mockMvc = MockMvcBuilders.standaloneSetup(mockController).build();
}

@Test
public void testGetPerson() throws Exception {
    when(appUserModel.getMemberById(1108L)).thenReturn(new Member());

    mockMvc.perform(get("/person/{id}", 1108L)).andExpect(status().isOk())
            .andExpect(view().name("test/person"))
            .andExpect(model().attribute("person", new Member()));

}
}

然后Junit显示错误日志:

  

由以下原因引起:org.springframework.beans.factory.BeanCreationException:使用名称' org.springframework.validation.beanvalidation.LocalValidatorFactoryBean创建bean时出错#0':init方法的调用失败;嵌套异常是javax.validation.ValidationException:无法实例化Configuration。       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1486)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)       at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:295)       在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)       在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)       在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)       at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)       在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)       在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)       at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:106)       at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:57)       在org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)       在org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248)       在org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:124)       at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148)       ......还有24个   引发者:javax.validation.ValidationException:无法实例化配置。       at javax.validation.Validation $ GenericBootstrapImpl.configure(Validation.java:272)       at org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.afterPropertiesSet(LocalValidatorFactoryBean.java:182)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1545)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1483)       ......还有39个   引起:java.lang.NullPointerException       在java.util.ResourceBundle.getBundle(ResourceBundle.java:960)       在org.hibernate.validator.engine.ResourceBundleMessageInterpolator.loadBundle(ResourceBundleMessageInterpolator.java:202)       at org.hibernate.validator.engine.ResourceBundleMessageInterpolator.getFileBasedResourceBundle(ResourceBundleMessageInterpolator.java:182)       在org.hibernate.validator.engine.ResourceBundleMessageInterpolator。(ResourceBundleMessageInterpolator.java:81)       在org.hibernate.validator.engine.ResourceBundleMessageInterpolator。(ResourceBundleMessageInterpolator.java:73)       在org.hibernate.validator.engine.ConfigurationImpl。(ConfigurationImpl.java:57)       在org.hibernate.validator.HibernateValidator.createGenericConfiguration(HibernateValidator.java:43)       at javax.validation.Validation $ GenericBootstrapImpl.configure(Validation.java:269)       ......还有42个

这个项目没有休眠。我需要帮助

0 个答案:

没有答案