详解在SpringBoot中使用MongoDb做单元测试的代码
程序员文章站
2022-04-26 15:06:31
先评价: 功能倒是不错,但是总觉得耽误时间先引入pom依赖 org.springframework.boot
先评价: 功能倒是不错,但是总觉得耽误时间
先引入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单元测试内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 为了向女友炫耀 程序员魔改《合成大西瓜》:暴力吃瓜
下一篇: 洗发用品的利润高吗?洗发用品怎么进货?