Newer
Older
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
package com.example.freshonline.controller;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.junit.Assert;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@RunWith(Parameterized.class)
@SpringBootTest
@TestPropertySource(locations = "classpath:application-test.properties")
@AutoConfigureMockMvc
class StockedGoodsControllerIntegrationTest_2 {
@Autowired
private MockMvc mockMvc;
private static String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&allowPublicKeyRetrieval=true&useSSL=false";
private static String classname = "com.mysql.cj.jdbc.Driver";
private static String username = "root";
private static String password = "";
// @SuppressWarnings("static-access")
// @Value("${spring.datasource.url}")
// public void setUrl(String url) {
// this.url = url;
// }
//
// @SuppressWarnings("static-access")
// @Value("${spring.datasource.driver-class-name}")
// public void setClassname(String classname){
// this.classname = classname;
// }
//
// @SuppressWarnings("static-access")
// @Value("${spring.datasource.username}")
// public void setUsername(String username){
// this.username = username;
// }
//
// @SuppressWarnings("static-access")
// @Value("${spring.datasource.password}")
// public void setPassword(String password){
// this.password = password;
// }
// md 干 啥也用不了 破大防
@ParameterizedTest
@CsvSource({
"http://localhost:8080/goods,7,1",
"http://localhost:8080/goods,7,1"})
public void getSearch(String url, int expectedGoodsTotal, int expectedFirstGoodsId) throws Exception{
System.out.println("testing getSearch, test url = " + url + " expectedGoodsTotal = "
+ expectedGoodsTotal + " expectedFirstGoodsId = " + expectedFirstGoodsId);
MvcResult mvcResult = mockMvc.perform(get(url)).andReturn();
System.out.println("response type: " + mvcResult.getResponse().getContentType());
JSONObject resp = JSONObject.parseObject(mvcResult.getResponse().getContentAsString());
JSONObject data = (JSONObject) resp.get("data");
JSONArray stockedGoodsList = data.getJSONArray("goods_list");
int goods_total = (int) data.get("goods_total");
Assert.assertEquals(goods_total, expectedGoodsTotal);
if (goods_total != 0){
int firstGoodsId = stockedGoodsList.getJSONObject(0).getInteger("id");
Assert.assertEquals(firstGoodsId, expectedFirstGoodsId);
}
}
private static void executeSqlScript(String scriptName) throws ClassNotFoundException, SQLException, FileNotFoundException {
Class.forName(classname);
Connection connection = DriverManager.getConnection(url, username, password);
ScriptRunner scriptRunner = new ScriptRunner(connection);
Resources.setCharset(Charset.forName("UTF8"));
scriptRunner.runScript(new FileReader(
System.getProperty("user.dir") + "/src/test/static.test-sqls/" + scriptName));
connection.close();
}
@BeforeAll
public static void setup() throws ClassNotFoundException, SQLException, FileNotFoundException {
executeSqlScript("stocked_goods_test_init.sql");
}
@AfterAll
public static void finish() throws ClassNotFoundException, SQLException, FileNotFoundException {
executeSqlScript("stocked_goods_test_finish.sql");
}
}