Browse Source

增加web测试

zhouhao 8 years ago
parent
commit
d6c9f8e3d5

+ 5 - 0
hsweb-system/hsweb-system-config/hsweb-system-config-starter/pom.xml

@@ -32,6 +32,11 @@
             <artifactId>hsweb-system-config-dao-mybatis</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-system-config-controller</artifactId>
+            <version>${project.version}</version>
+        </dependency>
 
         <dependency>
             <groupId>com.h2database</groupId>

+ 42 - 7
hsweb-system/hsweb-system-config/hsweb-system-config-starter/src/test/java/org/hswebframework/web/starter/config/ConfigTests.java

@@ -1,6 +1,7 @@
 package org.hswebframework.web.starter.config;
 
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 import org.hsweb.ezorm.rdb.executor.SqlExecutor;
 import org.hswebframework.web.bean.config.ConfigBean;
 import org.hswebframework.web.bean.config.SimpleConfigBean;
@@ -11,6 +12,7 @@ import org.hswebframework.web.tests.SimpleWebApplicationTests;
 import org.junit.Assert;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.MediaType;
 
 import java.sql.SQLException;
 import java.util.Date;
@@ -28,28 +30,61 @@ public class ConfigTests extends SimpleWebApplicationTests {
     @Autowired
     private ConfigService<QueryParamBean> configService;
 
+    @Test
+    public void testMvc() throws Exception {
+        //创建bean
+        ConfigBean configBean = configService.createBean();
+        Assert.assertEquals(configBean.getClass(), SimpleConfigBean.class);
+        configBean.setId(IDGenerator.RANDOM.generate());
+        configBean.addContent("test", 1, "测试");
+        configBean.setCreateDate(new Date());
+        configBean.setCreatorId("test");
+        String jsonStr = JSON.toJSONString(configBean);
+
+        JSONObject jsonObject = testPost("/config")
+                .setUp(builder -> builder.accept(MediaType.APPLICATION_JSON)
+                        .contentType(MediaType.APPLICATION_JSON)
+                        .content(jsonStr))
+                .exec().resultAsJson();
+        Assert.assertEquals(jsonObject.getString("data"), configBean.getId());
+
+        JSONObject getRes = testGet("/config/" + configBean.getId()).exec().resultAsJson();
+        Assert.assertEquals(getRes
+                .getObject("data", SimpleConfigBean.class)
+                .get("test")
+                .getNumber(0).intValue(), 1);
+
+        getRes = testGet("/config").exec().resultAsJson();
+        Assert.assertEquals(getRes.getJSONObject("data").getJSONArray("data")
+                .getObject(0, SimpleConfigBean.class)
+                .get("test")
+                .getNumber(0).intValue(), 1);
+    }
+
     @Test
     public void test() throws SQLException {
+        //判断是否安装成功
         boolean installSuccess = executor.tableExists("s_config");
         Assert.assertTrue(installSuccess);
+        //创建bean
         ConfigBean configBean = configService.createBean();
         Assert.assertEquals(configBean.getClass(), SimpleConfigBean.class);
         configBean.setId(IDGenerator.RANDOM.generate());
         configBean.addContent("test", 1, "测试");
         configBean.setCreateDate(new Date());
         configBean.setCreatorId("test");
-
+        //test insert
         configService.insert(configBean);
-
         Assert.assertEquals(configBean.get("test").getNumber(0), 1);
-        configBean = configService.selectSingle(new QueryParamBean());
+        configBean = configService.selectSingle(QueryParamBean.empty());
         configBean.addContent("test2", "2", "");
-        configService.updateByPk(configBean);
-
+        //test update
+        Assert.assertEquals(configService.updateByPk(configBean), 1);
         Assert.assertEquals(configBean.get("test2").getNumber(0).intValue(), 2);
-        configBean = configService.selectSingle(new QueryParamBean());
+        configBean = configService.selectSingle(QueryParamBean.empty());
+        //test delete
         configService.deleteByPk(configBean.getId());
-        Assert.assertEquals(configService.count(new QueryParamBean()), 0);
+        Assert.assertEquals(configService.count(QueryParamBean.empty()), 0);
     }
 
 }

+ 4 - 1
hsweb-system/hsweb-system-config/hsweb-system-config-starter/src/test/resources/application.yml

@@ -10,4 +10,7 @@ spring:
 hsweb:
     app:
       name: 配置管理测试
-      version: 3.0.0
+      version: 3.0.0
+    web:
+      mappings:
+        config: /config #default

+ 4 - 0
hsweb-tests/pom.xml

@@ -28,6 +28,10 @@
             <artifactId>hsweb-spring-boot-starter</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-jdbc</artifactId>

