如何启动和停止 H2 服务器进行测试

Posted

技术标签:

【中文标题】如何启动和停止 H2 服务器进行测试【英文标题】:How start and stop H2 server for testing 【发布时间】:2016-08-22 17:34:32 【问题描述】:

做了一些 REST API(Spring + Hibernate 应用程序),想添加一些控制器测试。请帮助如何启动(@Before)和完成(@After)服务器? DataConfig.java:

@Configuration
@PropertySource("file:$properties.homeapp.properties")
public class DataConfig 
    @Autowired
    private Environment env;

    @Bean
    public LocalSessionFactoryBean sessionFactory() 
        Resource config = new ClassPathResource("hibernate.cfg.xml");
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setConfigLocation(config);
        sessionFactory.setPackagesToScan(env.getProperty("HomeAutomation.entity.package"));
        sessionFactory.setDataSource(dataSource());
        return sessionFactory;
    

    @Bean
    public DataSource dataSource() 
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName(env.getProperty("HomeAutomation.db.driver"));
        ds.setUrl(env.getProperty("HomeAutomation.db.url"));
        ds.setUsername(env.getProperty("HomeAutomation.db.username"));
        ds.setPassword(env.getProperty("HomeAutomation.db.password"));
        return ds;
    

hibernate.cfg.xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="dialect">org.hibernate.dialect.H2Dialect</property>
        <property name="show_sql">true</property>
        <property name="hibernate.implicit_naming_strategy">org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl</property>
        <property name="hbm2ddl.auto">update</property>
    </session-factory>
</hibernate-configuration>

app.properties:

HomeAutomation.entity.package = com.zzheads.HomeAutomation.model
HomeAutomation.db.driver = org.h2.Driver
HomeAutomation.db.url = jdbc:h2:tcp://localhost/~/data/HomeAutomation
HomeAutomation.db.username = sa
HomeAutomation.db.password =

Application.java:

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan
public class Application     
    public static final String BASE_URL ="http://localhost:8080/";
    public static void main(String[] args) 
        SpringApplication.run(Application.class, args);
    

以及测试的样子(此处需要帮助):

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class RoomControllerTest 
    @Autowired
    private RoomService roomService;
    @Autowired
    private EquipmentService equipmentService;
    @Autowired
    private ControlService controlService;

    private ApiClient client;
    private Gson gson;
    private static final String PORT = "4568";
    private static final String TEST_DATASOURCE = "jdbc:h2:mem:testing";

    @BeforeClass
    public static void startServer() 
    

    @AfterClass
    public static void stopServer() 
    

    @Before public void setUp() throws Exception 
    

    @After public void tearDown() throws Exception 
    

    @Test public void testAddRoom() throws Exception 
        Map<String, Object> values = new HashMap<>();
        values.put("roomName", "Kitchen");
        values.put("squareFootage", "325");
        ApiResponse res = client.request("POST", "/room", gson.toJson(values));
        assertEquals(HttpStatus.CREATED.value(), res.getStatus());
    

【问题讨论】:

只是一个简短的评论来完成我的回答。如果你想使用服务器模式,你应该启动一个 tcpserver,但也要使用一个 tcp jdbc url,比如jdbc:h2:tcp://localhost/test。您还可以将内存与 tcp 服务器一起使用。如果不需要 tcp 服务器,则需要正确关闭内存数据库以清除其内容。 【参考方案1】:

你需要从这个类调用start()stop()org.h2.server.TcpServer

【讨论】:

当H2服务器启动由环境或spring等自动管理时,此解决方案不起作用。

以上是关于如何启动和停止 H2 服务器进行测试的主要内容,如果未能解决你的问题,请参考以下文章

如何停止 DataTables 实例已启动的所有当前正在进行的 Ajax 查询?

linux 如何启动,停止,查看服务

如何使用 h2load 为基于 Netty 的 http2 服务器做基准测试?

活动停止/关闭时音乐服务停止?

用蚂蚁启动/停止 H2

以编程方式停止 H2 数据库