1.Maven引入包
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>2.1.214</version> <scope>test</version> </dependency>
2.写UnitTest
import org.junit.After; import org.junit.Before; import org.junit.Test; import java.sql.*; public class TestClass throws Exception{ Connection conn = null; @Before public void beforeTest(){ String URL = "jdbc:h2:mem:test;DB_COLSE_DELAY=-1"; String USR = "sa"; String PSD = "666666"; Class.forName("org.h2.Driver"); conn = DriverManager.getConnection(URL,USR,PSD); Statement statement = conn.createStatement(); statement.execute("drop table if exists user"); statement.execute("create table user ( id NUMBER, name VARCHAR2(10) )"); statement.execute("insert into user(id,name) values (1,hhh)"); ResultSet resultSet = statement.executeQuery("select * from user"); while(resultSet.next()){ resultSet.getInteger("id"); resultSer.getString("name"); } } @After public void afterTest() throws Exception{ Statement statement = conn.createStatement(); statement.execute("drop table if exists user"); } @Test public void Test(){ ... } }
标签:H2,Spring,UnitTest,user,statement,org,import,junit,conn From: https://www.cnblogs.com/xin-xing/p/17276619.html