Skip to content
Snippets Groups Projects
Commit 3b62fc9f authored by Josh Sun's avatar Josh Sun
Browse files

Merge branch '27-integration-test-for-searchpage-api' into 'master'

Resolve "Integration test for searchpage API"

Closes #27

See merge request !23
parents 17649eba 85ba3bc7
No related branches found
No related tags found
1 merge request!23Resolve "Integration test for searchpage API"
spring.datasource.url=jdbc:mysql://localhost:3306/FreshOnline-Test?useUnicode=true&characterEncoding=utf8&allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=
......
......@@ -19,8 +19,6 @@ import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@RunWith(SpringRunner.class)
@SpringBootTest
......@@ -73,9 +71,6 @@ class StockedGoodsControllerIntegrationTest {
void getGoodsDetails() {
}
@Test
void getSearch() {
}
@Test
void uploadGoodsPictures() {
......
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");
}
}
\ No newline at end of file
SET SQL_SAFE_UPDATES = 0;
delete from stocked_goods;
\ No newline at end of file
-- MySQL dump 10.13 Distrib 8.0.27, for macos11 (x86_64)
--
-- Host: localhost Database: test
-- ------------------------------------------------------
-- Server version 8.0.28
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!50503 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Dumping data for table `stocked_goods`
--
LOCK TABLES `stocked_goods` WRITE;
/*!40000 ALTER TABLE `stocked_goods` DISABLE KEYS */;
INSERT INTO `stocked_goods` VALUES (1,'beef1',2,20.33,10.33,2.20,'beef things',1,18.33,0.00,0,'TNT',123,0,'/usr/balabala',0),(2,'pork',2,22.33,20.33,3.33,'pork things',1,18.33,5.00,2,'abc',124,0,'/usr/else',0),(3,'lamp',2,33.00,50.00,5.00,'lamp things',0,0.00,3.00,5,'bcd',125,0,'/usr/lalala',0),(4,'beef2',2,21.33,10.33,2.20,'beef things',0,0.00,4.32,20,'TNT',123,0,'/usr/balabala',0),(5,'beef3',2,22.33,10.33,2.20,'beef things',0,0.00,0.00,0,'TNT',123,0,'/usr/balabala',0),(6,'beef4',2,20.33,10.33,2.20,'beef things',0,0.00,0.00,0,'ABC',123,0,'/usr/balabala',0),(7,'beef5',2,20.33,10.33,2.20,'beef things',0,0.00,0.00,0,'NOTNT',123,0,'/usr/balabala',0);
/*!40000 ALTER TABLE `stocked_goods` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2022-02-25 19:37:52
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment