我使用Spring MVC和Spring启动来编写Restful服务。这个代码通过postman工作正常。当我为控制器进行单元测试以接受post请求时,模拟的myService将始终初始化自身而不是返回由when ... thenReturn定义的模拟值...我使用verify(为MyService,倍(1))executeRule(任何(MyRule.class));并且它显示模拟未被使用。 我也尝试使用standaloneSetup for mockMoc,但它抱怨它无法找到路径“/ api / rule”的映射。 有人可以帮忙找出问题吗?
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class MyControllerTest {
@Mock
private MyService myService;
@InjectMocks
private MyController myRulesController;
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void controllerTest() throws Exception{
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
Long userId=(long)12345;
MyRule happyRule = MyRule.createHappyRule(......);
List<myEvent> mockEvents=new ArrayList<myEvent>();
myEvents.add(new MyEvent(......));
when(myService.executeRule(any(MyRule.class))).thenReturn(mockEvents);
String requestBody = ow.writeValueAsString(happyRule);
MvcResult result = mockMvc.perform(post("/api/rule").contentType(MediaType.APPLICATION_JSON)
.content(requestBody))
.andExpect(status().isOk())
.andExpect(
content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
verify(MyService,times(1)).executeRule(any(MyRule.class));
String jsonString = result.getResponse().getContentAsString();
}
}
下面是我的控制器类,其中MyService是一个接口。我已经实现了这个界面。
@RestController
@RequestMapping("/api/rule")
public class MyController {
@Autowired
private MyService myService;
@RequestMapping(method = RequestMethod.POST,consumes = "application/json",produces = "application/json")
public List<MyEvent> eventsForRule(@RequestBody MyRule myRule) {
return myService.executeRule(myRule);
}
}
答案 0 :(得分:1)
api是应用程序的上下文根吗?如果是这样,请从请求URI中删除上下文根并进行测试。传递上下文根将抛出404.如果您打算传递上下文根,请参考下面的测试用例。希望这会有所帮助。
@RunWith(MockitoJUnitRunner.class)
public class MyControllerTest {
@InjectMocks
private MyController myRulesController;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = standaloneSetup(myRulesController).build();
}
@Test
public void controllerTest() throws Exception{
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
MyController.User user = new MyController.User("test-user");
ow.writeValueAsString(user);
MvcResult result = mockMvc.perform(post("/api/rule").contentType(MediaType.APPLICATION_JSON).contextPath("/api")
.content(ow.writeValueAsString(user)))
.andExpect(status().isOk())
.andExpect(
content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
}
}
以下是控制器
/**
* Created by schinta6 on 4/26/16.
*/
@RestController
@RequestMapping("/api/rule")
public class MyController {
@RequestMapping(method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
public User eventsForRule(@RequestBody User payload) {
return new User("Test-user");
}
public static class User {
private String name;
public User(String name){
this.name = name;
}
}
}