发表于: 2025-03-22 20:45:54
0 14
今天完成的事情:
昨天又没提交上,两天写一起了
今天让师兄检查发现JdbcTemplate格式有问题缺少了实现类。
package org.example.model;
public class ye {
private Long id;
private String name;
private String qq;
private String phone;
private String graduatedSchool;
private String number;
private String wish;
private String link;
private String brother;
private String comprehentPlace;
private String comprehentTime;
private String createAt;
private String updateAt;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getGraduatedSchool() {
return graduatedSchool;
}
public void setGraduatedSchool(String graduatedSchool) {
this.graduatedSchool = graduatedSchool;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getWish() {
return wish;
}
public void setWish(String wish) {
this.wish = wish;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getBrother() {
return brother;
}
public void setBrother(String brother) {
this.brother = brother;
}
public String getComprehentPlace() {
return comprehentPlace;
}
public void setComprehentPlace(String comprehentPlace) {
this.comprehentPlace = comprehentPlace;
}
public String getComprehentTime() {
return comprehentTime;
}
public void setComprehentTime(String comprehentTime) {
this.comprehentTime = comprehentTime;
}
public String getCreateAt() {
return createAt;
}
public void setCreateAt(String createAt) {
this.createAt = createAt;
}
public String getUpdateAt() {
return updateAt;
}
public void setUpdateAt(String updateAt) {
this.updateAt = updateAt;
}
public ye() {
}
public ye(Long id, String name, String qq, String phone, String graduatedSchool, String number, String wish, String link, String brother, String comprehentPlace, String comprehentTime, String createAt, String updateAt) {
this.id = id;
this.name = name;
this.qq = qq;
this.phone = phone;
this.graduatedSchool = graduatedSchool;
this.number = number;
this.wish = wish;
this.link = link;
this.brother = brother;
this.comprehentPlace = comprehentPlace;
this.comprehentTime = comprehentTime;
this.createAt = createAt;
this.updateAt = updateAt;
}
@Override
public String toString() {
return "ye{" +
"id=" + id +
", name='" + name + '\'' +
", qq='" + qq + '\'' +
", phone='" + phone + '\'' +
", graduatedSchool='" + graduatedSchool + '\'' +
", number='" + number + '\'' +
", wish='" + wish + '\'' +
", link='" + link + '\'' +
", brother='" + brother + '\'' +
", comprehentPlace='" + comprehentPlace + '\'' +
", comprehentTime='" + comprehentTime + '\'' +
", createAt='" + createAt + '\'' +
", updateAt='" + updateAt + '\'' +
'}';
}
}
18.学习Junit,并尝试写自己的第一个单元测试,记着要写在自己的src/main/test下。
设计JdbcTemplate的单元测试
添加代码在pom.xml上。
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.2</version>
<scope>test</scope>
</dependency>
<!-- Mockito -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.12.4</version>
<scope>test</scope>
</dependency>
<!-- Mockito JUnit Jupiter Integration -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>3.12.4</version>
<scope>test</scope>
</dependency>
w'www
package org.example.dao.impl;
import org.example.dao.UserDao;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
class UserDaoImplTest {
@Mock
private JdbcTemplate jdbcTemplate;
@InjectMocks
private UserDaoImpl userDao; // 修改为 UserDaoImpl 类型
@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this);
}
@Test
void addUser() {
String name = "John Doe";
int QQ = 123456;
userDao.addUser(name, QQ);
verify(jdbcTemplate, times(1)).update(eq("INSERT INTO ye (name, QQ) VALUES (?, ?)"), eq(name), eq(QQ));
}
@Test
void updateUser() {
// 准备测试数据
int id = 1;
String name = "Jane Doe";
int QQ = 654321;
// 调用被测方法
userDao.updateUser(id, name, QQ);
// 验证 jdbcTemplate 的 update 方法是否被调用
verify(jdbcTemplate, times(1)).update(eq("UPDATE ye SET name = ?, QQ = ? WHERE id = ?"), eq(name), eq(QQ), eq(id));
}
@Test
void deleteUser() {
// 准备测试数据
int id = 1;
// 调用被测方法
userDao.deleteUser(id);
// 验证 jdbcTemplate 的 update 方法是否被调用
verify(jdbcTemplate, times(1)).update(eq("DELETE FROM ye WHERE id = ?"), eq(id));
}
@Test
void queryAllUsers() {
// 准备测试数据
List<String> mockUsers = Arrays.asList(
"ID: 1, Username: Alice, QQ: 111111",
"ID: 2, Username: Bob, QQ: 222222"
);
// 模拟 jdbcTemplate 的 query 方法返回 mockUsers
when(jdbcTemplate.query(anyString(), any(RowMapper.class))).thenReturn(mockUsers);
// 调用被测方法
List<String> users = userDao.queryAllUsers();
// 验证结果
assertNotNull(users);
assertEquals(2, users.size());
assertTrue(users.containsAll(mockUsers));
verify(jdbcTemplate, times(1)).query(eq("SELECT * FROM ye"), any(RowMapper.class));
}
}
明天计划的事:
- 对Mybatis连接数据库的增删改查进行单元测试。
遇到的问题:
对Mybatis连接数据库的单元测试一直报错,发现Mybatis的格式不对
收获:
成功使用修改了JdbcTemplate的错误格式,进行了junit测试,学习一些面向对象的知识。
评论