gpt4 book ai didi

java - Mockito 测试 - 测试 write() 方法导致 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 19:34:59 30 4
gpt4 key购买 nike

基于示例here ,我尝试为我的方法 write() 创建一个单元测试,该方法基本上采用一个对象并将其保存到我的 SQLite DB 中。

我收到此错误:

enter image description here

在下面的代码中,这两行已用“错误”一词进行了注释。

1) 与数据库连接

public class DBAdapter {

public Connection getConnection() throws WebApplicationException {
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource)
ctx.lookup("java:/comp/env/jdbc/escapeconnect");
Connection con = ds.getConnection();
Statement st = con.createStatement();
st.execute("PRAGMA foreign_keys=on;");
st.close();
return con;
} catch (NamingException e) {
e.printStackTrace();
throw new WebApplicationException(e.getMessage());
} catch (SQLException e) {
e.printStackTrace();
throw new WebApplicationException(e.getMessage());
}
}
}

2) 获取对象并将其插入数据库的类

public class DAOpanel implements DAOpanelIF {

DBAdapter dbAdapter = new DBAdapter();

@Override
public int write(PanelDAOBean panel) {

String query = "";

if(panel.getId()<1) {
query = "INSERT INTO panel (name,device_mac) VALUES(?,?)";

try (Connection con = dbAdapter.getConnection();
PreparedStatement pstm = con.prepareStatement(query);){ // ERROR
pstm.setString(1, panel.getName());
pstm.setString(2, panel.getDevice_mac());

int rowsAffected = pstm.executeUpdate();
if(rowsAffected==1) {
ResultSet rs = pstm.getGeneratedKeys();
if(rs.next()) {
return rs.getInt(1);
}
}
pstm.close();
} catch (SQLException e) {
e.printStackTrace();
throw new WebApplicationException(e.getMessage());
}
}else {
query = "UPDATE panel SET name = ?, device_mac = ? WHERE id = ?";

try (Connection con = dbAdapter.getConnection();
PreparedStatement pstm = con.prepareStatement(query);){
pstm.setString(1, panel.getName());
pstm.setString(2, panel.getDevice_mac());
pstm.setInt(3, panel.getId());
pstm.executeUpdate();
pstm.close();
} catch (SQLException e) {
e.printStackTrace();
throw new WebApplicationException(e.getMessage());
}
}
return -1;
}

3)DAOpanel类的write()方法测试

@ExtendWith(MockitoExtension.class)
class TestDaoPanel {

PanelDAOBean panelForTest = new PanelDAOBean();

@InjectMocks private DAOpanel daoPanel;

@Mock private DBAdapter dbAdapter;

@Mock private Connection con;

@Mock private PreparedStatement pstm;

@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
panelForTest.setName("Panel Unit Test");
panelForTest.setDevice_mac("FF:FF:FF:FF:FF:FF");

when(con.prepareStatement(Mockito.any())).thenReturn(pstm);
when(dbAdapter.getConnection()).thenReturn(con);
}

/**
* Test method for {@link
* main.java.ch.ffhs.pa5.escapeconnect.persistency.DAOpanel#write(PanelDAOBean)}.
*/
@Test
void testWrite() {
daoPanel.write(panelForTest); // ERROR
}

4)要插入数据库的对象

public class PanelDAOBean {
private int id = 0;
private String name;
private String device_mac;
public PanelDAOBean() {
super();
}
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;
}
public String getDevice_mac() {
return device_mac;
}
public void setDevice_mac(String device_mac) {
this.device_mac = device_mac;
}

}

感谢您的帮助。

最佳答案

您的问题在以下行

    public class DAOpanel implements DAOpanelIF {

DBAdapter dbAdapter = new DBAdapter();
...
}

您正在模拟DBAdapter,但没有将其注入(inject)DAOpanel。您只需添加一个接受 DAOpanel

的构造函数即可
public class DAOpanel implements DAOpanelIF {
priavte final DBAdapter dbAdapter;
public DAOpanel(DBAdapter dbAdapter) {
this.dbAdapter = dbAdapter;
}
}

当您使用@Mock@InjectMock时,依赖项将自动注入(inject)到构造函数中。

关于java - Mockito 测试 - 测试 write() 方法导致 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59230019/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com