在数字化转型中,选择合适的跨平台开发框架不仅能提高效率,还有助于确保数据安全与合规性。
703
2023-03-15
详解在SpringBoot中使用MongoDb做单元测试的代码
先评价: 功能倒是不错,但是总觉得耽误时间
先引入pom依赖
代码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.ouiLNiCt.println("使用了redisservice");
User user = userDao.find("id=?", 1l).first();
System.out.println("打印出了一个用户名"+user.userName);
List
for (Map
Object object = map.get("id");
System.out.println(object);
Object object2 = map.get("user_name");
System.out.println(object2);
}iLNiC
MongoUser mongoUser2 = new MongoUser();
mongoUser2.setId("1");
mongoUser2.setName("小明");
mongoTemplate.insert(mongoUser2);
List
for (MongoUser mongoUser : findAll) {
System.out.println("id="+mongoUser.getId());
System.out.println("id="+mongoUser.getName());
}
}
public static void main(String[] args) {
HashMap
hashMap.put("a", 1);
hashMap.put("b", 2);
Set
for (String string : keySet) {
System.out.println(string);
Object object = hashMap.get(string);
http:// System.out.println(object);
System.out.println("================");
}
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~