2014-07-03 70 views
0

我試圖用Thymeleaf構建一些使用Spring 4的基本Web應用程序,並且我有測試問題。控制器彈簧的單元測試4 MockMvc缺少代碼屬性

首先我的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") 
} 

接着,我的控制器類:

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"; 

} 

} 

和測試: 包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)); 
} 
} 

而且有我所得到的,當我運行這個測試:

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) 

你有什麼想法?

回答

0

一些測試範圍的依賴需要增加,這將有助於你在開發和測試

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' 

,你可以擺脫

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

,因爲它下載了很多的依賴關係,這將有助於開發時間,但不在測試時間