您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)在SpringBoot中如何使用MongoDb實現(xiàn)單元測試功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
先引入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("================"); } } }
看完上述內(nèi)容,你們對在SpringBoot中如何使用MongoDb實現(xiàn)單元測試功能有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。