我嘗試學習正確且持續地測試我的項目。不幸的是,有一些事情我不明白,因為關于這個話題有很多不同的意見。我不明白如何決定哪些課程“值得”測試它們。作為示例,我從 oracle 中獲取了示例 JDBC 連接類:public Connection getConnection() throws SQLException { Connection conn = null; Properties connectionProps = new Properties(); connectionProps.put("user", this.userName); connectionProps.put("password", this.password); if (this.dbms.equals("mysql")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + "://" + this.serverName + ":" + this.portNumber + "/", connectionProps); } else if (this.dbms.equals("derby")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + ":" + this.dbName + ";create=true", connectionProps); } System.out.println("Connected to database"); return conn;} 我知道我可以模擬對象以便能夠更孤立地查看班級。測試這樣的課程是否有用,或者我可以從做這樣的事情中受益,看看理論上是否可以建立聯系?public class TestClass { @Mock private Connection conn; @Mock private Database database; @BeforeEach public void setUp() throws Exception { assertNotNull(database); when(database.getConnection()).thenReturn(conn); }}
添加回答
舉報
0/150
提交
取消