Java Mockito可以';不要嘲笑方法的结果
我在测试(模拟方法的值)控制器中的delete方法时遇到问题。在普通模式下,它可以正常工作,但在我测试时不能 这是我的密码 我的控制器Java Mockito可以';不要嘲笑方法的结果,java,spring,spring-boot,testing,mocking,Java,Spring,Spring Boot,Testing,Mocking,我在测试(模拟方法的值)控制器中的delete方法时遇到问题。在普通模式下,它可以正常工作,但在我测试时不能 这是我的密码 我的控制器 @RestController @RequestMapping("/") public class MainController { @DeleteMapping(value = "/deletePost/{id}") public ResponseEntity<String> deletePost(
@RestController
@RequestMapping("/")
public class MainController {
@DeleteMapping(value = "/deletePost/{id}")
public ResponseEntity<String> deletePost(@PathVariable int id) throws SQLException {
boolean isRemoved = postsService.deletePost(connection, id);
if (!isRemoved)
return new ResponseEntity<>("Post with given id was not found", HttpStatus.NOT_FOUND);
else {
modifiedPostsService.insertModificationData(connection, new ModificationData(id, "deletion"));
return new ResponseEntity<>("Post with given id has been deleted.", HttpStatus.OK);
}
}
}
我的邮局
@Override
public boolean deletePost(Connection connection, int id) throws SQLException {
boolean isPostExists = isPostExist(connection, id);
PreparedStatement ps;
ps = connection.prepareStatement("delete from POSTS where ID = " + id);
ps.executeUpdate();
return isPostExists;
}
最后是我的测试
@WebMvcTest(MainController.class)
class MainControllerTests {
@Autowired
private MockMvc mockMvc;
@MockBean
private Connection connection;
@MockBean
private PostsService mockPostsService;
@Test
void testIfDeletePostUrlIsOk() throws Exception {
Mockito.when(mockPostsService.deletePost(connection, 1)).thenReturn(true);
mockMvc.perform(MockMvcRequestBuilders
.delete("/deletePost/1")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
}
testIfDeletePostUrlIsOk()
返回404而不是200(我猜模拟值-true不起作用,而是false)。为什么以及如何解决这个问题?当您使用:
Mockito.mock(...)
如果要在本地作用域上创建模拟对象,则必须将其注入SUT或使用:
@MockBean
将使spring的应用程序上下文可以访问您的对象,以便spring可以选择您的模拟
@SpringBootTest
@AutoConfigureMockMvc
public class TestingWebApplicationTest {
@Autowired
private MockMvc mockMvc;
@MockBean
Connection mockConnection;
@MockBean
PostsService mockPostsService;
@Test
void testIfDeletePostUrlIsOk() throws Exception {
Mockito.when(mockPostsService.deletePost(any(), anyInt())).thenReturn(true);
mockMvc.perform(MockMvcRequestBuilders
.delete("/deletePost/1")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
}
您需要将模拟注入控制器,注解@SpringBootTest和@MockBean将完成这项工作您刚刚创建了一个本地服务模拟,但控制器不会自动使用它。您需要以某种方式注入mock,但我不知道如何注入;(感谢您的帮助,但不幸的是它仍然不起作用,相同的错误,404而不是200。使用mockPostsService.deletePost(org.mockito.ArgumentMatchers.any(Connection.class),1)要匹配任何连接类型的输入,mockito使用对象引用来匹配您的方法调用和您的mock。您的意思是:mockito.when(mockPostsService.deletePost(org.mockito.ArgumentMatchers.any(Connection.class),1))。然后返回(true);?确切地说,您也可以导入静态org.mockito.ArgumentMatchers.org.mockito.ArgumentMatchers.anyInt()感谢您的帮助,但不幸的是,它仍然不起作用,相同的错误,404而不是200。您可以将调试放入控制器并检查您的代码是否返回404?您是否使用junit5?请复制您从testing classIn controller导入的内容。如果没有post,也可能是404代码状态(这是我的型号)在具有给定id的数据库中,否则有200个。我正在使用JUNIT 5.7.0。以下是我的一些导入(空间不足):导入org.junit.jupiter.api.Test;导入org.mockito.mockito;导入org.springframework.boot.Test.autoconfigure.web.servlet.WebMvcTest;导入org.springframework.boot.Test.mockito.MockBean;导入org.springframework.http.MediaType;导入org.springframework.Test.web.servlet.MockMvc;导入静态org.springframework.Test.web.servlet.result.MockMvcResultMatchers.status;是的,我知道,这就是为什么我要求您放置调试断点,并检查404是否从控制器方法或服务器应用程序socketo.when(mockPostsService.deletePost(any(),anyInt())返回。然后返回(true)
@SpringBootTest
@AutoConfigureMockMvc
public class TestingWebApplicationTest {
@Autowired
private MockMvc mockMvc;
@MockBean
Connection mockConnection;
@MockBean
PostsService mockPostsService;
@Test
void testIfDeletePostUrlIsOk() throws Exception {
Mockito.when(mockPostsService.deletePost(any(), anyInt())).thenReturn(true);
mockMvc.perform(MockMvcRequestBuilders
.delete("/deletePost/1")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
}