提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.report.service.goview; |
H |
2 |
|
|
3 |
import com.iailab.framework.test.core.ut.BaseDbUnitTest; |
|
4 |
import com.iailab.module.report.controller.admin.goview.vo.data.GoViewDataRespVO; |
|
5 |
import org.junit.jupiter.api.Test; |
|
6 |
import org.springframework.boot.test.mock.mockito.MockBean; |
|
7 |
import org.springframework.context.annotation.Import; |
|
8 |
import org.springframework.jdbc.core.JdbcTemplate; |
|
9 |
import org.springframework.jdbc.support.rowset.SqlRowSet; |
|
10 |
import org.springframework.jdbc.support.rowset.SqlRowSetMetaData; |
|
11 |
|
|
12 |
import javax.annotation.Resource; |
|
13 |
import java.util.Arrays; |
|
14 |
|
|
15 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
|
16 |
import static org.mockito.ArgumentMatchers.eq; |
|
17 |
import static org.mockito.Mockito.mock; |
|
18 |
import static org.mockito.Mockito.when; |
|
19 |
|
|
20 |
@Import(GoViewDataServiceImpl.class) |
|
21 |
public class GoViewDataServiceImplTest extends BaseDbUnitTest { |
|
22 |
|
|
23 |
@Resource |
|
24 |
private GoViewDataServiceImpl goViewDataService; |
|
25 |
|
|
26 |
@MockBean |
|
27 |
private JdbcTemplate jdbcTemplate; |
|
28 |
|
|
29 |
@Test |
|
30 |
public void testGetDataBySQL() { |
|
31 |
// 准备参数 |
|
32 |
String sql = "SELECT id, name FROM system_users"; |
|
33 |
// mock 方法 |
|
34 |
SqlRowSet sqlRowSet = mock(SqlRowSet.class); |
|
35 |
when(jdbcTemplate.queryForRowSet(eq(sql))).thenReturn(sqlRowSet); |
|
36 |
// mock 元数据 |
|
37 |
SqlRowSetMetaData metaData = mock(SqlRowSetMetaData.class); |
|
38 |
when(sqlRowSet.getMetaData()).thenReturn(metaData); |
|
39 |
when(metaData.getColumnNames()).thenReturn(new String[]{"id", "name"}); |
|
40 |
// mock 数据明细 |
|
41 |
when(sqlRowSet.next()).thenReturn(true).thenReturn(true).thenReturn(false); |
|
42 |
when(sqlRowSet.getObject("id")).thenReturn(1L).thenReturn(2L); |
|
43 |
when(sqlRowSet.getObject("name")).thenReturn("iailab").thenReturn("平台"); |
|
44 |
|
|
45 |
// 调用 |
|
46 |
GoViewDataRespVO dataBySQL = goViewDataService.getDataBySQL(sql); |
|
47 |
// 断言 |
|
48 |
assertEquals(Arrays.asList("id", "name"), dataBySQL.getDimensions()); |
|
49 |
assertEquals(2, dataBySQL.getDimensions().size()); |
|
50 |
assertEquals(2, dataBySQL.getSource().get(0).size()); |
|
51 |
assertEquals(1L, dataBySQL.getSource().get(0).get("id")); |
|
52 |
assertEquals("iailab", dataBySQL.getSource().get(0).get("name")); |
|
53 |
assertEquals(2, dataBySQL.getSource().get(1).size()); |
|
54 |
assertEquals(2L, dataBySQL.getSource().get(1).get("id")); |
|
55 |
assertEquals("平台", dataBySQL.getSource().get(1).get("name")); |
|
56 |
} |
|
57 |
|
|
58 |
} |