如何为使用mongodb进行数据持久化的spring boot rest控制器编写Junit测试用例
我正在尝试为我的spring boot控制器编写junit测试用例 我有两个API getUser和AddUser,这些API通过dao与mongotemplate通信,将数据放入MongoDB并从MongoDB获取数据 我有一个MongoConfiguration类(它有服务器地址、端口密码等)是通过属性文件@Value配置的,属性文件是通过@PropertySource mongo模板是使用从@Autowiredmongoconfig类读取的详细信息创建的 当我编写junit测试用例并进行测试时,它使用的是实际的mongoDB,它是通过标准属性文件为所有开发人员配置的,因此在我的测试中,我想使用某种嵌入式mongo来模拟mongoDB模板 那么,如何避免在junit测试用例中使用实际的mongodb呢 这是我下面的代码,它使用的是实际的数据库如何为使用mongodb进行数据持久化的spring boot rest控制器编写Junit测试用例,mongodb,junit,spring-boot-test,Mongodb,Junit,Spring Boot Test,我正在尝试为我的spring boot控制器编写junit测试用例 我有两个API getUser和AddUser,这些API通过dao与mongotemplate通信,将数据放入MongoDB并从MongoDB获取数据 我有一个MongoConfiguration类(它有服务器地址、端口密码等)是通过属性文件@Value配置的,属性文件是通过@PropertySource mongo模板是使用从@Autowiredmongoconfig类读取的详细信息创建的 当我编写junit测试用例并进行测
public class UserControllerTest extends AbstractTest {
@Override
@Before
public void setUp() {
super.setUp();
}
@Test
public void createProduct() throws Exception {
User user = new User();
user.setName("UnitTest");
String inputJson = super.mapToJson(user);
String uri = "/iot/user/createuser";
MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(uri)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(inputJson)).andReturn();
int status = mvcResult.getResponse().getStatus();
assertEquals(201, status);
}
}