woong's

Spock Test 사용하기 본문

Develop/Test

Spock Test 사용하기

dlsdnd345 2016. 2. 13. 19:09
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Spock Test 사용하기


이번에 스터디 기회가 생겨서 기존에 배웠던 , Spring + Maven 을 이번에는 Spring + Gradle 로  

스터디를 학습 하다가 배운 Spock 에 대해 사용법을 간단히 소개 합니다 .


 Spring + Maven 을 통해서 스터디 할적에는 CRUD 를 테스트 하기 위해서 Junit Test 를 이용하여 테스트를 진행하였는데 이번에는 Spock 을 통해서 진행하였습니다 .


Spock 문법에 대해서는 Junt 을 사용해 보신분이라면 보시면 금방 따라 하실수 있습니다 .

저또한 이번에 처음써보았지만 그리 낯설지는 않았습니다 .


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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
public class BookAppTest {
 
    BookApp bookApp;
 
    @Before
    public void setUp() throws SQLException{
 
        bookApp = new BookApp();    
        Book book = new Book();
        book.setName("토비의 스프링");
        book.setAuthor("이일민");
        book.setComment("대세");
        book.setPublishDate(new Date());
 
        bookApp.add(book);
        bookApp.add(book);
    }
 
    @Test
    public void add() throws SQLException{
 
        bookApp = new BookApp();    
 
        Book book = new Book();
        book.setName("토비의 스프링");
        book.setAuthor("이일민");
        book.setComment("대세");
        book.setPublishDate(new Date());
 
        bookApp.add(book);
        
        int result = bookApp.countAll();
        assertThat(result, is(3));
        
    }
 
    @Test
    public void update() throws SQLException{
        
        bookApp = new BookApp();    
        List<Book> bookList = bookApp.getAll();
        
        Book book = new Book();
        book.setId(bookList.get(0).getId());
        book.setName("안드로이드");
        book.setAuthor("젤리빈");
        book.setComment("진저브레드");
        book.setPublishDate(new Date());
        bookApp.update(book);
        
        List<Book> bookList2 = bookApp.getAll();
        assertThat(bookList2.get(0).getName(), is("안드로이드"));
        assertThat(bookList2.get(0).getAuthor(), is("젤리빈"));
        assertThat(bookList2.get(0).getComment(), is("진저브레드"));
        
    }
    //
    @Test
    public void search() throws SQLException{
        
        bookApp = new BookApp();    
        
        List<Book> result = bookApp.search("토비의 스프링");
        for (int i = 0; i < result.size(); i++) {
            assertThat(result.get(i).getName(),is("토비의 스프링"));
        }
    }
    
    @Test
    public void delete() throws SQLException{
 
        bookApp = new BookApp();    
        List<Book> bookList = bookApp.getAll();
 
        Book book = new Book();
        book.setId(bookList.get(0).getId());    
        bookApp.delete(book);
 
        int result = bookApp.countAll();
        assertThat(result, is(1));
 
    }
 
    @Test
    public void get() throws SQLException{
 
        bookApp = new BookApp();    
 
        List<Book> bookList = bookApp.getAll();
 
        Book book = new Book();
        book = bookApp.get(bookList.get(0).getId());
 
        assertThat(book.getName(), is("토비의 스프링"));
        assertThat(book.getAuthor(), is("이일민"));
 
    }
 
    @Test
    public void getAll() throws SQLException{
 
        bookApp = new BookApp();    
        List<Book> bookList = bookApp.getAll();
 
        assertThat(bookList.size(), is(2));
 
        assertThat(bookList.get(0).getName(), is("토비의 스프링"));
        assertThat(bookList.get(0).getAuthor(), is("이일민"));
    }
 
    @Test
    public void deleteAll() throws SQLException {    
 
        bookApp = new BookApp();    
        bookApp.deleteAll();
 
        int result = bookApp.countAll();
        assertThat(result, is(0));
    }
 
