项目代码
https://github.com/yinhai1114/Java_Learning_Code/tree/main/IDEA_Chapter15/src/com/yinhai/homework
JUnit测试框架
1.基本介绍
1. JUnit是一个Java语言的单元测试框架
2.多数Java的开发环境都已经集成了JUnit作为单元测试的工具
2.如何使用
创建方法后,在方法体的开头使用@Test,点击Alt + Enter 选择JUnit5.4,点OK等待后就会加入JUnit框架,加入之后就可以在方法处右键Run使用
1.
public class Homework01 {public static void main(String[] args) {}@Testpublic void testList(){DAO<User> userDAO = new DAO<>();userDAO.save("1",new User(1, "hello"));userDAO.save("2",new User(2, "xiaowang"));userDAO.save("3",new User(3, "xiaobai"));System.out.println(userDAO.list());userDAO.update("213213213",new User(0,"xiaowang1"));System.out.println("修改后");System.out.println(userDAO.list());userDAO.delete("1");System.out.println(userDAO.list());}
}class DAO<T>{private Map<String,T> map = new HashMap<>();@Testpublic void save(String id,T entity){map.put(id,entity);}@Testpublic T get (String id){//符合return (T)map.get(id);}@Testpublic void update(String id,T entity){//符合Set<String> keySet = map.keySet();for (String key : keySet) {if(!id.equals(key)){System.out.println("错误,没有该键值");return;}}map.put(id,entity);}@Testpublic List<T> list() {//使用迭代器也行 都一样Collection<T> values = map.values();System.out.println("---取出所有的value 放在List集合内----");List<T> list = new ArrayList<>(values);return list;}@Testpublic void delete(String id){if(map.remove(id) != null){System.out.println("删除成功");}else{System.out.println("失败");}}
}
class User{private int id;private String name;public User(int id, String name) {this.id = id;this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +'}';}
}