相关疑难解决方法(0)

java.lang.AssertionError:未设置内容类型 - Spring Controller Junit Tests

我正在尝试在我的控制器上进行一些单元测试.无论我做什么,所有控制器测试都会返回

java.lang.AssertionError: Content type not set
Run Code Online (Sandbox Code Playgroud)

我正在测试这些方法返回json和xml数据.

以下是控制器的示例:

@Controller
@RequestMapping("/mypath")

public class MyController {

   @Autowired
   MyService myService;

   @RequestMapping(value="/schema", method = RequestMethod.GET)
   public ResponseEntity<MyObject> getSchema(HttpServletRequest request) {

       return new ResponseEntity<MyObject>(new MyObject(), HttpStatus.OK);

   }

}
Run Code Online (Sandbox Code Playgroud)

单元测试设置如下:

public class ControllerTest() { 

private static final String path = "/mypath/schema";
private static final String jsonPath = "$.myObject.val";
private static final String defaultVal = "HELLO";

MockMvc mockMvc;

@InjectMocks
MyController controller;

@Mock
MyService myService;

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);

    mockMvc = standaloneSetup(controller)
                .setMessageConverters(new MappingJackson2HttpMessageConverter(),
                        new Jaxb2RootElementHttpMessageConverter()).build(); …
Run Code Online (Sandbox Code Playgroud)

java junit spring spring-mvc mockmvc

8
推荐指数
1
解决办法
2万
查看次数

执行junit时如何将参数传递给Spring MVC控制器中的post方法?

我正在对我的 Spring MVC 控制器方法进行单元测试。下面是我尝试进行单元测试的方法,因为它在我启动服务器时工作正常。

每当我点击index页面时,它都会在浏览器上显示三个文本框,我在其中输入数据并按下提交按钮,然后调用以addNewServers正确的值进行。

现在我需要对同样的事情进行单元测试:

@RequestMapping(value = "/index", method = RequestMethod.GET)
public Map<String, String> addNewServer() {
    final Map<String, String> model = new LinkedHashMap<String, String>();
    return model;
}

@RequestMapping(value = "/index", method = RequestMethod.POST)
public Map<String, String> addNewServers(@RequestParam String[] servers, @RequestParam String[] address,
    @RequestParam String[] names) {     


}
Run Code Online (Sandbox Code Playgroud)

下面是我的junit类:

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 test04_newServers() throws …
Run Code Online (Sandbox Code Playgroud)

java junit spring spring-mvc spring-test-mvc

2
推荐指数
1
解决办法
6923
查看次数

标签 统计

java ×2

junit ×2

spring ×2

spring-mvc ×2

mockmvc ×1

spring-test-mvc ×1