java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot MongoDb单元测试

详解在SpringBoot中使用MongoDb做单元测试的代码

作者:爱吃早餐的程序员

这篇文章主要介绍了详解在SpringBoot中使用MongoDb做单元测试的代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

先评价: 功能倒是不错,但是总觉得耽误时间

先引入pom依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
 </dependency>

代码1

public class MongoUser implements Serializable{
 private static final long serialVersionUID = 1L;
 @Id
   private String id;
   private String name;
   private String age;
 
   public String getId() {
     return id;
   }
 
   public void setId(String id) {
     this.id = id;
   }
 
   public String getName() {
     return name;
   }
 
   public void setName(String name) {
     this.name = name;
   }
 
   public String getAge() {
     return age;
   }
 
   public void setAge(String age) {
     this.age = age;
   }
 
   @Override
   public boolean equals(Object o) {
     if (this == o) return true;
     if (o == null || getClass() != o.getClass()) return false;
     MongoUser user = (MongoUser) o;
     return Objects.equals(id, user.id) &&
         Objects.equals(name, user.name) &&
         Objects.equals(age, user.age);
   }
 
   @Override
   public int hashCode() {
     return Objects.hash(id, name, age);
   }
}

代码2

@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class ApplicationTests {
 
 @Autowired
 private RedisService redisService;
 
 @Autowired
 private UserDao userDao;
 
 @Autowired
 private JdbcTemplate jdbcTemplate;
 
 @Autowired
 private MongoTemplate mongoTemplate;
 
 @Test
  public void hello() {
    System.out.println("hello world");
    redisService.setObjectJSON("test", "1");
    System.out.println("使用了redisservice");
    User user = userDao.find("id=?", 1l).first();
    System.out.println("打印出了一个用户名"+user.userName);
    List<Map<String,Object>> queryForList = jdbcTemplate.queryForList("select id,user_name from c_user where id<2");
    for (Map<String, Object> map : queryForList) {
  Object object = map.get("id");
  System.out.println(object);
  Object object2 = map.get("user_name");
  System.out.println(object2);
 }
    MongoUser mongoUser2 = new MongoUser();
    mongoUser2.setId("1");
    mongoUser2.setName("小明");
 mongoTemplate.insert(mongoUser2);
    List<MongoUser> findAll = mongoTemplate.findAll(MongoUser.class);
    for (MongoUser mongoUser : findAll) {
  System.out.println("id="+mongoUser.getId());
  System.out.println("id="+mongoUser.getName());
 }
  }
 
 public static void main(String[] args) {
 HashMap<String,Object> hashMap = new HashMap<String, Object>();
 hashMap.put("a", 1);
 hashMap.put("b", 2);
 Set<String> keySet = hashMap.keySet();
 for (String string : keySet) {
  System.out.println(string);
  Object object = hashMap.get(string);
  System.out.println(object);
  System.out.println("================");
 }
 }
 
}

到此这篇关于详解在SpringBoot中使用MongoDb做单元测试的代码的文章就介绍到这了,更多相关SpringBoot MongoDb单元测试内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文