简体   繁体   English

控制器Spring 4 MockMvc缺少代码属性的单元测试

[英]Unit Test of Controller Spring 4 MockMvc Absent Code Attribute

I've tried to build some basic web application using Spring 4 with Thymeleaf and I have problem with testing. 我尝试使用带有Thymeleaf的Spring 4构建一些基本的Web应用程序,但是我在测试方面遇到了问题。

First my build.gradle dependencies: 首先我的build.gradle依赖项:

dependencies {
    compile("org.springframework:spring-webmvc:4.0.5.RELEASE")
    compile("org.thymeleaf:thymeleaf-spring4:2.1.3.RELEASE")
    compile 'org.springframework:spring-test:4.0.5.RELEASE'
    providedCompile("javax:javaee-web-api:6.0")
    testCompile("junit:junit")
    testCompile ("org.mockito:mockito-all:1.9.5")
}

Next, my controller class: 接下来,我的控制器类:

package pl.com.tegess.RetrospectionSystem;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class GreetingController {

@RequestMapping("/greeting")
public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
    model.addAttribute("name", name);
    return "greeting";

}

}

and the test: package pl.com.tegess.RetrospectionSystem; 和测试:包pl.com.tegess.RetrospectionSystem;

import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import pl.com.tegess.RetrospectionSystem.configuration.WebAppConfiguration;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;

public class GreetingControllerTest {

private static final String greetingView = "/WEB-INF/templates/greeting.html";


MockMvc mockMvc;

@InjectMocks
GreetingController controller;

@Before
public void setup(){
    MockitoAnnotations.initMocks(this);
    mockMvc = standaloneSetup(controller).setViewResolvers(viewResolver()).build();
}

private InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/templates/");
    viewResolver.setSuffix(".html");
    return viewResolver;
}

@Test
public void testGreeting() throws Exception {
    mockMvc.perform(get("/greeting")).andExpect(forwardedUrl(greetingView));
}
}

And there is what i get when i run this test: 当我运行此测试时,我得到的是:

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletException
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup(MockMvcBuilders.java:71)
at pl.com.tegess.RetrospectionSystem.GreetingControllerTest.setup(GreetingControllerTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

Have you any idea? 你有什么主意吗

Some of the test scope dependencies need to be added that will assist you in both development and testing 需要添加一些测试范围依赖项,这将有助于您进行开发和测试

testCompile 'javax.el:javax.el-api:3.0.0'   
testCompile 'org.glassfish.web:el-impl:2.2'
providedCompile 'javax.servlet:javax.servlet-api:3.0.1'  

and you can get rid of the 你可以摆脱

providedCompile("javax:javaee-web-api:6.0")

as it downloads a lot of dependencies that would help at development time but not at testing time 因为它下载了很多依赖关系,这些依赖关系在开发时会有所帮助,而在测试时却无济于事

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Mockito和MockMvc的方法中的缺少代码属性 - Absent Code attribute in method with Mockito and MockMvc Spring 过滤器链未应用于单元测试 MockMvc - Spring filter chain is not applied on unit test MockMvc Junit测试Spring控制器MockMvc @ContextConfiguration - Junit test spring controller mockMvc @ContextConfiguration Spring Controller ModelAndView 测试 MockMvc 空响应 - Spring Controller ModelAndView test MockMvc empty Response 单元测试 - 使用 MockMvc 使用 @RequestHeader 和 HashMap 测试 controller - Unit Test - Using MockMvc to test the controller with @RequestHeader with HashMap 为什么在Spring MockMVC单元测试中不传播身份验证? - Why is authentication not being propagated in spring mockmvc unit test? Spring Boot 单元测试 MockMVC 抛出 BadRequest 即使存在 API - Spring Boot Unit Test MockMVC throws BadRequest eventhough there exists API for it Struts2单元测试给出错误:类文件javax / servlet / ServletException中不是本机或抽象的方法中的缺少Code属性 - Struts2 Unit test gives the error: Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletException Spring MockMvc - 如何测试REST控制器的删除请求? - Spring MockMvc - How to test delete request of REST controller? 带有 Map 参数的控制器上的 Spring Boot 补丁请求。 用 mockmvc 测试 - Spring boot patch request on controller with Map parameter. Test with mockmvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM