使用spring-test-mvc进行弹簧控制器单元测试失败

时间:2015-06-21 14:22:42

标签: java spring unit-testing spring-mvc

我正在学习使用EasyMock和Spring测试框架的弹簧控制器的单元测试。我为我的控制器做了一个简单的单元测试。

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.firstmav.domain.Employee;
import com.firstmav.service.EmployeeService;

@Controller
public class DataController {

    @Autowired
    EmployeeService employeeservice;

    @RequestMapping("form")
    public ModelAndView getform(@ModelAttribute Employee employee){
        return new ModelAndView("form");
    }

    @RequestMapping("reguser")
    public ModelAndView registeruser(@ModelAttribute Employee employee){
        employeeservice.insertRow(employee);
        return new ModelAndView("redirect:list");
    }

    @RequestMapping("list")
    public ModelAndView getlist(){
        List<Employee> employeelist = employeeservice.getList();
        return new ModelAndView("list", "employeeList", employeelist);
    }

    @RequestMapping("delete")
    public ModelAndView deleteitem(@RequestParam int id){
        employeeservice.deleteRow(id);
        return new ModelAndView("redirect:list");
    }

    @RequestMapping("edit")
    public ModelAndView edititem(@ModelAttribute Employee employee, @RequestParam int id){
        Employee employeeObject = employeeservice.getRowByID(id);
        return new ModelAndView("edit", "employeeObject", employeeObject);
    }

    @RequestMapping("update")
    public ModelAndView updaterow(@ModelAttribute Employee employee){
        employeeservice.updateRow(employee);
        return new ModelAndView("redirect:list");
    }
}

我在这里遇到了失败的测试用例。

import org.junit.Before;
import org.junit.Test;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import com.firstmav.controller.DataController;


public class DataControllerTest {
private MockMvc mockmvc;

@Before
public void setup(){
     InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
     viewResolver.setPrefix("/WEB-INF/pages/");
     viewResolver.setSuffix(".jsp");

     mockmvc = MockMvcBuilders.standaloneSetup(new DataController()).setViewResolvers(viewResolver).build();
}
@Test
public void main() throws Exception{
    mockmvc.perform(get("/form")).andExpect(status().isOk()).andExpect(view().name("form"));
}
}

我在测试用例中包含了控制器导入,但我总是得到noclassdeffound异常。

    java.lang.NoClassDefFoundError: javax/servlet/ServletException
    at org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup(MockMvcBuilders.java:71)
    at com.ada.test.DataControllerTest.setup(DataControllerTest.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 25 more

我不明白我在哪里弄错了。任何人都可以帮助我或指出正确的方向吗?

1 个答案:

答案 0 :(得分:1)

这个问题与你的代码无关。你错过了包含javax.servlet.ServletException的库(jar)。在运行时你得到了这个异常。如果有的话,请检查类路径该位置的servlet-api.jar。 虽然将servlet-api.jar添加到类路径位置可以解决问题,但如果需要,还可以检查具有此类here

的jar