java.lang.AssertionError:在junit Spring MVC控制器时未设置内容类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22183178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:10:41  来源:igfitidea点击:

java.lang.AssertionError: Content type not set while junit Spring MVC Controller?

javaspringspring-mvcjunitspring-test-mvc

提问by AKIWEB

I am using JUnit to test my Spring MVC controller. Below is my method which returns a index.jsppage and shows Hello Worldon the screen -

我正在使用 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;
}

And below is my JUnit test for the above method:

下面是我对上述方法的 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"));

    }
}

Above junit was working fine when I debugged it but when I ran the junit as run as junit, it gave me this error -

当我调试 junit 时,上面的 junit 工作正常,但是当我将 junit 作为运行时run as junit,它给了我这个错误 -

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

So to fix that I followed this tutorialand after that I am getting this exception -

所以为了解决这个问题,我遵循了本教程,之后我收到了这个异常 -

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.match(ContentResultMatchers.java:75)
    at org.springframework.test.web.servlet.MockMvc.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.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.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access
@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;
}
0(ParentRunner.java:42) at org.junit.runners.ParentRunner.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)

What am I doing wrong here? On the .jsp page it shows Hello World..

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

I am following up on my previous question here

我在这里跟进我之前的问题

采纳答案by Biju Kunjummen

I see a couple of issues with the request mapped method:

我看到请求映射方法有几个问题:

Ideally, you should have a @ResponseBodyannotation on your method, to indicate that the returned content is to be streamed out:

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

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

Now with this done, your test can look like this:

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

##代码##

In your case, what is happening is Spring is trying to figure out the view name and is getting the view name as index, which is again the controller path, thus the circular view path error that you are seeing.

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

回答by Matthias M

You should also check your request mappings:

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

Does get("/...") match @RequestMapping ?

get("/...") 是否匹配 @RequestMapping ?

If the handleRequest is never called, you will get the same error Content type not set.

如果从不调用 handleRequest,您将收到相同的错误Content type not set