    @Test
    public void countAll() throws SQLException{
 
        bookApp = new BookApp();    
        int result = bookApp.countAll();
 
        assertThat(result, is(2));
    }
 
    @After
    public void tearDown() throws SQLException{
        bookApp = new BookApp();
        bookApp.deleteAll();    
    }
}


이부분이 Junit 으로 CRUD를 확인한 Test 가 되겠습니다 .


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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
class BookAppGroovyTest extends Specification  {
 
    def bookApp
 
    def setup(){
 
        bookApp = new BookApp();
 
        def book = new Book();
        book.setName("토비의 스프링")
        book.setAuthor("이일민")
        book.setComment("대세")
        book.setPublishDate(new Date())
 
        bookApp.add(book)
        bookApp.add(book)
    }
 
 
    def "책 추가"(){
 
        when:
        bookApp = new BookApp();
 
        def book = new Book();
        book.setName("토비의 스프링")
        book.setAuthor("이일민")
        book.setComment("대세")
        book.setPublishDate(new Date())
 
        bookApp.add(book);
 
        int result = bookApp.countAll()
        then:
        result ==3
    }
 
    def "책 수정"(){
 
        when:
        bookApp = new BookApp();
        def bookList = bookApp.getAll();
 
        def book = new Book();
        book.setId(bookList.get(0).getId());
        book.setName("안드로이드");
        book.setAuthor("젤리빈");
        book.setComment("진저브레드");
        book.setPublishDate(new Date());
        bookApp.update(book);
 
        def bookList2 = bookApp.getAll();
        then:
        bookList2.get(0).getName() =="안드로이드"
        bookList2.get(0).getAuthor() == "젤리빈"
        bookList2.get(0).getComment() == "진저브레드"
 
    }
 
    def "책 제목으로 찾기"(){
 
        when:
        
        bookApp = new BookApp();
        
        def result = bookApp.search("토비의 스프링");
        
        then:
            for ( i in 0.. result.size()-1) {
                result.get(i).getName() == "토비의 스프링"
            }
    }
 
    def "책 삭제"(){
 
        when:
        bookApp = new BookApp();
        def bookList = bookApp.getAll();
 
        def book = new Book();
        book.setId(bookList.get(0).getId());
        bookApp.delete(book);
 
        def result = bookApp.countAll();
        then:
        result == 1
    }
 
    def "책 id 로 하나 찾기 "(){
        when:
 
        bookApp = new BookApp();
        def bookList = bookApp.getAll();
        def book = new Book();
        book = bookApp.get(bookList.get(0).getId());
 
        then:
        book.getName() == "토비의 스프링"
        book.getAuthor() == "이일민"
    }
 
    def "책 모든 갯수"(){
 
        when:
        bookApp = new BookApp();
        def result = bookApp.countAll()
        then:
        result == 2
    }
 
    def "모든 책 검색"(){
 
        when:
 
        bookApp = new BookApp();
        def bookList = bookApp.getAll();
 
        then:
        bookList.size() ==2
        bookList.get(0).getName() == "토비의 스프링"
        bookList.get(0).getAuthor() == "이일민"
    }
    
    def "모든 책 삭제"(){
        when:
        bookApp = new BookApp();
        bookApp.deleteAll();
 
        def result = bookApp.countAll();
        then:
        result == 0
    }
 
    def cleanup(){
        bookApp = new BookApp()
        bookApp.deleteAll()
    }
 
}


이부분이 위의 Junit Test 를 Spock Groovy 로 바꾼 Test 가 되겠습니다 .


이 Spock Groovy 에 대해서 이번에 찾아보면서  NHN 백기선 씨가 Hello World 에서 소개 된적이있었던것을 보았습니다 .


제가 부연설명을 하는것보다 정리가 잘되있는것을 보는게 나을거 같아 해당 홈페이지 링크 합니다 .

Comments