+ 65 - 0
hsweb-tests/src/main/java/org/hswebframework/web/tests/SimpleWebApplicationTests.java

@@ -24,15 +24,24 @@ import org.hswebframework.web.commons.beans.factory.BeanFactory;
 import org.hswebframework.web.commons.beans.factory.MapperBeanFactory;
 import org.hswebframework.web.dao.datasource.DataSourceHolder;
 import org.hswebframework.web.dao.datasource.DatabaseType;
+import org.junit.Before;
 import org.junit.runner.RunWith;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.http.MediaType;
 import org.springframework.jdbc.datasource.DataSourceUtils;
+import org.springframework.mock.web.MockHttpSession;
 import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.test.context.web.WebAppConfiguration;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
+import org.springframework.web.context.WebApplicationContext;
 
+import javax.annotation.Resource;
+import javax.servlet.http.HttpSession;
 import javax.sql.DataSource;
 import javax.validation.Validation;
 import javax.validation.Validator;
@@ -40,6 +49,9 @@ import javax.validation.ValidatorFactory;
 import java.sql.Connection;
 import java.sql.SQLException;
 
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
+import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
+
 /**
  * TODO 完成注释
  *
@@ -49,8 +61,61 @@ import java.sql.SQLException;
 @SpringBootTest(classes = SimpleWebApplicationTests.Config.class)
 public class SimpleWebApplicationTests {
 
+    protected MockMvc               mvc;
+    @Resource
+    protected WebApplicationContext wac;
+
+    protected MockHttpSession session;
+
+    @Before
+    public void setup() throws Exception {
+        this.mvc = webAppContextSetup(this.wac).build();
+        this.session = new MockHttpSession();
+    }
+
+    private TestProcess test(MockHttpServletRequestBuilder builder) {
+        return new TestProcess() {
+            @Override
+            public TestProcess setUp(TestProcessSetUp testProcessSetUp) {
+                testProcessSetUp.setUp(builder);
+                return this;
+            }
+
+            @Override
+            public TestResult exec() throws Exception {
+                return () -> mvc.perform(builder);
+            }
+        };
+    }
+
+    protected MockHttpServletRequestBuilder initDefaultSetting(MockHttpServletRequestBuilder builder) {
+        return builder.session(session).characterEncoding("UTF-8").contentType(MediaType.APPLICATION_JSON);
+    }
+
+    protected TestProcess testGet(String api) throws Exception {
+        MockHttpServletRequestBuilder msrb = initDefaultSetting(get(api));
+        return test(msrb);
+    }
+
+    protected TestProcess testPost(String api) throws Exception {
+        MockHttpServletRequestBuilder msrb = initDefaultSetting(post(api));
+        return test(msrb);
+    }
+
+    protected TestProcess testDelete(String api) throws Exception {
+        MockHttpServletRequestBuilder msrb = initDefaultSetting(delete(api));
+        return test(msrb);
+    }
+
+
+    protected TestProcess testPut(String api) throws Exception {
+        MockHttpServletRequestBuilder msrb = initDefaultSetting(put(api));
+        return test(msrb);
+    }
+
     @Configuration
     @SpringBootApplication
+    @WebAppConfiguration
     public static class Config {
 
         @Bean(name = "validator")

+ 8 - 0
hsweb-tests/src/main/java/org/hswebframework/web/tests/TestProcess.java

@@ -0,0 +1,8 @@
+package org.hswebframework.web.tests;
+
+public interface TestProcess {
+    TestProcess setUp(TestProcessSetUp testProcessSetUp);
+
+    TestResult exec() throws Exception;
+
+}

+ 7 - 0
hsweb-tests/src/main/java/org/hswebframework/web/tests/TestProcessSetUp.java

@@ -0,0 +1,7 @@
+package org.hswebframework.web.tests;
+
+import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
+
+public interface TestProcessSetUp {
+    void setUp(MockHttpServletRequestBuilder requestBuilder);
+}

+ 30 - 0
hsweb-tests/src/main/java/org/hswebframework/web/tests/TestResult.java

@@ -0,0 +1,30 @@
+package org.hswebframework.web.tests;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import org.springframework.test.web.servlet.ResultActions;
+
+/**
+ * @author zhouhao
+ * @TODO
+ */
+public interface TestResult {
+    ResultActions getResultActions() throws Exception;
+
+    default String resultAsString() {
+        try {
+            return getResultActions().andReturn().getResponse().getContentAsString();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    default JSONObject resultAsJson() {
+        return JSON.parseObject(resultAsString());
+    }
+
+    default JSONArray resultAsJsonArray() {
+        return JSON.parseArray(resultAsString());
+    }
+}