【问题标题】:java.lang.AssertionError: Content type not set while junit Spring MVC Controller?java.lang.AssertionError:junit Spring MVC 控制器时未设置内容类型?
【发布时间】:2014-04-06 15:36:56
【问题描述】:

我正在使用 JUnit 来测试我的 Spring MVC 控制器。下面是我的方法,它返回一个index.jsp 页面并在屏幕上显示Hello World -

@RequestMapping(value = "index", method = RequestMethod.GET)
public HashMap<String, String> handleRequest() {
    HashMap<String, String> model = new HashMap<String, String>();

    String name = "Hello World";
    model.put("greeting", name);

    return model;
}

下面是我对上述方法的 JUnit 测试:

public class ControllerTest {

    private MockMvc mockMvc;

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

        this.mockMvc = standaloneSetup(new Controller()).setViewResolvers(viewResolver).build();
    }

    @Test
    public void test01_Index() throws Exception {

    mockMvc.perform(get("/index")).andExpect(status().isOk()).andExpect(content().contentType("application/json"))
        .andExpect(jsonPath("$.greeting").value("Hello World"));

    }
}

当我调试 junit 时,它运行良好,但是当我以 run as junit 运行 junit 时,它给了我这个错误 -

MockMvc : Circular view path [view]: would dispatch back to the current handler URL [/view] again

为了解决这个问题,我关注了这个tutorial,之后我得到了这个异常 -

java.lang.AssertionError: Content type not set
    at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:39)
    at org.springframework.test.util.AssertionErrors.assertTrue(AssertionErrors.java:72)
    at org.springframework.test.web.servlet.result.ContentResultMatchers$1.match(ContentResultMatchers.java:75)
    at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:141)
    at com.ebay.personalization.bullseye.zookeeper.test.ZookeeperControllerTest.test01_Index(ZookeeperControllerTest.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:76)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:602)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    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)

我在这里做错了什么?在 .jsp 页面上,它显示 Hello World..

我正在跟进我之前的问题here

【问题讨论】:

    标签: java spring spring-mvc junit spring-test-mvc


    【解决方案1】:

    您还应该检查您的请求映射:

    get("/...") 匹配 @RequestMapping 吗?

    如果从不调用handleRequest,你会得到同样的错误Content type not set

    【讨论】:

    • 你说的完全正确!我刚刚更改了代码中的映射,错过了在我的 Junit 中做同样的事情!谢谢!
    【解决方案2】:

    我发现请求映射方法存在一些问题:

    理想情况下,您的方法上应该有一个@ResponseBody 注释,以指示返回的内容将被流出:

    @RequestMapping(value = "index", method = RequestMethod.GET)
    @ResponseBody
    public HashMap<String, String> handleRequest() {
        HashMap<String, String> model = new HashMap<String, String>();
        String name = "Hello World";
        model.put("greeting", name);
    
        return model;
    }
    

    现在完成此操作后,您的测试将如下所示:

    mockMvc.perform(get("/index").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith("application/json"))
            .andExpect(jsonPath("$.greeting").value("Hello World"));
    

    在您的情况下,正在发生的事情是 Spring 试图找出视图名称并将视图名称获取为 index,这又是控制器路径,因此您看到的是圆形视图路径错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-21
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      • 1970-01-01
      • 2011-04-06
      • 2015-08-13
      相关资源
      最近更新 更多