在SpringBoot中如何使用MongoDb实现单元测试功能

  介绍

今天就跟大家聊聊有关在SpringBoot中如何使用MongoDb实现单元测试功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

先引入pom依赖

& lt; dependency>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-starter-test   & lt; scope> test   & lt;/dependency>

代码1

 @RunWith (SpringRunner.class)
  @SpringBootTest
  @ transactional
  公开课ApplicationTests {
  
  @ autowired
  私人RedisService RedisService;
  
  @ autowired
  私人UserDao UserDao;
  
  @ autowired
  私人JdbcTemplate JdbcTemplate;
  
  @ autowired
  私人MongoTemplate MongoTemplate;
  
  @Test
  公共空间hello () {
  System.out.println(“你好world");
  redisService.setObjectJSON (“test",“1“);
  System.out.println(“使用了redisservice");
  用户用户=userDao.find (“id=, # 63;“, 1 l)当代();
  System.out.println(“打印出了一个用户名“+ user.userName);
  List比;queryForList=jdbcTemplate.queryForList(“选择id, user_name从c_user id<2“);
  (Map<字符串,Object>地图:queryForList) {
  对象对象=map.get (“id");
  System.out.println(对象);
  对象object2=map.get (“user_name");
  System.out.println (object2);
  }
  MongoUser mongoUser2=new MongoUser ();
  mongoUser2.setId (“1“);
  mongoUser2.setName(“小明“);
  mongoTemplate.insert (mongoUser2);
  ListfindAll=mongoTemplate.findAll (MongoUser.class);
  (MongoUser MongoUser: findAll) {
  System.out.println (“id=? mongoUser.getId ());
  System.out.println (“id=? mongoUser.getName ());
  }
  }
  
  公共静态void main (String [] args) {
  Object> HashMap<字符串;hashMap=new HashMap<字符串,Object> ();
  hashMap.put (“a", 1);
  hashMap.put (“b" 2);
  Set键盘=hashMap.keySet ();
  字符串(字符串:键盘){
  System.out.println(字符串);
  对象对象=hashMap.get(字符串);
  System.out.println(对象);
  System.out.println (“================?;
  }
  }
  
  }

看完上述内容,你们对在SpringBoot中如何使用MongoDb实现单元测试功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。

在SpringBoot中如何使用MongoDb实现单元测试功能