Java 如何使用JUnit5从SongServlet测试方法

Java 如何使用JUnit5从SongServlet测试方法,java,servlets,mockito,junit5,Java,Servlets,Mockito,Junit5,我的任务是测试一个用Java编写的HttpServlet,它连接到数据库并实现了以下方法: doGet()、doPost()、doDelete()、doOptions() 为了独立于数据库连接测试功能,我实现了一个InMemoryDao,它用Json文件中的测试数据填充H2数据库,并注入到我的ServletTest类中 下面是doGet()方法的一个示例: 我记忆中的松岛课是这样的: public class InMemorySongDao extends MysqlSongDao { pu

我的任务是测试一个用Java编写的HttpServlet,它连接到数据库并实现了以下方法:

doGet()、doPost()、doDelete()、doOptions()

为了独立于数据库连接测试功能,我实现了一个InMemoryDao,它用Json文件中的测试数据填充H2数据库,并注入到我的ServletTest类中

下面是doGet()方法的一个示例:

我记忆中的松岛课是这样的:

public class InMemorySongDao extends MysqlSongDao {


public InMemorySongDao() throws SQLException {
    super(new ComboPooledDataSource());
    UUID uuid = UUID.randomUUID();

    // Connect to a unique in-memory database identified by a random uuid
    this.dataSource.setJdbcUrl("jdbc:h2:mem:" + uuid);
    try (PreparedStatement st = this.dataSource.getConnection().prepareStatement(
            "CREATE TABLE songs (" +
                    "id int not null primary key auto_increment," +
                    "title varchar(100) not null," +
                    "artist varchar(100)," +
                    "label varchar(100)," +
                    "released int" +
                    ")")) {
        st.execute();
    }
}

/**
 * Creates a songs dao prefilled with the songs from the given resource.
 */
public InMemorySongDao(String resourceName) throws SQLException, IOException {
    this();
    final ObjectMapper mapper = new ObjectMapper();
    JsonNode rootNode = mapper.readTree(getClass().getResource(resourceName));

    // Read array node or use empty node
    ArrayNode array = (rootNode.isArray()) ? (ArrayNode) rootNode : mapper.createArrayNode();
    try (PreparedStatement st = this.dataSource.getConnection().prepareStatement("INSERT INTO songs (id, title, artist, label, released) values (?,?,?,?,?)")) {
        // Iterate over the array and populate the database with the songs
        Iterator<JsonNode> elements = array.elements();
        while (elements.hasNext()) {
            JsonNode node = elements.next();
            if (!node.isObject()) continue;
            st.setInt(1, node.get("id").asInt());
            st.setString(2, node.get("title").asText());
            st.setString(3, node.get("artist").asText());
            st.setString(4, node.get("label").asText());
            st.setInt(5, node.get("released").asInt());
            st.addBatch();
        }
        st.executeBatch();
    }
  }
}
内存Songdao中的公共类扩展了MysqlSongDao{
public InMemorySongDao()引发SQLException{
super(新的ComboPooledDataSource());
UUID UUID=UUID.randomUUID();
//连接到由随机uuid标识的唯一内存中数据库
setJdbcUrl(“jdbc:h2:mem:+uuid”);
try(PreparedStatement st=this.dataSource.getConnection().prepareStatement(
“创建表格歌曲(”+
id int not null主键自动递增+
title varchar(100)不为空+
“艺术家瓦查尔(100),”+
标签varchar(100)+
“已发布int”+
")")) {
st.execute();
}
}
/**
*创建一个歌曲dao,其中预先填充了给定资源中的歌曲。
*/
publicInMemorySongdao(字符串resourceName)抛出SQLException、IOException{
这个();
最终ObjectMapper映射器=新ObjectMapper();
JsonNode rootNode=mapper.readTree(getClass().getResource(resourceName));
//读取数组节点或使用空节点
ArrayNode数组=(rootNode.isArray())?(ArrayNode)rootNode:mapper.createArrayNode();
try(PreparedStatement st=this.dataSource.getConnection().prepareStatement(“插入歌曲(id、标题、艺术家、标签、发布)值(?,,,,,?)”){
//迭代数组并用歌曲填充数据库
迭代器元素=数组。元素();
while(elements.hasNext()){
JsonNode=elements.next();
如果(!node.isObject())继续;
st.setInt(1,node.get(“id”).asInt());
st.setString(2,node.get(“title”).asText());
st.setString(3,node.get(“artist”).asText());
st.setString(4,node.get(“label”).asText());
st.setInt(5,node.get(“released”).asInt();
st.addBatch();
}
st.executeBatch();
}
}
}
如果有人能在这方面给我任何帮助,我将非常感激。不幸的是,我无法通过研究找到任何合适的例子

亲切问候,, 麦克风

public class InMemorySongDao extends MysqlSongDao {


public InMemorySongDao() throws SQLException {
    super(new ComboPooledDataSource());
    UUID uuid = UUID.randomUUID();

    // Connect to a unique in-memory database identified by a random uuid
    this.dataSource.setJdbcUrl("jdbc:h2:mem:" + uuid);
    try (PreparedStatement st = this.dataSource.getConnection().prepareStatement(
            "CREATE TABLE songs (" +
                    "id int not null primary key auto_increment," +
                    "title varchar(100) not null," +
                    "artist varchar(100)," +
                    "label varchar(100)," +
                    "released int" +
                    ")")) {
        st.execute();
    }
}

/**
 * Creates a songs dao prefilled with the songs from the given resource.
 */
public InMemorySongDao(String resourceName) throws SQLException, IOException {
    this();
    final ObjectMapper mapper = new ObjectMapper();
    JsonNode rootNode = mapper.readTree(getClass().getResource(resourceName));

    // Read array node or use empty node
    ArrayNode array = (rootNode.isArray()) ? (ArrayNode) rootNode : mapper.createArrayNode();
    try (PreparedStatement st = this.dataSource.getConnection().prepareStatement("INSERT INTO songs (id, title, artist, label, released) values (?,?,?,?,?)")) {
        // Iterate over the array and populate the database with the songs
        Iterator<JsonNode> elements = array.elements();
        while (elements.hasNext()) {
            JsonNode node = elements.next();
            if (!node.isObject()) continue;
            st.setInt(1, node.get("id").asInt());
            st.setString(2, node.get("title").asText());
            st.setString(3, node.get("artist").asText());
            st.setString(4, node.get("label").asText());
            st.setInt(5, node.get("released").asInt());
            st.addBatch();
        }
        st.executeBatch();
    }
  }
}