我正在尝试在我的测试失败的第一种情况下编写一些测试,虽然我知道我的代码可以工作:
import org.junit.*;
import static org.junit.Assert.*;
import java.sql.Connection;
import java.sql.SQLException;
public class TestGetConnection {
@Test
public void getConnection() throws SQLException{
GetConnection getCon = new GetConnection();
Connection con = getCon.getConnection();
assertEquals("com.mysql.cj.jdbc.ConnectionImpl@3aa9e816",con.toString());
}
}
这是 GetConnection 类:
package BE.Intec.Daniel.BabySteps;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Scanner;
public class GetConnection {
private String url2 = "jdbc:mysql://";
private String url;
private String user;
private String timezone = "?useUnicode=true&use JDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=CET";
private String password;
private Scanner input = new Scanner(System.in);
public GetConnection() {
System.out.println("Geef het adres zonder http:// of www. van de database in aub.");
url2 += input.nextLine();
url = url2 + timezone;
System.out.println("Geef de username in aub.");
user = input.nextLine();
System.out.println("Geef het paswoord in aub.");
password = input.nextLine();
}
public String setUrl(String dbName) {
url2 += dbName;
return url = url2 + timezone;
}
public java.sql.Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
}
在第二个测试中,我首先使用模拟对象来获取连接,但不知何故,在尝试创建数据库之前,之前的操作没有被执行。
package BE.Intec.Daniel.BabySteps;
import static org.junit.Assert.*;
import java.sql.Connection;
import java.sql.SQLException;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;
public class TestCreateDatabase {
private Connection con;
@Before
public void init() throws SQLException{
GetConnection getcon = EasyMock.createMock(GetConnection.class);
con = getcon.getConnection();
}
@Test
public void createDatabase(){
CreateDataBase newDb = new CreateDataBase(con);
String result = newDb.createDataBase();
assertEquals("Uw databse werd succesvol gecreëerd",result);
}
}
这是 CreateDatabase 类:
package BE.Intec.Daniel.BabySteps;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Scanner;
import com.mysql.cj.api.jdbc.Statement;
public class CreateDataBase {
private Connection con;
private java.sql.Statement stat;
private String databaseName;
public CreateDataBase(Connection con) {
this.con = con;
createDataBase();
}
public String createDataBase() {
Scanner in = new Scanner(System.in);
System.out.println("Hoe moet uw database noemen?");
databaseName = in.nextLine();
String succes = "";
try {
stat = con.createStatement();
String statement = "CREATE DATABASE IF NOT EXISTS " + databaseName;
stat.executeUpdate(statement);
succes ="Uw databse werd succesvol gecreëerd";
} catch (SQLException e) {
System.out.println("Er ging iets fout");
e.printStackTrace();
}
return succes;
}
public String getDatabaseName() {
return databaseName;
}
}
有什么想法为什么我的测试失败吗? 任何帮助将不胜感激 别介意我的英语,我是比利时人,所以不是本地人。
最佳答案
这第一个测试是技术测试。您想要检查驱动程序是否正确配置。
您应该将 scanner.nextLine()
返回的字符串值 stub 为您想要用于测试的值(网址、用户名、密码)。
此外,您不应该在 assertEquals()
方法中检查 "com.mysql.cj.jdbc.ConnectionImpl@3aa9e816"
。依赖 Object 类中的默认 toString()
方法来断言结果是一个坏主意,因为它在其表示形式中包含对象的哈希码(@...
部分),这不是比较内容的正确方法。
这应该足够了:
Assert.assertTrue(con.toString().contains("com.mysql.cj.jdbc.ConnectionImpl"));
这是更新的测试方法:
@Test
public void getConnection() throws SQLException {
String input = "urlToTest\nuser\nvalue";
InputStream in = new ByteArrayInputStream(input.getBytes());
System.setIn(in);
GetConnection getCon = new GetConnection();
Connection con = getCon.getConnection();
Assert.assertTrue(con.toString().contains("com.mysql.cj.jdbc.ConnectionImpl"));
}
对于第二个测试,您应该选择 Mockito,而不是 EasyMock,因为它更容易、有效、灵活且功能强大。
另外,使用的测试逻辑不正确。
在这里,您模拟了 GetConnection 对象,但没有指定在调用方法时模拟对象应该执行的操作:
@Before
public void init() throws SQLException{
GetConnection getcon = EasyMock.createMock(GetConnection.class);
con = getcon.getConnection();
}
无论如何, mock 这可能不是最好的选择。
CreateDataBase
类执行数据库创建。
如果您想测试它,您应该通过集成(即与真实数据库。为什么不是内存数据库)来测试它,而不是使用模拟。
关于java - 使用模拟对象进行 Junit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43401851/