MySQL, mariaDB와의 연결을 담당하는 DataSource를 설정하고 테스트를 해보았다. 


 스프링 로딩을 위한 어노테이션으로 @RunWith, @WebAppConfiguration, @ContextConfiguration 을 사용했다. 


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
 * DataSource Operating test
 */
 
package org.zerock.web;
 
import java.sql.Connection;
 
import javax.inject.Inject;
import javax.sql.DataSource;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
 
// for spring loading
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations ={"file:src/main/webapp/WEB-INF/spring/**/*.xml"})
public class DataSourceTest {
    
    @Inject
    private DataSource ds;
    
    @Test
    public void testConnection() throws Exception {
        try(Connection con = ds.getConnection()) {
            System.out.println(con);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
}
 
cs



+ Recent posts