2016-08-22 136 views
0

完成一些REST API(Spring + Hibernate應用程序),想要添加一些控制器測試。請幫助啓動(@Before)和完成(@After)服務器? DataConfig.java:如何啓動和停止H2服務器進行測試

@Configuration 
@PropertySource("file:${properties.home}app.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()); 
    } 
+0

只是一個快速句話下面完成我的答案。如果你想使用服務器模式,你應該啓動一個tcpserver,但也使用一個tcp jdbc url,比如''jdbc:h2:tcp:// localhost/test'''。您也可以使用tcp服務器的內存。如果您不需要tcp服務器,則需要正確關閉內存數據庫以清除其內容。 –

回答

0

你需要調用start()stop()從這個類:org.h2.server.TcpServer