├── .gitignore ├── README.md ├── cobertura.bat ├── local_unitreport.bat ├── pom.xml ├── sample-test ├── .gitignore ├── README.md ├── build.bat ├── clover.bat ├── cobertura.bat ├── pom-clover.xml ├── pom-cobertura.xml ├── pom-jacoco.xml ├── pom.xml ├── show_test.bat └── src │ ├── main │ ├── java │ │ ├── config │ │ │ └── cache │ │ │ │ └── CacheConfig.java │ │ ├── controller │ │ │ ├── ErrorController.java │ │ │ ├── SimpleController.java │ │ │ ├── login │ │ │ │ └── LoginController.java │ │ │ └── order │ │ │ │ └── OrderController.java │ │ ├── dao │ │ │ └── user │ │ │ │ └── UserMapper.java │ │ ├── model │ │ │ ├── order │ │ │ │ └── Order.java │ │ │ └── user │ │ │ │ └── User.java │ │ ├── service │ │ │ └── user │ │ │ │ ├── UserService.java │ │ │ │ └── impl │ │ │ │ └── UserServiceImpl.java │ │ └── util │ │ │ └── SpringInitByWeb.java │ ├── resources │ │ ├── applicationContext.xml │ │ ├── cache │ │ │ └── spring-cache.xml │ │ ├── dao │ │ │ ├── dao.xml │ │ │ └── datasource-prod.xml │ │ ├── dispatcher-servlet.xml │ │ └── service │ │ │ └── spring-service.xml │ └── webapp │ │ ├── WEB-INF │ │ ├── decorators.xml │ │ ├── ftl │ │ │ ├── login.ftl │ │ │ └── order │ │ │ │ ├── add.ftl │ │ │ │ └── index.ftl │ │ ├── layouts │ │ │ ├── default.jsp │ │ │ ├── footer.jsp │ │ │ ├── header.jsp │ │ │ ├── login_template.jsp │ │ │ └── order_tpl.jsp │ │ └── web.xml │ │ ├── index.jsp │ │ └── static │ │ └── js │ │ └── jquery │ │ ├── jquery-1.9.1.min.js │ │ ├── jquery.cookie.js │ │ └── jquery.min.js │ └── test │ ├── java │ ├── app │ │ ├── AppTest.java │ │ ├── Application.java │ │ ├── HomeController.java │ │ ├── JettryRun.java │ │ └── TestApplicationContext.java │ ├── config │ │ └── cache │ │ │ ├── CacheConfigDev.java │ │ │ └── CacheManagerTest.java │ ├── controller │ │ ├── BaseContorllerMockTest.java │ │ ├── BaseContorllerTest.java │ │ ├── ErrorControllerTest.java │ │ ├── SimpleControllerTest.java │ │ └── login │ │ │ ├── LoginControllerMockTest.java │ │ │ └── LoginControllerTest.java │ ├── dao │ │ ├── AbstractDbBaseExporter.java │ │ ├── BaseDaoTest.java │ │ ├── BaseDbUnitTest.java │ │ ├── SampleDbExporter.java │ │ └── user │ │ │ ├── UserMapperDbUnitTest.java │ │ │ └── UserMapperTest.java │ ├── service │ │ ├── BaseTest.java │ │ ├── MockBaseTest.java │ │ └── user │ │ │ ├── UserCacheTest.java │ │ │ ├── UserServiceImplTest.java │ │ │ └── UserServiceImplWithMockTest.java │ └── test │ │ ├── AppTest.java │ │ ├── dbunit │ │ └── SampleTest.java │ │ ├── junit │ │ ├── Fibonacci.java │ │ ├── FibonacciTest.java │ │ ├── TestAllFetures.java │ │ └── TestMethodOrder.java │ │ ├── mock │ │ ├── AFinalClass.java │ │ ├── AStaticClass.java │ │ ├── ClassUnderTest.java │ │ ├── MockStaticTest.java │ │ ├── MockTest.java │ │ ├── MockTest2.java │ │ └── MyInterface.java │ │ ├── selenium │ │ ├── BaiduTestByTestNg.java │ │ ├── BaiduWebTest.java │ │ ├── BaiduWebTest2.java │ │ └── BaiduWebTest3.java │ │ ├── spring │ │ ├── AnnotaionTest.java │ │ ├── IfProfileTest.java │ │ └── ProfileTest.java │ │ ├── testframework │ │ └── DataProviderTest.java │ │ ├── testng │ │ ├── FindNumbers.java │ │ └── TestAllFetures.java │ │ └── utilstest │ │ ├── User.java │ │ └── UtilsTest.java │ └── resources │ ├── clover.license │ ├── config │ └── application.properties │ ├── controller │ └── LoginController │ │ └── testLogin.csv │ ├── dao │ ├── datasource-dev.xml │ ├── init-data.xml │ ├── mockdao.xml │ └── user │ │ ├── Users.xml │ │ ├── Users2.xml │ │ └── left.xml │ ├── data │ ├── schema.sql │ └── test-data.sql │ ├── service │ ├── UserService │ │ └── testCalRank.csv │ └── mockservice.xml │ └── testng.xml └── show_test.bat /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.iml 3 | .clover 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sample-test 2 | 3 | [![Join the chat at https://gitter.im/superproxy/sample-test](https://badges.gitter.im/superproxy/sample-test.svg)](https://gitter.im/superproxy/sample-test?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 4 | 5 | ##单元测试示例
6 | 7 | testng和单元覆盖工具集成
8 | testng和spring test集成
9 | mockito和spring mvc mock对象集成
10 | controller service dao 单元测试和集成测试的方法
11 | 添加csv和json测试文件支持
12 | 13 | 14 | ###使用 15 | 1. 本地查看
16 | 执行local_unitreport.bat
17 | 在target/surefire-reports中查看测试结果
18 | 在target/site/cobertura中查看测试覆盖率
19 | 20 | 2. sonar集成查看
21 | 下载sonar
22 | 下载单元测试覆盖率插件cobertura
23 | http://docs.sonarqube.org/display/PLUG/Cobertura+Plugin
24 | 25 | 运行 cobertura.bat,在sonar上查看结果
26 | -------------------------------------------------------------------------------- /cobertura.bat: -------------------------------------------------------------------------------- 1 | mvn -Dmaven.test.failure.ignore=true -Dcobertura.report.format=xml clean cobertura:cobertura sonar:sonar 2 | 3 | rem mvn -Dmaven.test.failure.ignore=true clean cobertura:cobertura -Dcobertura.report.format=xml sonar:sonar 4 | 5 | rem mvn -Dmaven.test.skip clean compile cobertura:cobertura -Dcobertura.report.format=xml install sonar:sonar -------------------------------------------------------------------------------- /local_unitreport.bat: -------------------------------------------------------------------------------- 1 | mvn -Dmaven.test.failure.ignore=true clean cobertura:cobertura -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.springframework.boot 8 | spring-boot-starter-parent 9 | 10 | 1.2.6.RELEASE 11 | 12 | sample 13 | sample-parent 14 | 1.0-SNAPSHOT 15 | pom 16 | 17 | 18 | sample-test 19 | 20 | 21 | 22 | java 23 | cobertura 24 | UTF-8 25 | UTF-8 26 | target/surefire-reports/junitreports 27 | reuseReports 28 | 29 | 30 | 31 | target/site/cobertura/coverage.xml 32 | 33 | 1.6.2 34 | 4.1.7.RELEASE 35 | 36 | 1.1.3 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-dependencies 45 | 1.2.6.RELEASE 46 | pom 47 | import 48 | 49 | 50 | org.testng 51 | testng 52 | 6.1.1 53 | 54 | 55 | org.apache.commons 56 | commons-lang3 57 | 3.4 58 | 59 | 60 | 61 | org.slf4j 62 | slf4j-api 63 | 1.6.2 64 | 65 | 66 | org.slf4j 67 | jcl-over-slf4j 68 | 1.6.2 69 | 70 | 71 | ch.qos.logback 72 | logback-core 73 | ${logback.version} 74 | 75 | 76 | ch.qos.logback 77 | logback-classic 78 | ${logback.version} 79 | 80 | 81 | 82 | com.h2database 83 | h2 84 | 1.4.178 85 | 86 | 87 | org.springframework 88 | spring-context 89 | ${spring.version} 90 | 91 | 92 | org.springframework 93 | spring-beans 94 | ${spring.version} 95 | 96 | 97 | org.springframework 98 | spring-context-support 99 | ${spring.version} 100 | 101 | 102 | org.springframework 103 | spring-orm 104 | ${spring.version} 105 | 106 | 107 | org.springframework 108 | spring-webmvc 109 | ${spring.version} 110 | 111 | 112 | com.alibaba 113 | druid 114 | 1.0.2 115 | 116 | 117 | 118 | org.mybatis 119 | mybatis 120 | 3.2.2 121 | 122 | 123 | org.mybatis 124 | mybatis-spring 125 | 1.2.0 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | org.apache.geronimo.specs 139 | geronimo-servlet_3.0_spec 140 | 1.0 141 | 142 | 143 | 144 | javax.servlet 145 | jstl 146 | 1.2 147 | provided 148 | 149 | 150 | 151 | opensymphony 152 | sitemesh 153 | 2.4.2 154 | 155 | 156 | 157 | org.freemarker 158 | freemarker 159 | 2.3.15 160 | 161 | 162 | org.springframework 163 | spring-test 164 | ${spring.version} 165 | 166 | 167 | 168 | com.alibaba 169 | fastjson 170 | 1.2.5 171 | 172 | 173 | 174 | 175 | javassist 176 | javassist 177 | 3.12.1.GA 178 | 179 | 180 | org.mockito 181 | mockito-all 182 | 1.10.19 183 | 184 | 185 | 186 | org.powermock 187 | powermock-api-mockito 188 | ${powermock.version} 189 | 190 | 191 | org.powermock 192 | powermock-module-testng 193 | ${powermock.version} 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | org.apache.maven.plugins 202 | maven-surefire-plugin 203 | 2.18.1 204 | 205 | 206 | 207 | 208 | src/test/resources/testng.xml 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | org.codehaus.mojo 219 | cobertura-maven-plugin 220 | 2.6 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | Codehaus repository 233 | http://repository.codehaus.org/ 234 | 235 | 236 | 237 | 238 | -------------------------------------------------------------------------------- /sample-test/.gitignore: -------------------------------------------------------------------------------- 1 | .clover 2 | /target 3 | /.idea 4 | -------------------------------------------------------------------------------- /sample-test/README.md: -------------------------------------------------------------------------------- 1 | # sample-test 2 | 3 | ##单元测试示例
4 | 5 | testng和单元覆盖工具集成
6 | testng和spring test集成
7 | mockito和spring mvc mock对象集成
8 | controller service dao 单元测试和集成测试的方法
9 | 10 | 11 | ###2015/8/29 12 | 扩展了testng dataprovider的功能 13 | 添加cvs和json测试文件支持 14 | 15 | ###2015/8/30 16 | 添加powermock演示 17 | 18 | 19 | ###2015/8/31 20 | TODO:添加性能测试演示 21 | 22 | 23 | -------------------------------------------------------------------------------- /sample-test/build.bat: -------------------------------------------------------------------------------- 1 | rem mvn clean install 2 | mvn clean clover2:setup test clover2:aggregate clover2:clover 3 | mvn sonar:sonar -------------------------------------------------------------------------------- /sample-test/clover.bat: -------------------------------------------------------------------------------- 1 | rem mvn clean install 2 | mvn clean clover2:setup test clover2:aggregate clover2:clover 3 | mvn sonar:sonar -------------------------------------------------------------------------------- /sample-test/cobertura.bat: -------------------------------------------------------------------------------- 1 | mvn -Dmaven.test.failure.ignore=true -Dcobertura.report.format=xml clean cobertura:cobertura sonar:sonar 2 | 3 | rem mvn -Dmaven.test.failure.ignore=true clean cobertura:cobertura -Dcobertura.report.format=xml sonar:sonar 4 | 5 | rem mvn -Dmaven.test.skip clean compile cobertura:cobertura -Dcobertura.report.format=xml install sonar:sonar -------------------------------------------------------------------------------- /sample-test/pom-clover.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | sample 8 | sample-test 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 4.0.5 13 | java 14 | clover 15 | UTF-8 16 | UTF-8 17 | target/surefire-reports/junitreports 18 | reuseReports 19 | target/site/clover/clover.xml 20 | 21 | 22 | 23 | org.testng 24 | testng 25 | 6.1.1 26 | test 27 | 28 | 29 | 30 | org.slf4j 31 | slf4j-api 32 | 1.6.2 33 | 34 | 35 | org.slf4j 36 | jcl-over-slf4j 37 | 1.6.2 38 | 39 | 40 | ch.qos.logback 41 | logback-core 42 | 0.9.29 43 | 44 | 45 | ch.qos.logback 46 | logback-classic 47 | 0.9.29 48 | 49 | 50 | 51 | 52 | org.springframework 53 | spring-context 54 | 3.2.4.RELEASE 55 | 56 | 57 | 58 | org.springframework 59 | spring-webmvc 60 | 3.2.4.RELEASE 61 | 62 | 63 | 64 | com.h2database 65 | h2 66 | 1.4.178 67 | 68 | 69 | 70 | org.springframework 71 | spring-beans 72 | 3.2.4.RELEASE 73 | 74 | 75 | org.springframework 76 | spring-context-support 77 | 3.2.4.RELEASE 78 | 79 | 80 | org.springframework 81 | spring-orm 82 | 3.2.4.RELEASE 83 | 84 | 85 | com.alibaba 86 | druid 87 | 1.0.2 88 | 89 | 90 | org.springframework 91 | spring-test 92 | 3.2.4.RELEASE 93 | 94 | 95 | org.mybatis 96 | mybatis 97 | 3.2.2 98 | 99 | 100 | 101 | org.springframework 102 | spring-oxm 103 | 3.2.4.RELEASE 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | org.mybatis 114 | mybatis-spring 115 | 1.2.0 116 | 117 | 118 | 119 | org.apache.geronimo.specs 120 | geronimo-servlet_3.0_spec 121 | 1.0 122 | provided 123 | 124 | 125 | 126 | javax.servlet 127 | jstl 128 | 1.2 129 | provided 130 | 131 | 132 | 133 | opensymphony 134 | sitemesh 135 | 2.4.2 136 | 137 | 138 | 139 | org.freemarker 140 | freemarker 141 | 2.3.15 142 | 143 | 144 | 145 | com.alibaba 146 | fastjson 147 | 1.1.41 148 | 149 | 150 | 151 | 152 | org.mockito 153 | mockito-all 154 | 1.8.5 155 | test 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | org.apache.maven.plugins 164 | maven-surefire-plugin 165 | 2.18.1 166 | 167 | 168 | 169 | 170 | src/test/resources/testng.xml 171 | 172 | 173 | 174 | 175 | com.atlassian.maven.plugins 176 | maven-clover2-plugin 177 | ${clover.version} 178 | 179 | /src/test/resources/clover.license 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | com.atlassian.maven.plugins 191 | maven-clover2-plugin 192 | ${clover.version} 193 | 194 | /src/test/resources/clover.license 195 | 196 | 197 | 198 | 199 | 200 | 201 | Codehaus repository 202 | http://repository.codehaus.org/ 203 | 204 | 205 | 206 | -------------------------------------------------------------------------------- /sample-test/pom-cobertura.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | sn-test 8 | testng-test 9 | 1.0-SNAPSHOT 10 | 11 | 12 | java 13 | cobertura 14 | UTF-8 15 | UTF-8 16 | 17 | reuseReports 18 | target/cobertura/cobertura.ser 19 | 20 | 21 | 22 | org.testng 23 | testng 24 | 6.1.1 25 | test 26 | 27 | 28 | 29 | org.slf4j 30 | slf4j-api 31 | 1.6.2 32 | 33 | 34 | org.slf4j 35 | jcl-over-slf4j 36 | 1.6.2 37 | 38 | 39 | ch.qos.logback 40 | logback-core 41 | 0.9.29 42 | 43 | 44 | ch.qos.logback 45 | logback-classic 46 | 0.9.29 47 | 48 | 49 | 50 | 51 | org.springframework 52 | spring-context 53 | 3.2.4.RELEASE 54 | 55 | 56 | 57 | org.springframework 58 | spring-webmvc 59 | 3.2.4.RELEASE 60 | 61 | 62 | 63 | com.h2database 64 | h2 65 | 1.4.178 66 | 67 | 68 | 69 | org.springframework 70 | spring-beans 71 | 3.2.4.RELEASE 72 | 73 | 74 | org.springframework 75 | spring-context-support 76 | 3.2.4.RELEASE 77 | 78 | 79 | org.springframework 80 | spring-orm 81 | 3.2.4.RELEASE 82 | 83 | 84 | com.alibaba 85 | druid 86 | 1.0.2 87 | 88 | 89 | org.springframework 90 | spring-test 91 | 3.2.4.RELEASE 92 | 93 | 94 | org.mybatis 95 | mybatis 96 | 3.2.2 97 | 98 | 99 | 100 | org.springframework 101 | spring-oxm 102 | 3.2.4.RELEASE 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | org.mybatis 113 | mybatis-spring 114 | 1.2.0 115 | 116 | 117 | 118 | javaee 119 | javaee-api 120 | 5 121 | provided 122 | 123 | 124 | 125 | javax.servlet 126 | jstl 127 | 1.2 128 | 129 | 130 | 131 | opensymphony 132 | sitemesh 133 | 2.4.2 134 | 135 | 136 | 137 | org.freemarker 138 | freemarker 139 | 2.3.15 140 | 141 | 142 | 143 | com.alibaba 144 | fastjson 145 | 1.1.41 146 | 147 | 148 | 149 | 150 | org.mockito 151 | mockito-all 152 | 1.8.5 153 | test 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | org.apache.maven.plugins 162 | maven-surefire-plugin 163 | 2.18.1 164 | 165 | methods 166 | 1 167 | 168 | src/test/resources/testng.xml 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | org.codehaus.mojo 180 | cobertura-maven-plugin 181 | 2.6 182 | 183 | 184 | html 185 | xml 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | Codehaus repository 194 | http://repository.codehaus.org/ 195 | 196 | 197 | 198 | -------------------------------------------------------------------------------- /sample-test/pom-jacoco.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | sn-test 8 | testng-test 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | org.testng 21 | testng 22 | 6.1.1 23 | test 24 | 25 | 26 | 27 | org.slf4j 28 | slf4j-api 29 | 1.6.2 30 | 31 | 32 | org.slf4j 33 | jcl-over-slf4j 34 | 1.6.2 35 | 36 | 37 | ch.qos.logback 38 | logback-core 39 | 0.9.29 40 | 41 | 42 | ch.qos.logback 43 | logback-classic 44 | 0.9.29 45 | 46 | 47 | 48 | org.jacoco 49 | org.jacoco.agent 50 | runtime 51 | 0.5.3.201107060350 52 | test 53 | 54 | 55 | 56 | 57 | 58 | 59 | org.apache.maven.plugins 60 | maven-surefire-plugin 61 | 2.18.1 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | target/jacoco.exec 72 | 73 | 74 | 75 | 76 | org.jacoco 77 | jacoco-maven-plugin 78 | 0.5.3.201107060350 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | org.jacoco 91 | jacoco-maven-plugin 92 | 0.5.3.201107060350 93 | 94 | 95 | org.codehaus.mojo 96 | cobertura-maven-plugin 97 | 2.7 98 | 99 | 100 | 101 | 102 | 103 | Codehaus repository 104 | http://repository.codehaus.org/ 105 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /sample-test/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | sample 7 | sample-parent 8 | 1.0-SNAPSHOT 9 | 10 | 11 | 4.0.0 12 | sample 13 | sample-test 14 | 1.0-SNAPSHOT 15 | war 16 | 17 | 18 | 2.5.1 19 | java 20 | cobertura 21 | UTF-8 22 | UTF-8 23 | target/surefire-reports/junitreports 24 | reuseReports 25 | 26 | 27 | 28 | target/site/cobertura/coverage.xml 29 | 30 | 31 | 32 | 33 | com.github.superproxy 34 | test-data-provider 35 | 0.1.0 36 | test 37 | 38 | 39 | 40 | org.seleniumhq.selenium 41 | selenium-java 42 | 2.47.1 43 | test 44 | 45 | 46 | 47 | net.sf.ehcache 48 | ehcache 49 | 2.10.2 50 | 51 | 52 | net.sf.ehcache 53 | ehcache-core 54 | 2.6.11 55 | 56 | 57 | 58 | 59 | com.github.springtestdbunit 60 | spring-test-dbunit 61 | 1.0.1 62 | test 63 | 64 | 65 | 66 | org.testng 67 | testng 68 | test 69 | 70 | 71 | 72 | 73 | org.dbunit 74 | dbunit 75 | ${dbunitVersion} 76 | test 77 | 78 | 79 | 80 | org.powermock 81 | powermock-api-mockito 82 | test 83 | 84 | 85 | org.powermock 86 | powermock-module-testng 87 | test 88 | 89 | 90 | org.apache.commons 91 | commons-lang3 92 | 93 | 94 | 95 | org.slf4j 96 | slf4j-api 97 | 98 | 99 | org.slf4j 100 | jcl-over-slf4j 101 | 102 | 103 | ch.qos.logback 104 | logback-core 105 | 106 | 107 | ch.qos.logback 108 | logback-classic 109 | 110 | 111 | 112 | 113 | org.springframework 114 | spring-context 115 | 116 | 117 | 118 | org.springframework 119 | spring-webmvc 120 | 121 | 122 | 123 | com.h2database 124 | h2 125 | 126 | 127 | 128 | org.springframework 129 | spring-beans 130 | 131 | 132 | org.springframework 133 | spring-context-support 134 | 135 | 136 | org.springframework 137 | spring-orm 138 | 139 | 140 | com.alibaba 141 | druid 142 | 143 | 144 | 145 | org.mybatis 146 | mybatis 147 | 148 | 149 | 150 | org.mybatis 151 | mybatis-spring 152 | 153 | 154 | 155 | org.unitils 156 | unitils-core 157 | 3.4.2 158 | test 159 | 160 | 161 | 162 | 163 | junit 164 | junit 165 | 4.12 166 | test 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | org.apache.geronimo.specs 180 | geronimo-servlet_3.0_spec 181 | provided 182 | 183 | 184 | 185 | javax.servlet 186 | jstl 187 | provided 188 | 189 | 190 | 191 | opensymphony 192 | sitemesh 193 | 194 | 195 | 196 | org.freemarker 197 | freemarker 198 | 199 | 200 | 201 | org.mockito 202 | mockito-all 203 | test 204 | 205 | 206 | org.springframework 207 | spring-test 208 | 209 | 210 | com.alibaba 211 | fastjson 212 | 213 | 214 | org.springframework.boot 215 | spring-boot-autoconfigure 216 | 1.2.6.RELEASE 217 | test 218 | 219 | 220 | org.eclipse.jetty 221 | jetty-server 222 | 9.3.5.v20151012 223 | test 224 | 225 | 226 | org.mortbay.jetty 227 | jetty 228 | 6.1.25 229 | test 230 | 231 | 232 | org.eclipse.jetty 233 | jetty-http 234 | 9.3.5.v20151012 235 | test 236 | 237 | 238 | org.eclipse.jetty 239 | jetty-util 240 | 9.3.5.v20151012 241 | test 242 | 243 | 244 | org.eclipse.jetty 245 | jetty-util 246 | 9.2.13.v20150730 247 | test 248 | 249 | 250 | 251 | -------------------------------------------------------------------------------- /sample-test/show_test.bat: -------------------------------------------------------------------------------- 1 | mvn test 2 | 3 | mvn sonar:sonar -------------------------------------------------------------------------------- /sample-test/src/main/java/config/cache/CacheConfig.java: -------------------------------------------------------------------------------- 1 | package config.cache; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.cache.CacheManager; 6 | import org.springframework.cache.ehcache.EhCacheCacheManager; 7 | import org.springframework.cache.ehcache.EhCacheManagerFactoryBean; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | import org.springframework.core.io.ClassPathResource; 11 | 12 | @Configuration 13 | //@Profile("prod") 14 | public class CacheConfig { 15 | 16 | private static final Logger log = LoggerFactory.getLogger(CacheConfig.class); 17 | 18 | @Bean 19 | public CacheManager cacheManager() { 20 | log.debug("Cache manager is ehCacheCacheManager"); 21 | return new EhCacheCacheManager(ehCacheCacheManager().getObject()); 22 | } 23 | 24 | @Bean 25 | public EhCacheManagerFactoryBean ehCacheCacheManager() { 26 | EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean(); 27 | cmfb.setConfigLocation(new ClassPathResource("ehcache.xml")); 28 | cmfb.setShared(true); 29 | return cmfb; 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /sample-test/src/main/java/controller/ErrorController.java: -------------------------------------------------------------------------------- 1 | package controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | 6 | import javax.servlet.http.HttpServletRequest; 7 | 8 | @Controller 9 | @RequestMapping("/") 10 | public class ErrorController { 11 | 12 | @RequestMapping("error") 13 | public String error(HttpServletRequest request) { 14 | return "error.ftl"; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /sample-test/src/main/java/controller/SimpleController.java: -------------------------------------------------------------------------------- 1 | package controller; 2 | 3 | import org.springframework.http.HttpHeaders; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.http.MediaType; 6 | import org.springframework.http.ResponseEntity; 7 | import org.springframework.stereotype.Controller; 8 | import org.springframework.ui.Model; 9 | import org.springframework.ui.ModelMap; 10 | import org.springframework.web.bind.annotation.RequestMapping; 11 | import org.springframework.web.bind.annotation.RequestMethod; 12 | import org.springframework.web.bind.annotation.ResponseBody; 13 | import org.springframework.web.servlet.ModelAndView; 14 | 15 | import javax.servlet.http.HttpServletRequest; 16 | import javax.servlet.http.HttpServletResponse; 17 | import java.io.IOException; 18 | import java.io.PrintWriter; 19 | 20 | @Controller 21 | public class SimpleController { 22 | 23 | @RequestMapping(value = "/testModelAndView", method = RequestMethod.GET) 24 | public ModelAndView testModelAndView() { 25 | ModelAndView modelAndView = new ModelAndView("test2"); 26 | modelAndView.addObject("ss"); 27 | return modelAndView; 28 | } 29 | 30 | @RequestMapping(value = "/testVoid", method = RequestMethod.GET) 31 | public void testVoid(HttpServletRequest request, HttpServletResponse response) throws IOException { 32 | try { 33 | PrintWriter writer = response.getWriter(); 34 | writer.write("hello"); 35 | writer.flush(); 36 | writer.close(); 37 | } catch (IOException e) { 38 | e.printStackTrace(); 39 | } 40 | } 41 | 42 | @RequestMapping(value = "/testHello", method = RequestMethod.GET) 43 | public String testHello(String msg, Model model) { 44 | model.addAttribute("msg", "hi"); 45 | return "index"; 46 | } 47 | 48 | @RequestMapping(value = "/testEcho", method = RequestMethod.GET) 49 | public String testEcho(String msg, ModelMap model) { 50 | model.addAttribute("msg", msg); 51 | return "index"; 52 | } 53 | 54 | 55 | @RequestMapping(value = "/testResponseBody", method = RequestMethod.GET) 56 | @ResponseBody 57 | public String testResponseBody() { 58 | return "{json:\"abc\"}"; 59 | } 60 | 61 | @RequestMapping(value = "/testReponseEntity", method = RequestMethod.GET) 62 | public ResponseEntity testReponseEntity() { 63 | HttpHeaders headers = new HttpHeaders(); 64 | headers.setContentType(MediaType.TEXT_PLAIN); 65 | return new ResponseEntity("The String ResponseBody with custom header Content-Type=text/plain", 66 | headers, HttpStatus.OK); 67 | } 68 | 69 | 70 | } 71 | -------------------------------------------------------------------------------- /sample-test/src/main/java/controller/login/LoginController.java: -------------------------------------------------------------------------------- 1 | package controller.login; 2 | 3 | import model.user.User; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.ui.ModelMap; 7 | import org.springframework.util.StringUtils; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RequestMethod; 10 | import service.user.UserService; 11 | 12 | import javax.servlet.http.HttpServletRequest; 13 | import javax.servlet.http.HttpServletResponse; 14 | 15 | /** 16 | * 登陆服务 17 | */ 18 | @Controller 19 | public class LoginController { 20 | 21 | @Autowired 22 | private UserService userService; 23 | 24 | 25 | /** 26 | * 登录页面 27 | * 28 | * @return 29 | */ 30 | @RequestMapping(value = "/login", method = RequestMethod.GET) 31 | public String login() { 32 | return "login"; 33 | } 34 | 35 | /** 36 | * 登录验证 37 | * 38 | * @param userName 39 | * @param password 40 | * @param validate 41 | * @param request 42 | * @return 43 | */ 44 | @RequestMapping("/logon") 45 | public String logon(String userName, String password, String validate, 46 | HttpServletRequest request, 47 | HttpServletResponse response, ModelMap model) { 48 | model.addAttribute("userName", userName); 49 | if (StringUtils.isEmpty(validate)) { 50 | model.addAttribute("message", "验证码不能为空"); 51 | return "login"; 52 | } 53 | 54 | if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) { 55 | model.addAttribute("message", "用户名或密码不能为空"); 56 | return "login"; 57 | } 58 | 59 | User user; 60 | try { 61 | user = userService.queryUser(userName, password); 62 | } catch (Exception e) { 63 | return "error"; 64 | } 65 | if (user != null) { 66 | return "index"; 67 | } else { 68 | model.addAttribute("message", "用户名或密码不正确"); 69 | return "login"; 70 | } 71 | } 72 | } -------------------------------------------------------------------------------- /sample-test/src/main/java/controller/order/OrderController.java: -------------------------------------------------------------------------------- 1 | package controller.order; 2 | 3 | import model.order.Order; 4 | import org.springframework.stereotype.Controller; 5 | import org.springframework.ui.Model; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | 8 | import javax.servlet.http.HttpServletRequest; 9 | 10 | @Controller 11 | public class OrderController { 12 | 13 | @RequestMapping("/order/create") 14 | public String addOrder(Model model, String item, HttpServletRequest request) { 15 | Object order = request.getSession().getAttribute("order"); 16 | if (order == null) { 17 | order = new Order(); 18 | request.getSession().setAttribute("order", order); 19 | } 20 | ((Order) order).addItem(item); 21 | return "order/index"; 22 | } 23 | 24 | 25 | /** 26 | * 界面 27 | * 28 | * @return 29 | */ 30 | @RequestMapping("/order/") 31 | public String index() { 32 | return "order/index"; 33 | } 34 | 35 | 36 | /** 37 | * 界面 38 | * 39 | * @return 40 | */ 41 | @RequestMapping("/order/add") 42 | public String add() { 43 | return "order/add"; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /sample-test/src/main/java/dao/user/UserMapper.java: -------------------------------------------------------------------------------- 1 | package dao.user; 2 | 3 | import model.user.User; 4 | import org.apache.ibatis.annotations.*; 5 | 6 | import java.util.List; 7 | 8 | public interface UserMapper { 9 | @Select("SELECT * FROM users WHERE name = #{username} AND password=#{password}") 10 | User query(@Param("username") 11 | String userName, @Param("password") 12 | String password); 13 | @Select("SELECT * FROM users WHERE name = #{username}") 14 | User queryByUserName(@Param("username") 15 | String userName); 16 | 17 | @Update("UPDATE users SET age = #{age} WHERE name = #{name}") 18 | int update(User user); 19 | 20 | @Select("SELECT * FROM users") 21 | List getAllUsers(); 22 | 23 | @Delete("DELETE FROM users WHERE name = #{username}") 24 | int delete(@Param("username") String userName); 25 | 26 | @Insert("INSERT INTO users(name, password, age) VALUES(#{name}, #{password}, #{age})") 27 | int create(User user); 28 | } 29 | -------------------------------------------------------------------------------- /sample-test/src/main/java/model/order/Order.java: -------------------------------------------------------------------------------- 1 | package model.order; 2 | 3 | import java.io.Serializable; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | public class Order implements Serializable { 8 | 9 | private List items = new ArrayList(); 10 | 11 | 12 | public void addItem(String item) { 13 | items.add(item); 14 | } 15 | 16 | public String[] getItems() { 17 | return items.toArray(new String[0]); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /sample-test/src/main/java/model/user/User.java: -------------------------------------------------------------------------------- 1 | package model.user; 2 | 3 | import org.apache.commons.lang3.builder.ToStringBuilder; 4 | 5 | import java.io.Serializable; 6 | 7 | public class User implements Serializable { 8 | private String name; 9 | private String password; 10 | private int age; 11 | private int rank; 12 | 13 | public User(String name, String password, int age) { 14 | this.age = age; 15 | this.name = name; 16 | this.password = password; 17 | } 18 | 19 | public User(String name, String password) { 20 | this.name = name; 21 | this.password = password; 22 | } 23 | 24 | 25 | public int getRank() { 26 | return rank; 27 | } 28 | 29 | public void setRank(int rank) { 30 | this.rank = rank; 31 | } 32 | 33 | public String getPassword() { 34 | return password; 35 | } 36 | 37 | public void setPassword(String password) { 38 | this.password = password; 39 | } 40 | 41 | public User(int age, String name) { 42 | this.age = age; 43 | this.name = name; 44 | } 45 | 46 | public User() { 47 | } 48 | 49 | public int getAge() { 50 | return age; 51 | } 52 | 53 | public void setAge(int age) { 54 | this.age = age; 55 | } 56 | 57 | public String getName() { 58 | return name; 59 | } 60 | 61 | public void setName(String name) { 62 | this.name = name; 63 | } 64 | 65 | @Override 66 | public String toString() { 67 | return ToStringBuilder.reflectionToString(this); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /sample-test/src/main/java/service/user/UserService.java: -------------------------------------------------------------------------------- 1 | package service.user; 2 | 3 | import model.user.User; 4 | 5 | public interface UserService { 6 | User queryUser(String userName, String password) throws Exception; 7 | 8 | User queryUser(String userName); 9 | 10 | boolean addUser(User user); 11 | boolean updateUser(User user); 12 | 13 | /** 14 | * 计算用户等级 15 | * 16 | * @param user 用户 17 | * @return 返回用户等级,数字表示 18 | */ 19 | int calRank(User user); 20 | } 21 | -------------------------------------------------------------------------------- /sample-test/src/main/java/service/user/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package service.user.impl; 2 | 3 | import cache.CommonCacheManager; 4 | import dao.user.UserMapper; 5 | import model.user.User; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.cache.CacheManager; 8 | import org.springframework.cache.annotation.CacheConfig; 9 | import org.springframework.cache.annotation.CacheEvict; 10 | import org.springframework.cache.annotation.Cacheable; 11 | import org.springframework.stereotype.Service; 12 | import service.user.UserService; 13 | 14 | import javax.annotation.Resource; 15 | 16 | @CacheConfig(cacheNames = {"test"}) 17 | @Service 18 | public class UserServiceImpl implements UserService, CommonCacheManager { 19 | 20 | @Resource 21 | private UserMapper userMapper; 22 | 23 | @Autowired 24 | private CacheManager cacheManager; 25 | 26 | @Override 27 | public boolean addUser(User user) { 28 | return userMapper.create(user) > 0; 29 | } 30 | 31 | @Override 32 | public int calRank(User user) { 33 | if (user.getAge() < 10) { 34 | return 0; 35 | } 36 | 37 | if (user.getAge() < 20) { 38 | return 1; 39 | } 40 | 41 | if (user.getAge() < 30) { 42 | return 2; 43 | } 44 | 45 | return 3; 46 | } 47 | 48 | @Override 49 | public User queryUser(String userName, String password) { 50 | return userMapper.query(userName, password); 51 | } 52 | 53 | @Override 54 | @Cacheable(key = "'user_'+#userName") 55 | public User queryUser(String userName) { 56 | System.out.println("query user"); 57 | return userMapper.queryByUserName(userName); 58 | } 59 | 60 | @Override 61 | @CacheEvict(value = "test", key = "'user_'+#user.name") 62 | public boolean updateUser(User user) { 63 | return userMapper.update(user) > 0; 64 | } 65 | 66 | @Override 67 | public void removeCache(String key) { 68 | cacheManager.getCache("test").evict(key); 69 | } 70 | 71 | @Override 72 | public void addCache(String key, User object) { 73 | cacheManager.getCache("test").put(key, object); 74 | } 75 | 76 | @Override 77 | public User getCache(String key) { 78 | return (User) cacheManager.getCache("test").get(key); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /sample-test/src/main/java/util/SpringInitByWeb.java: -------------------------------------------------------------------------------- 1 | package util; 2 | 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.web.context.WebApplicationContext; 5 | import org.springframework.web.context.support.WebApplicationContextUtils; 6 | 7 | import javax.servlet.ServletContextEvent; 8 | import javax.servlet.ServletContextListener; 9 | 10 | public class SpringInitByWeb implements ServletContextListener { 11 | private static WebApplicationContext springContext; 12 | 13 | public static ApplicationContext getApplicationContext() { 14 | return springContext; 15 | } 16 | 17 | public void contextInitialized(ServletContextEvent event) { 18 | springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext()); 19 | } 20 | 21 | public void contextDestroyed(ServletContextEvent event) { 22 | } 23 | 24 | 25 | } 26 | -------------------------------------------------------------------------------- /sample-test/src/main/resources/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /sample-test/src/main/resources/cache/spring-cache.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /sample-test/src/main/resources/dao/dao.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /sample-test/src/main/resources/dao/datasource-prod.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | java:comp/env/sample-test 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /sample-test/src/main/resources/dispatcher-servlet.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 25 | 26 | 27 | 28 | 0 29 | UTF-8 30 | 0.########## 31 | yyyy-MM-dd HH:mm:ss 32 | true 33 | ignore 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /sample-test/src/main/resources/service/spring-service.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /sample-test/src/main/webapp/WEB-INF/decorators.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | /static/* 5 | /common/* 6 | 7 | 8 | 9 | /login 10 | /logon 11 | /logout 12 | 13 | 14 | /order/* 15 | 16 | 17 | 18 | /* 19 | 20 | 21 | -------------------------------------------------------------------------------- /sample-test/src/main/webapp/WEB-INF/ftl/login.ftl: -------------------------------------------------------------------------------- 1 | 15 | 16 | -------------------------------------------------------------------------------- /sample-test/src/main/webapp/WEB-INF/ftl/order/add.ftl: -------------------------------------------------------------------------------- 1 |
2 | 7 | 8 |
-------------------------------------------------------------------------------- /sample-test/src/main/webapp/WEB-INF/ftl/order/index.ftl: -------------------------------------------------------------------------------- 1 | <#if Session["order"]?exists> 2 |

订单

3 | <#list Session["order"].items as item> 4 | ${item}
5 | 6 | 7 | -------------------------------------------------------------------------------- /sample-test/src/main/webapp/WEB-INF/layouts/default.jsp: -------------------------------------------------------------------------------- 1 | <%@ page contentType="text/html;charset=UTF-8" %> 2 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 3 | <%@ taglib prefix="sitemesh" 4 | uri="http://www.opensymphony.com/sitemesh/decorator" %> 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 16 | 17 | 18 | 19 | 20 | 23 | 24 | 25 | 26 | 27 | <%@ include file="/WEB-INF/layouts/footer.jsp" %> 28 | 29 | 30 | -------------------------------------------------------------------------------- /sample-test/src/main/webapp/WEB-INF/layouts/footer.jsp: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /sample-test/src/main/webapp/WEB-INF/layouts/header.jsp: -------------------------------------------------------------------------------- 1 | <%@ page contentType="text/html;charset=UTF-8" %> 2 | 首页 3 | -------------------------------------------------------------------------------- /sample-test/src/main/webapp/WEB-INF/layouts/login_template.jsp: -------------------------------------------------------------------------------- 1 | <%@ page contentType="text/html;charset=UTF-8" %> 2 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 3 | <%@ taglib prefix="sitemesh" 4 | uri="http://www.opensymphony.com/sitemesh/decorator" %> 5 | 6 | 7 | 8 | 9 | 10 | <sitemesh:title/> 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | <%@ include file="/WEB-INF/layouts/footer.jsp" %> 21 | 22 | 23 | -------------------------------------------------------------------------------- /sample-test/src/main/webapp/WEB-INF/layouts/order_tpl.jsp: -------------------------------------------------------------------------------- 1 | <%@ page contentType="text/html;charset=UTF-8" %> 2 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 3 | <%@ taglib prefix="sitemesh" 4 | uri="http://www.opensymphony.com/sitemesh/decorator" %> 5 | 6 | 7 | 8 | 9 | 10 | <sitemesh:title/> 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | <%@ include file="/WEB-INF/layouts/header.jsp" %> 20 | 21 | <%@ include file="/WEB-INF/layouts/footer.jsp" %> 22 | 23 | 24 | -------------------------------------------------------------------------------- /sample-test/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | DruidStatView 12 | com.alibaba.druid.support.http.StatViewServlet 13 | 14 | 15 | DruidStatView 16 | /druid/* 17 | 18 | 19 | 20 | contextConfigLocation 21 | classpath:applicationContext.xml 22 | 23 | 24 | 25 | org.springframework.web.context.ContextLoaderListener 26 | 27 | 28 | 29 | Set Character Encoding 30 | org.springframework.web.filter.CharacterEncodingFilter 31 | 32 | encoding 33 | UTF-8 34 | 35 | 36 | forceEncoding 37 | true 38 | 39 | 40 | 41 | Set Character Encoding 42 | /* 43 | 44 | 45 | 46 | dispatcher 47 | org.springframework.web.servlet.DispatcherServlet 48 | 49 | contextConfigLocation 50 | classpath:dispatcher-servlet.xml 51 | 52 | 1 53 | 54 | 55 | dispatcher 56 | / 57 | 58 | 59 | 60 | 61 | sitemesh 62 | com.opensymphony.sitemesh.webapp.SiteMeshFilter 63 | 64 | 65 | sitemesh 66 | /* 67 | 68 | 69 | 70 | 71 | authFilter 72 | filter.AuthFilter2 73 | 74 | logoutUrl 75 | http://www.yxz.com:8180/logout 76 | 77 | 78 | redirectUrl 79 | http://passport.yxz.com:8080/login?target= 80 | 81 | 82 | validateTicketUrl 83 | http://passport.yxz.com:8080/validate 84 | 85 | 86 | 87 | authFilter 88 | /* 89 | 90 | 91 | 92 | 93 | util.SpringInitByWeb 94 | 95 | 96 | 97 | 98 | / 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /sample-test/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Created by IntelliJ IDEA. 3 | User: 14120295 4 | Date: 2015/8/24 5 | Time: 10:56 6 | To change this template use File | Settings | File Templates. 7 | --%> 8 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /sample-test/src/main/webapp/static/js/jquery/jquery.cookie.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * jQuery Cookie Plugin v1.4.1 3 | * https://github.com/carhartl/jquery-cookie 4 | * 5 | * Copyright 2013 Klaus Hartl 6 | * Released under the MIT license 7 | */ 8 | (function (factory) { 9 | if (typeof define === 'function' && define.amd) { 10 | // AMD 11 | define(['jquery'], factory); 12 | } else if (typeof exports === 'object') { 13 | // CommonJS 14 | factory(require('jquery')); 15 | } else { 16 | // Browser globals 17 | factory(jQuery); 18 | } 19 | }(function ($) { 20 | 21 | var pluses = /\+/g; 22 | 23 | function encode(s) { 24 | return config.raw ? s : encodeURIComponent(s); 25 | } 26 | 27 | function decode(s) { 28 | return config.raw ? s : decodeURIComponent(s); 29 | } 30 | 31 | function stringifyCookieValue(value) { 32 | return encode(config.json ? JSON.stringify(value) : String(value)); 33 | } 34 | 35 | function parseCookieValue(s) { 36 | if (s.indexOf('"') === 0) { 37 | // This is test quoted cookie as according to RFC2068, unescape... 38 | s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\'); 39 | } 40 | 41 | try { 42 | // Replace server-side written pluses with spaces. 43 | // If we can't decode the cookie, ignore it, it's unusable. 44 | // If we can't parse the cookie, ignore it, it's unusable. 45 | s = decodeURIComponent(s.replace(pluses, ' ')); 46 | return config.json ? JSON.parse(s) : s; 47 | } catch (e) { 48 | } 49 | } 50 | 51 | function read(s, converter) { 52 | var value = config.raw ? s : parseCookieValue(s); 53 | return $.isFunction(converter) ? converter(value) : value; 54 | } 55 | 56 | var config = $.cookie = function (key, value, options) { 57 | 58 | // Write 59 | 60 | if (value !== undefined && !$.isFunction(value)) { 61 | options = $.extend({}, config.defaults, options); 62 | 63 | if (typeof options.expires === 'number') { 64 | var days = options.expires, t = options.expires = new Date(); 65 | t.setTime(+t + days * 864e+5); 66 | } 67 | 68 | return (document.cookie = [ 69 | encode(key), '=', stringifyCookieValue(value), 70 | options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE 71 | options.path ? '; path=' + options.path : '', 72 | options.domain ? '; domain=' + options.domain : '', 73 | options.secure ? '; secure' : '' 74 | ].join('')); 75 | } 76 | 77 | // Read 78 | 79 | var result = key ? undefined : {}; 80 | 81 | // To prevent the for loop in the first place assign an empty array 82 | // in case there are no cookies at all. Also prevents odd result when 83 | // calling $.cookie(). 84 | var cookies = document.cookie ? document.cookie.split('; ') : []; 85 | 86 | for (var i = 0, l = cookies.length; i < l; i++) { 87 | var parts = cookies[i].split('='); 88 | var name = decode(parts.shift()); 89 | var cookie = parts.join('='); 90 | 91 | if (key && key === name) { 92 | // If second argument (value) is test function it's test converter... 93 | result = read(cookie, value); 94 | break; 95 | } 96 | 97 | // Prevent storing test cookie that we couldn't decode. 98 | if (!key && (cookie = read(cookie)) !== undefined) { 99 | result[name] = cookie; 100 | } 101 | } 102 | 103 | return result; 104 | }; 105 | 106 | config.defaults = {}; 107 | 108 | $.removeCookie = function (key, options) { 109 | if ($.cookie(key) === undefined) { 110 | return false; 111 | } 112 | 113 | // Must not alter options, thus extending test fresh object... 114 | $.cookie(key, '', $.extend({}, options, {expires: -1})); 115 | return !$.cookie(key); 116 | }; 117 | 118 | })); 119 | -------------------------------------------------------------------------------- /sample-test/src/test/java/app/AppTest.java: -------------------------------------------------------------------------------- 1 | package app; 2 | 3 | import controller.SimpleController; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 6 | import org.springframework.boot.test.SpringApplicationConfiguration; 7 | import org.springframework.boot.test.WebIntegrationTest; 8 | import org.springframework.test.context.web.WebAppConfiguration; 9 | import org.testng.annotations.Test; 10 | import service.BaseTest; 11 | 12 | @EnableAutoConfiguration 13 | //@ImportXml("classpath:sbeans.xml") 14 | //@SpringApplicationConfiguration(classes = {HomeController.class, LoginController.class}) 15 | @SpringApplicationConfiguration(classes = {SimpleController.class}) 16 | @WebAppConfiguration 17 | @WebIntegrationTest 18 | @Test 19 | public class AppTest extends BaseTest { 20 | 21 | // /** 22 | // * 定制 EmbeddedServletContainerFactory 23 | // * 24 | // * @return 25 | // */ 26 | // @Bean 27 | // public EmbeddedServletContainerFactory servletContainer() { 28 | // TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); 29 | // factory.setPort(9000); 30 | // factory.setSessionTimeout(10, TimeUnit.MINUTES); 31 | //// factory.addErrorPages(new ErrorPage(HttpStatus.404, "/notfound.html")); 32 | // return factory; 33 | // } 34 | 35 | public static void main(String[] args) { 36 | // 必须是注解的类 37 | // Caused by: org.springframework.context.ApplicationContextException: 38 | // Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean. 39 | SpringApplication.run(AppTest.class, args); 40 | } 41 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/app/Application.java: -------------------------------------------------------------------------------- 1 | package app; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | 7 | // same as @Configuration @EnableAutoConfiguration @ComponentScan 8 | @SpringBootApplication 9 | public class Application { 10 | public static void main(String[] args) { 11 | SpringApplication.run(Application.class, args); 12 | } 13 | 14 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/app/HomeController.java: -------------------------------------------------------------------------------- 1 | package app; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 | import org.springframework.boot.test.IntegrationTest; 6 | import org.springframework.stereotype.Controller; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.ResponseBody; 9 | 10 | @EnableAutoConfiguration 11 | @IntegrationTest 12 | @Controller 13 | public class HomeController { 14 | 15 | @RequestMapping("/") 16 | @ResponseBody 17 | public String home() { 18 | return "s"; 19 | } 20 | 21 | public static void main(String[] args) { 22 | SpringApplication.run(HomeController.class, args); 23 | } 24 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/app/JettryRun.java: -------------------------------------------------------------------------------- 1 | package app; 2 | 3 | import org.eclipse.jetty.server.Server; 4 | import org.mortbay.jetty.webapp.WebAppContext; 5 | 6 | public class JettryRun { 7 | 8 | public static void main(String[] args) throws Exception { 9 | Server server = new Server(8080); 10 | WebAppContext context = new WebAppContext(); 11 | context.setContextPath("/myapp"); 12 | context.setDescriptor("/WEB-INF/web.xml"); 13 | context.setResourceBase("/"); 14 | context.setParentLoaderPriority(true); 15 | 16 | server.start(); 17 | server.join(); 18 | } 19 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/app/TestApplicationContext.java: -------------------------------------------------------------------------------- 1 | package app; 2 | 3 | import org.junit.runner.RunWith; 4 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 | import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; 6 | import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; 7 | import org.springframework.boot.test.IntegrationTest; 8 | import org.springframework.boot.test.SpringApplicationConfiguration; 9 | import org.springframework.context.annotation.Configuration; 10 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 11 | import org.springframework.test.context.web.WebAppConfiguration; 12 | 13 | import java.util.concurrent.TimeUnit; 14 | 15 | @RunWith(SpringJUnit4ClassRunner.class) 16 | @SpringApplicationConfiguration(classes = {HomeController.class}) 17 | @WebAppConfiguration 18 | @EnableAutoConfiguration 19 | @IntegrationTest 20 | @Configuration 21 | public class TestApplicationContext { 22 | 23 | // @Bean 24 | public EmbeddedServletContainerFactory servletContainer() { 25 | TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); 26 | factory.setPort(9000); 27 | factory.setSessionTimeout(10, TimeUnit.MINUTES); 28 | //factory.addErrorPages(new ErrorPage(HttpStatus.404, "/notfound.html")); 29 | return factory; 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/config/cache/CacheConfigDev.java: -------------------------------------------------------------------------------- 1 | package config.cache; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.cache.CacheManager; 6 | import org.springframework.cache.concurrent.ConcurrentMapCacheManager; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.Configuration; 9 | import org.springframework.context.annotation.Profile; 10 | 11 | @Configuration 12 | @Profile("dev") 13 | public class CacheConfigDev { 14 | 15 | private static final Logger log = LoggerFactory.getLogger(CacheConfigDev.class); 16 | 17 | @Bean 18 | public CacheManager concurrentMapCacheManager() { 19 | log.debug("Cache manager is concurrentMapCacheManager"); 20 | return new ConcurrentMapCacheManager("movieFindCache"); 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/config/cache/CacheManagerTest.java: -------------------------------------------------------------------------------- 1 | package config.cache; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.cache.Cache; 7 | import org.springframework.cache.CacheManager; 8 | import org.springframework.context.annotation.Configuration; 9 | import org.springframework.context.annotation.Import; 10 | import org.springframework.test.context.ActiveProfiles; 11 | import org.springframework.test.context.ContextConfiguration; 12 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 13 | import org.testng.Assert; 14 | 15 | @RunWith(SpringJUnit4ClassRunner.class) 16 | @ContextConfiguration() 17 | @ActiveProfiles("dev") 18 | public class CacheManagerTest { 19 | 20 | @Configuration 21 | // @EnableCaching 22 | @Import({CacheConfigDev.class, CacheConfig.class}) 23 | public static class AppConfig { 24 | } 25 | 26 | @Autowired 27 | private CacheManager cacheManager; 28 | 29 | @Test 30 | public void test() { 31 | for (String cacheName : cacheManager.getCacheNames()) { 32 | System.out.println("cache Name:" + cacheName); 33 | 34 | } 35 | 36 | Cache cache = cacheManager.getCache("movieFindCache"); 37 | cache.put("a", "1"); 38 | Assert.assertEquals(cache.get("a").get(), "1"); 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/controller/BaseContorllerMockTest.java: -------------------------------------------------------------------------------- 1 | package controller; 2 | 3 | import org.springframework.mock.web.MockHttpServletRequest; 4 | import org.springframework.mock.web.MockHttpServletResponse; 5 | import org.springframework.mock.web.MockHttpSession; 6 | import org.springframework.test.context.ContextConfiguration; 7 | import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; 8 | import org.testng.annotations.BeforeMethod; 9 | 10 | import java.util.Hashtable; 11 | import java.util.Map; 12 | 13 | @ContextConfiguration(locations = { 14 | "classpath:service/mockservice.xml", 15 | "classpath:dispatcher-servlet.xml", 16 | }) 17 | public abstract class BaseContorllerMockTest extends AbstractTestNGSpringContextTests { 18 | 19 | protected MockHttpServletRequest request; 20 | protected MockHttpServletResponse response; 21 | 22 | protected Map sessionMap; 23 | protected MockHttpSession session; 24 | 25 | @BeforeMethod 26 | public void beforeMethod() { 27 | request = new MockHttpServletRequest(); 28 | response = new MockHttpServletResponse(); 29 | sessionMap = new Hashtable(); 30 | session = new MockHttpSession(); 31 | request.setSession(session); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /sample-test/src/test/java/controller/BaseContorllerTest.java: -------------------------------------------------------------------------------- 1 | package controller; 2 | 3 | import org.springframework.mock.web.MockHttpServletRequest; 4 | import org.springframework.mock.web.MockHttpServletResponse; 5 | import org.springframework.mock.web.MockHttpSession; 6 | import org.springframework.test.context.ContextConfiguration; 7 | import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; 8 | import org.testng.annotations.BeforeMethod; 9 | 10 | import java.util.Hashtable; 11 | import java.util.Map; 12 | 13 | @ContextConfiguration(locations = { 14 | "classpath:dao/datasource-prod.xml", 15 | "classpath:dao/init-data.xml", 16 | "classpath:dao/dao.xml", 17 | "classpath:service/spring-service.xml", 18 | "classpath:dispatcher-servlet.xml", 19 | }) 20 | public abstract class BaseContorllerTest extends AbstractTestNGSpringContextTests { 21 | 22 | protected MockHttpServletRequest request; 23 | protected MockHttpServletResponse response; 24 | 25 | protected Map sessionMap; 26 | protected MockHttpSession session; 27 | 28 | @BeforeMethod 29 | public void beforeMethod() { 30 | request = mockRequest(); 31 | response = mockResponse() ; 32 | sessionMap = new Hashtable(); 33 | session = mockSession(); 34 | request.setSession(session); 35 | } 36 | 37 | protected MockHttpServletRequest mockRequest() { 38 | return new MockHttpServletRequest(); 39 | } 40 | 41 | protected MockHttpServletResponse mockResponse() { 42 | return new MockHttpServletResponse(); 43 | } 44 | 45 | protected MockHttpSession mockSession() { 46 | return new MockHttpSession(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /sample-test/src/test/java/controller/ErrorControllerTest.java: -------------------------------------------------------------------------------- 1 | package controller; 2 | 3 | import org.springframework.mock.web.MockHttpServletRequest; 4 | import org.testng.annotations.AfterMethod; 5 | import org.testng.annotations.BeforeMethod; 6 | import org.testng.annotations.Test; 7 | 8 | import javax.annotation.Resource; 9 | 10 | public class ErrorControllerTest extends BaseContorllerTest { 11 | 12 | @Resource 13 | private ErrorController errorController; 14 | 15 | 16 | @BeforeMethod 17 | public void setUp() throws Exception { 18 | 19 | } 20 | 21 | @AfterMethod 22 | public void tearDown() throws Exception { 23 | 24 | } 25 | 26 | @Test 27 | public void testError() throws Exception { 28 | request = new MockHttpServletRequest(); 29 | errorController.error(request); 30 | } 31 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/controller/SimpleControllerTest.java: -------------------------------------------------------------------------------- 1 | package controller; 2 | 3 | import org.springframework.mock.web.MockHttpServletResponse; 4 | import org.springframework.test.web.ModelAndViewAssert; 5 | import org.springframework.test.web.servlet.MockMvc; 6 | import org.springframework.test.web.servlet.MvcResult; 7 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 8 | import org.springframework.web.servlet.ModelAndView; 9 | import org.testng.Assert; 10 | import org.testng.annotations.AfterMethod; 11 | import org.testng.annotations.BeforeMethod; 12 | import org.testng.annotations.Test; 13 | 14 | import javax.annotation.Resource; 15 | 16 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 17 | 18 | @Test 19 | public class SimpleControllerTest extends BaseContorllerTest { 20 | 21 | @Resource 22 | SimpleController simpleController; 23 | private MockMvc mockMvc; 24 | 25 | @BeforeMethod 26 | public void setUp() throws Exception { 27 | mockMvc = MockMvcBuilders.standaloneSetup(simpleController).build(); 28 | 29 | } 30 | 31 | @AfterMethod 32 | public void tearDown() throws Exception { 33 | 34 | } 35 | 36 | @Test 37 | public void testTestModelAndView() throws Exception { 38 | 39 | } 40 | 41 | @Test 42 | public void testTestVoid() throws Exception { 43 | MvcResult result = mockMvc.perform(get("/testVoid")).andReturn(); 44 | MockHttpServletResponse mockHttpServletResponse = result.getResponse(); 45 | String s = mockHttpServletResponse.getContentAsString(); 46 | Assert.assertEquals(s, "hello"); 47 | 48 | } 49 | 50 | @Test 51 | public void testTestHello() throws Exception { 52 | // 走controller逻辑,spring自动构造参数 53 | MvcResult result = mockMvc.perform(get("/testHello").param("msg", "hello")).andReturn(); 54 | ModelAndView model = result.getModelAndView(); 55 | 56 | ModelAndViewAssert.assertViewName(model, "index"); 57 | ModelAndViewAssert.assertModelAttributeValue(model, "msg", "hi"); 58 | 59 | } 60 | 61 | @Test 62 | public void testTestEcho() throws Exception { 63 | // 走controller逻辑,spring自动构造参数 64 | MvcResult result = mockMvc.perform(get("/testEcho").param("msg", "hello")).andReturn(); 65 | ModelAndView model = result.getModelAndView(); 66 | ModelAndViewAssert.assertViewName(model, "index"); 67 | ModelAndViewAssert.assertModelAttributeValue(model, "msg", "hello"); 68 | } 69 | 70 | @Test 71 | public void testTestResponseBody() throws Exception { 72 | 73 | } 74 | 75 | @Test 76 | public void testTestReponseEntity() throws Exception { 77 | 78 | } 79 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/controller/login/LoginControllerMockTest.java: -------------------------------------------------------------------------------- 1 | package controller.login; 2 | 3 | import controller.BaseContorllerMockTest; 4 | import org.springframework.ui.ExtendedModelMap; 5 | import org.testng.annotations.Test; 6 | import service.user.UserService; 7 | 8 | import javax.annotation.Resource; 9 | 10 | import static org.mockito.Mockito.when; 11 | import static org.testng.Assert.assertEquals; 12 | 13 | 14 | @Test 15 | public class LoginControllerMockTest extends BaseContorllerMockTest { 16 | 17 | @Resource 18 | private LoginController loginController; 19 | 20 | @Resource 21 | private UserService userService; 22 | 23 | /** 24 | * 模拟数据,桩 25 | * 26 | * @throws Exception 27 | */ 28 | 29 | @Test 30 | public void testLogon() throws Exception { 31 | // service异常情况 32 | String userName = "admin"; 33 | String password = "admin"; 34 | String answer = "pwd"; 35 | when(userService.queryUser(userName, password)).thenThrow(new RuntimeException("dao access error")); 36 | assertEquals(loginController.logon(userName, password, answer, request, response, new ExtendedModelMap()), "error"); 37 | } 38 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/controller/login/LoginControllerTest.java: -------------------------------------------------------------------------------- 1 | package controller.login; 2 | 3 | import com.github.superproxy.testdataprovider.entry.testng.CommonDataProvider; 4 | import com.github.superproxy.testdataprovider.support.csv.Csv; 5 | import controller.BaseContorllerTest; 6 | import org.springframework.test.web.ModelAndViewAssert; 7 | import org.springframework.test.web.servlet.MockMvc; 8 | import org.springframework.test.web.servlet.MvcResult; 9 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 10 | import org.springframework.ui.ExtendedModelMap; 11 | import org.springframework.ui.ModelMap; 12 | import org.springframework.web.servlet.ModelAndView; 13 | import org.testng.annotations.BeforeMethod; 14 | import org.testng.annotations.Test; 15 | 16 | import javax.annotation.Resource; 17 | 18 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; 19 | import static org.testng.Assert.assertEquals; 20 | 21 | 22 | @Test 23 | public class LoginControllerTest extends BaseContorllerTest { 24 | 25 | @Resource 26 | LoginController loginController; 27 | 28 | private MockMvc mockMvc; 29 | 30 | @BeforeMethod() 31 | public void setUp() throws Exception { 32 | mockMvc = MockMvcBuilders.standaloneSetup(loginController).build(); 33 | } 34 | 35 | 36 | @Test 37 | public void testLogon() throws Exception { 38 | 39 | String userName = "admin"; 40 | String password = "admin"; 41 | String answer = "pwd"; 42 | assertEquals(loginController.logon(userName, password, answer, request, response, null), "index"); 43 | 44 | // 规则1 没有校验码 45 | answer = ""; 46 | userName = ""; 47 | password = ""; 48 | assertEquals(loginController.logon(userName, password, answer, request, response, null), "login"); 49 | 50 | // 规则2 没有用户名 51 | answer = "answer"; 52 | userName = ""; 53 | password = ""; 54 | assertEquals(loginController.logon(userName, password, answer, request, response, null), "login"); 55 | 56 | // 规则3 用户名不存在 57 | answer = "pwd"; 58 | userName = "admin2"; 59 | password = ""; 60 | assertEquals(loginController.logon(userName, password, answer, request, response, null), "login"); 61 | 62 | // 规则4 没有密码 63 | answer = "answer"; 64 | userName = "admin"; 65 | password = ""; 66 | assertEquals(loginController.logon(userName, password, answer, request, response, null), "login"); 67 | 68 | // 规则5 密码不对 69 | answer = "pwd"; 70 | userName = "admin"; 71 | password = "admin2"; 72 | assertEquals(loginController.logon(userName, password, answer, request, response, null), "login"); 73 | } 74 | 75 | @Test(dataProvider = "genData", dataProviderClass = CommonDataProvider.class) 76 | @Csv("/controller/LoginController/testLogin.csv") 77 | public void testLogin(String userName, String password, String answer, String expected) throws Exception { 78 | assertEquals(loginController.logon(userName, password, answer, request, response, null), expected); 79 | } 80 | 81 | @Test(dataProvider = "genData", dataProviderClass = CommonDataProvider.class) 82 | @Csv("/controller/LoginController/testLogin.csv") 83 | public void testLoginByModelAssert(String userName, String password, String answer, String expected) throws Exception { 84 | ModelMap modelMap = new ExtendedModelMap(); 85 | String result = loginController.logon(userName, password, answer, request, response, modelMap); 86 | ModelAndView modelAndView = new ModelAndView(result, modelMap); 87 | ModelAndViewAssert.assertViewName(modelAndView, expected); 88 | } 89 | 90 | @Test(dataProvider = "genData", dataProviderClass = CommonDataProvider.class) 91 | @Csv("/controller/LoginController/testLogin.csv") 92 | public void testLoginByMockMvc(String userName, String password, String answer, String expected) throws Exception { 93 | MvcResult result = mockMvc.perform(post("/logon") 94 | .param("userName", userName).param("password", password).param("validate", answer)).andReturn(); 95 | ModelAndView modelAndView = result.getModelAndView(); 96 | ModelAndViewAssert.assertViewName(modelAndView, expected); 97 | 98 | } 99 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/dao/AbstractDbBaseExporter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * SUNING APPLIANCE CHAINS. 3 | * Copyright (c) 2012-2012 All Rights Reserved. 4 | */ 5 | package dao; 6 | 7 | import org.dbunit.DatabaseUnitException; 8 | import org.dbunit.database.IDatabaseConnection; 9 | import org.dbunit.database.QueryDataSet; 10 | import org.dbunit.dataset.xml.FlatXmlDataSet; 11 | import org.testng.annotations.AfterMethod; 12 | import org.testng.annotations.BeforeMethod; 13 | 14 | import java.io.FileOutputStream; 15 | import java.sql.SQLException; 16 | 17 | /** 18 | * 功能描述: 导出数据库中相关表的数据,以用于dbunit作数据库初始化,只供手动调用,不自动化执行 19 | */ 20 | 21 | public abstract class AbstractDbBaseExporter extends BaseDbUnitTest { 22 | IDatabaseConnection connection = null; 23 | 24 | @BeforeMethod 25 | public void beforeMethod() throws DatabaseUnitException, SQLException { 26 | connection = getConnection(); 27 | } 28 | 29 | @AfterMethod 30 | public void afterMethod() throws SQLException { 31 | if (connection != null) { 32 | connection.close(); 33 | } 34 | 35 | } 36 | 37 | protected void exportDataSet(String[] tables, String[] out) throws Exception { 38 | int i = 0; 39 | for (String table : tables) { 40 | QueryDataSet partialDataSet = new QueryDataSet(connection); 41 | partialDataSet.addTable(table); 42 | FlatXmlDataSet.write(partialDataSet, new FileOutputStream(out[i++])); 43 | } 44 | 45 | } 46 | 47 | protected void exportDataSet(String table, String out) throws Exception { 48 | 49 | exportDataSet(new String[]{table}, new String[]{out}); 50 | } 51 | 52 | protected void exportDataSet(String table) throws Exception { 53 | exportDataSet(new String[]{table}, new String[]{table + ".xml"}); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /sample-test/src/test/java/dao/BaseDaoTest.java: -------------------------------------------------------------------------------- 1 | package dao; 2 | 3 | import com.github.springtestdbunit.DbUnitTestExecutionListener; 4 | import org.springframework.test.context.ActiveProfiles; 5 | import org.springframework.test.context.ContextConfiguration; 6 | import org.springframework.test.context.TestExecutionListeners; 7 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 8 | import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; 9 | 10 | 11 | @ActiveProfiles("dev") 12 | @ContextConfiguration(locations = { 13 | "classpath:dao/datasource-prod.xml", 14 | "classpath:dao/datasource-dev.xml", 15 | "classpath:dao/init-data.xml", 16 | "classpath:dao/dao.xml", 17 | }) 18 | @TestExecutionListeners({DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class}) 19 | public abstract class BaseDaoTest extends AbstractTestNGSpringContextTests { 20 | 21 | } 22 | -------------------------------------------------------------------------------- /sample-test/src/test/java/dao/BaseDbUnitTest.java: -------------------------------------------------------------------------------- 1 | package dao; 2 | 3 | import org.dbunit.DatabaseUnitException; 4 | import org.dbunit.database.DatabaseConfig; 5 | import org.dbunit.database.DatabaseConnection; 6 | import org.dbunit.ext.db2.Db2MetadataHandler; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | 9 | import javax.sql.DataSource; 10 | import java.sql.SQLException; 11 | 12 | public class BaseDbUnitTest extends BaseDaoTest { 13 | @Autowired 14 | DataSource dataSource; 15 | 16 | /** 17 | * 功能描述: 建立数据库连接 18 | * 19 | * @return 返回值 20 | * @throw 异常描述 21 | */ 22 | 23 | protected DatabaseConnection getConnection() throws DatabaseUnitException, SQLException { 24 | DatabaseConnection databaseConnection = new DatabaseConnection(dataSource.getConnection()); 25 | DatabaseConfig config = databaseConnection.getConfig(); 26 | config.setProperty(DatabaseConfig.PROPERTY_METADATA_HANDLER, new Db2MetadataHandler()); 27 | config.setProperty(DatabaseConfig.PROPERTY_BATCH_SIZE, Integer.valueOf(50)); 28 | config.setProperty(DatabaseConfig.FEATURE_BATCHED_STATEMENTS, true); 29 | return databaseConnection; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /sample-test/src/test/java/dao/SampleDbExporter.java: -------------------------------------------------------------------------------- 1 | package dao; 2 | 3 | import org.testng.annotations.Test; 4 | 5 | /** 6 | * 导出数据 7 | */ 8 | public class SampleDbExporter extends AbstractDbBaseExporter { 9 | @Test 10 | public void exportUser() throws Exception { 11 | exportDataSet("Users"); 12 | } 13 | 14 | @Test 15 | public void exportUser2() throws Exception { 16 | exportDataSet("Users", "user_data.xml"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /sample-test/src/test/java/dao/user/UserMapperDbUnitTest.java: -------------------------------------------------------------------------------- 1 | package dao.user; 2 | 3 | import com.github.springtestdbunit.annotation.DatabaseSetup; 4 | import com.github.springtestdbunit.annotation.ExpectedDatabase; 5 | import dao.BaseDaoTest; 6 | import model.user.User; 7 | import org.testng.annotations.Test; 8 | 9 | import javax.annotation.Resource; 10 | 11 | import static org.testng.Assert.*; 12 | import static org.testng.AssertJUnit.assertEquals; 13 | 14 | public class UserMapperDbUnitTest extends BaseDaoTest { 15 | 16 | 17 | @Resource 18 | private UserMapper userMapper; 19 | 20 | /** 21 | * 运行没有初始化数据 22 | * 23 | * @throws Exception 24 | */ 25 | @Test 26 | public void testNotExits() throws Exception { 27 | User user = userMapper.queryByUserName("admin2"); 28 | System.out.println(user); 29 | assertNull(user); 30 | } 31 | 32 | /** 33 | * 额外初始化数据集 34 | * 35 | * @throws Exception 36 | */ 37 | @Test 38 | @DatabaseSetup("/dao/user/Users.xml") 39 | public void testExits() throws Exception { 40 | User user = userMapper.queryByUserName("admin2"); 41 | System.out.println(user); 42 | assertNotNull(user); 43 | user.setAge(99); 44 | assertEquals(userMapper.update(user), 1); 45 | user = userMapper.queryByUserName("admin2"); 46 | assertTrue(99 == user.getAge()); 47 | } 48 | 49 | /** 50 | * DatabaseSetup初始化数据集,操作delete admin2,期望的数据集ExpectedDatabase 51 | * 52 | * @throws Exception 53 | */ 54 | @Test 55 | @DatabaseSetup("/dao/user/Users.xml") 56 | @ExpectedDatabase("/dao/user/left.xml") 57 | public void testRemove() throws Exception { 58 | this.userMapper.delete("admin2"); 59 | } 60 | 61 | 62 | @Test 63 | @DatabaseSetup("/dao/user/Users.xml") 64 | @ExpectedDatabase("/dao/user/Users2.xml") 65 | public void testInsert() throws Exception { 66 | userMapper.create(new User("admin3", "admin3", 3)); 67 | } 68 | 69 | /** 70 | * datasetup 会初始化数据admin2,所以在插入数据会报主键冲突 71 | */ 72 | @Test(expectedExceptions = Exception.class) 73 | @DatabaseSetup("/dao/user/Users.xml") 74 | public void test() { 75 | userMapper.create(new User("admin2", "admin2", 1)); 76 | } 77 | 78 | 79 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/dao/user/UserMapperTest.java: -------------------------------------------------------------------------------- 1 | package dao.user; 2 | 3 | import dao.BaseDaoTest; 4 | import model.user.User; 5 | import org.springframework.transaction.annotation.Transactional; 6 | import org.testng.annotations.Test; 7 | 8 | import javax.annotation.Resource; 9 | 10 | import static org.testng.Assert.*; 11 | 12 | public class UserMapperTest extends BaseDaoTest { 13 | 14 | 15 | @Resource 16 | private UserMapper userMapper; 17 | 18 | 19 | @Test(expectedExceptions = Exception.class) 20 | public void test() { 21 | userMapper.create(new User("admin", "admin", 1)); 22 | } 23 | 24 | @Test 25 | @Transactional 26 | public void testUpdate() throws Exception { 27 | User user = userMapper.queryByUserName("admin"); 28 | user.setAge(99); 29 | assertEquals(userMapper.update(user), 1); 30 | } 31 | 32 | 33 | @Test 34 | public void testGetUser() throws Exception { 35 | assertNotNull(userMapper.query("admin", "admin")); 36 | } 37 | 38 | @Test 39 | public void testCreate() throws Exception { 40 | User user = new User("yxz", "yxz", 1); 41 | assertEquals(userMapper.create(user), 1); 42 | } 43 | 44 | @Test 45 | public void testQuery() throws Exception { 46 | User user = new User("yxz", "yxz", 1); 47 | assertEquals(userMapper.create(user), 1); 48 | assertNotNull(userMapper.query("yxz", "yxz")); 49 | assertEquals(userMapper.delete("yxz"), 1); 50 | } 51 | 52 | @Test(dependsOnMethods = "testCreate") 53 | public void testGetAllUsers() throws Exception { 54 | assertTrue(userMapper.getAllUsers().size() == 2); 55 | } 56 | 57 | 58 | @Test(dependsOnMethods = "testCreate") 59 | public void testDelete() throws Exception { 60 | assertEquals(userMapper.delete("yxz"), 1); 61 | } 62 | 63 | 64 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/service/BaseTest.java: -------------------------------------------------------------------------------- 1 | package service; 2 | 3 | import org.springframework.test.context.ActiveProfiles; 4 | import org.springframework.test.context.ContextConfiguration; 5 | import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; 6 | 7 | @ActiveProfiles("dev") 8 | @ContextConfiguration(locations = { 9 | "classpath:dao/datasource-prod.xml", 10 | "classpath:dao/datasource-dev.xml", 11 | "classpath:dao/init-data.xml", 12 | "classpath:dao/dao.xml", 13 | "classpath:service/spring-service.xml", 14 | "classpath:cache/spring-cache.xml", 15 | }) 16 | public abstract class BaseTest extends AbstractTestNGSpringContextTests { 17 | 18 | } 19 | -------------------------------------------------------------------------------- /sample-test/src/test/java/service/MockBaseTest.java: -------------------------------------------------------------------------------- 1 | package service; 2 | 3 | import org.springframework.test.context.ContextConfiguration; 4 | import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; 5 | 6 | 7 | @ContextConfiguration(locations = {"classpath:service/spring-service.xml", "classpath:dao/mockdao.xml"}) 8 | public abstract class MockBaseTest extends AbstractTestNGSpringContextTests { 9 | } 10 | -------------------------------------------------------------------------------- /sample-test/src/test/java/service/user/UserCacheTest.java: -------------------------------------------------------------------------------- 1 | package service.user; 2 | 3 | import model.user.User; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.cache.CacheManager; 6 | import org.testng.Assert; 7 | import org.testng.annotations.Test; 8 | import service.BaseTest; 9 | 10 | public class UserCacheTest extends BaseTest { 11 | @Autowired 12 | private UserService userService; 13 | 14 | @Autowired 15 | private CacheManager cacheManager; 16 | 17 | @Test 18 | public void testQueryUser() { 19 | User user = userService.queryUser("admin"); 20 | Assert.assertNotNull(user); 21 | System.out.println("query admin first end"); 22 | Assert.assertNotNull(userService.queryUser("admin")); 23 | System.out.println("query admin send end"); 24 | Assert.assertEquals(cacheManager.getCache("test").get("user_admin").get(), user); 25 | } 26 | 27 | @Test 28 | public void testUpdateUser() { 29 | User user = userService.queryUser("admin"); 30 | Assert.assertEquals(user.getAge(), 1); 31 | user.setAge(0); 32 | userService.updateUser(user); 33 | user = userService.queryUser("admin"); 34 | Assert.assertNotNull(user); 35 | Assert.assertEquals(user.getAge(), 0); 36 | } 37 | 38 | 39 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/service/user/UserServiceImplTest.java: -------------------------------------------------------------------------------- 1 | package service.user; 2 | 3 | import com.github.superproxy.testdataprovider.entry.testng.CommonDataProvider; 4 | import com.github.superproxy.testdataprovider.support.csv.Csv; 5 | import model.user.User; 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import org.testng.annotations.Test; 9 | import service.BaseTest; 10 | 11 | import javax.annotation.Resource; 12 | 13 | import static org.testng.Assert.*; 14 | 15 | /** 16 | * 测试 UserService的内部逻辑, 17 | * 依赖dao部分使用mock对象 18 | */ 19 | @Test 20 | public class UserServiceImplTest extends BaseTest { 21 | 22 | @Resource 23 | private UserService userService; 24 | 25 | private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImplTest.class); 26 | 27 | @Test 28 | public void testLogin() throws Exception { 29 | String userName = ""; 30 | String password = ""; 31 | assertNotNull(userService.queryUser(userName, password)); 32 | } 33 | 34 | @Test 35 | public void testAddUser() throws Exception { 36 | User user = new User(); 37 | assertTrue(userService.addUser(user)); 38 | } 39 | 40 | @Test(dataProvider = "genData", dataProviderClass = CommonDataProvider.class) 41 | @Csv("/service/UserService/testCalRank.csv") 42 | public void testCalRank(String age, int expected) throws Exception { 43 | User user = new User(); 44 | user.setAge(Integer.parseInt(age)); 45 | assertEquals(userService.calRank(user), expected); 46 | } 47 | 48 | 49 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/service/user/UserServiceImplWithMockTest.java: -------------------------------------------------------------------------------- 1 | package service.user; 2 | 3 | import dao.user.UserMapper; 4 | import model.user.User; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.testng.annotations.Test; 8 | import service.MockBaseTest; 9 | 10 | import javax.annotation.Resource; 11 | 12 | import static org.mockito.Mockito.when; 13 | import static org.testng.Assert.assertNotNull; 14 | 15 | /** 16 | * 测试 UserService的内部逻辑, 17 | * 依赖dao部分使用mock对象 18 | */ 19 | @Test 20 | public class UserServiceImplWithMockTest extends MockBaseTest { 21 | private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImplWithMockTest.class); 22 | 23 | @Resource 24 | private UserService userService; 25 | 26 | /** 27 | * mock userMapper桩,为userService提供服务 28 | */ 29 | @Resource 30 | private UserMapper userMapper; 31 | 32 | @Test 33 | public void testLogin() throws Exception { 34 | String userName = ""; 35 | String password = ""; 36 | 37 | User user = new User(5, "yxz"); 38 | //mock 39 | when(userMapper.query(userName, password)).thenReturn(user); 40 | // 模拟调用 41 | User result = userService.queryUser(userName, password); 42 | LOGGER.debug("{}", result.toString()); 43 | assertNotNull(result); 44 | } 45 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/test/AppTest.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import junit.framework.TestSuite; 4 | import org.testng.annotations.Test; 5 | 6 | import java.util.Random; 7 | 8 | @Test 9 | public class AppTest { 10 | 11 | @Test 12 | public void test() 13 | { 14 | 15 | // TestS 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /sample-test/src/test/java/test/dbunit/SampleTest.java: -------------------------------------------------------------------------------- 1 | package test.dbunit; 2 | 3 | import org.dbunit.DBTestCase; 4 | import org.dbunit.DataSourceDatabaseTester; 5 | import org.dbunit.IDatabaseTester; 6 | import org.dbunit.dataset.IDataSet; 7 | import org.dbunit.dataset.xml.FlatXmlDataSetBuilder; 8 | import org.junit.Test; 9 | 10 | import javax.annotation.Resource; 11 | import javax.sql.DataSource; 12 | import java.io.FileInputStream; 13 | 14 | public class SampleTest extends DBTestCase { 15 | 16 | @Resource 17 | DataSource dataSource; 18 | 19 | public SampleTest(String name) { 20 | } 21 | 22 | protected IDataSet getDataSet() throws Exception { 23 | return new FlatXmlDataSetBuilder().build(new FileInputStream("dataset.xml")); 24 | } 25 | 26 | @Override 27 | protected IDatabaseTester newDatabaseTester() throws Exception { 28 | return new DataSourceDatabaseTester(dataSource); 29 | } 30 | 31 | @Test 32 | public void test() { 33 | 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/test/junit/Fibonacci.java: -------------------------------------------------------------------------------- 1 | package test.junit; 2 | 3 | public class Fibonacci { 4 | 5 | // F0=0,F1=1,Fn=F(n-1)+F(n-2)( 6 | public static int compute(int fInput) { 7 | 8 | if (fInput < 0) { 9 | throw new UnsupportedOperationException("fInput is less than zero"); 10 | } 11 | 12 | 13 | int f0 = 0; 14 | if (fInput == 0) { 15 | return f0; 16 | } 17 | 18 | int f1 = 1; 19 | if (fInput == 1) { 20 | return f1; 21 | } 22 | 23 | int next = 0; 24 | for (int n = 2; n <= fInput; n++) { 25 | next = f0 + f1; 26 | f0 = f1; 27 | f1 = next; 28 | } 29 | return next; 30 | } 31 | 32 | 33 | 34 | // public static int fibonacci(int n){ 35 | // if(n <= 2){ 36 | // return 1; 37 | // }else{ 38 | // return fibonacci(n-1) + fibonacci(n-2); 39 | // } 40 | // } 41 | } 42 | -------------------------------------------------------------------------------- /sample-test/src/test/java/test/junit/FibonacciTest.java: -------------------------------------------------------------------------------- 1 | package test.junit; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.junit.runners.Parameterized; 6 | 7 | import java.util.Arrays; 8 | import java.util.Collection; 9 | 10 | import static org.junit.Assert.assertEquals; 11 | 12 | @RunWith(Parameterized.class) 13 | public class FibonacciTest { 14 | @Parameterized.Parameters 15 | public static Collection data() { 16 | return Arrays.asList(new Object[][]{ 17 | {0, 0}, {1, 1}, {2, 1}, {3, 2}, {4, 3}, {5, 5}, {6, 8} 18 | }); 19 | } 20 | 21 | private int fInput; 22 | 23 | private int fExpected; 24 | 25 | public FibonacciTest(int input, int expected) { 26 | fInput= input; 27 | fExpected= expected; 28 | } 29 | 30 | @Test 31 | public void test() { 32 | assertEquals(fExpected, Fibonacci.compute(fInput)); 33 | } 34 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/test/junit/TestAllFetures.java: -------------------------------------------------------------------------------- 1 | package test.junit; 2 | 3 | 4 | import org.junit.Ignore; 5 | import org.junit.Test; 6 | 7 | import java.util.Arrays; 8 | import java.util.List; 9 | import java.util.Random; 10 | import java.util.Set; 11 | 12 | import static org.hamcrest.CoreMatchers.containsString; 13 | import static org.hamcrest.CoreMatchers.either; 14 | import static org.hamcrest.core.Is.is; 15 | import static org.hamcrest.core.IsCollectionContaining.hasItem; 16 | import static org.hamcrest.core.IsNot.not; 17 | import static org.junit.Assert.*; 18 | 19 | 20 | public class TestAllFetures { 21 | @Test 22 | public void test() { 23 | assertTrue(true); 24 | } 25 | 26 | public void testOpenPage() { 27 | assertTrue(true); 28 | } 29 | 30 | // @Test 31 | // public void ParaTest(String test1) { 32 | // System.out.println("This is " + test1); 33 | // } 34 | 35 | @Test(expected = IllegalArgumentException.class) 36 | public void testException() { 37 | throw new IllegalArgumentException("NullPoint"); 38 | } 39 | 40 | @Test() 41 | public void testThat() { 42 | int x = 3; 43 | assertThat(x, is(3)); 44 | 45 | assertThat(x, is(not(4))); 46 | 47 | String responseString = "color"; 48 | assertThat(responseString, either(containsString("color")).or(containsString("colour"))); 49 | List myList = Arrays.asList(1, 2, 3, 4); 50 | // Set myList = {1, 2, 3, 4}; 51 | assertThat(myList, hasItem(3)); 52 | } 53 | 54 | 55 | @Ignore 56 | @Test 57 | public void testIgnore() { 58 | System.out.println("This test case will ignore"); 59 | } 60 | 61 | @Test(timeout = 10) 62 | public void testTimeOut() throws InterruptedException { 63 | Thread.sleep(1); 64 | } 65 | 66 | 67 | // @Test(dataProvider = "provideNumbers") 68 | public void test(int number, int expected) { 69 | assertEquals(number + 10, expected); 70 | } 71 | 72 | // @DataProvider(name = "provideNumbers") 73 | public Object[][] provideData() { 74 | return new Object[][]{ 75 | {10, 20}, 76 | {100, 110}, 77 | {200, 210} 78 | }; 79 | } 80 | 81 | 82 | // @Test(threadPoolSize = 3, invocationCount = 6, timeOut = 500) 83 | public void f1() { 84 | log("start"); 85 | try { 86 | int sleepTime = new Random().nextInt(1000); 87 | if (sleepTime > 500) log(" should fail"); 88 | Thread.sleep(sleepTime); 89 | } catch (Exception e) { 90 | log(" *** INTERRUPTED"); 91 | } 92 | log("end"); 93 | } 94 | 95 | private void log(String s) { 96 | System.out.println("[" + Thread.currentThread().getId() + "] " + s); 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /sample-test/src/test/java/test/junit/TestMethodOrder.java: -------------------------------------------------------------------------------- 1 | package test.junit; 2 | 3 | import org.junit.FixMethodOrder; 4 | import org.junit.Test; 5 | import org.junit.runners.MethodSorters; 6 | 7 | @FixMethodOrder(MethodSorters.NAME_ASCENDING) 8 | public class TestMethodOrder { 9 | 10 | @Test 11 | public void testA() { 12 | System.out.println("first"); 13 | } 14 | 15 | @Test 16 | public void testB() { 17 | System.out.println("second"); 18 | } 19 | 20 | @Test 21 | public void testC() { 22 | System.out.println("third"); 23 | } 24 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/test/mock/AFinalClass.java: -------------------------------------------------------------------------------- 1 | package test.mock; 2 | 3 | public final class AFinalClass { 4 | public final String echoString(String s) { 5 | return s; 6 | } 7 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/test/mock/AStaticClass.java: -------------------------------------------------------------------------------- 1 | package test.mock; 2 | 3 | public class AStaticClass { 4 | public static final String echoString(String s) { 5 | return s; 6 | } 7 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/test/mock/ClassUnderTest.java: -------------------------------------------------------------------------------- 1 | package test.mock; 2 | 3 | import java.io.File; 4 | 5 | public class ClassUnderTest { 6 | public boolean callArgumentInstance(File file) { 7 | return file.exists(); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /sample-test/src/test/java/test/mock/MockStaticTest.java: -------------------------------------------------------------------------------- 1 | package test.mock; 2 | 3 | import org.apache.commons.lang3.StringUtils; 4 | import org.powermock.core.classloader.annotations.PrepareForTest; 5 | import org.powermock.modules.testng.PowerMockObjectFactory; 6 | import org.testng.ITestObjectFactory; 7 | import org.testng.annotations.ObjectFactory; 8 | import org.testng.annotations.Test; 9 | 10 | import static org.powermock.api.mockito.PowerMockito.mockStatic; 11 | import static org.powermock.api.mockito.PowerMockito.when; 12 | import static org.testng.Assert.assertTrue; 13 | 14 | @Test 15 | @PrepareForTest(StringUtils.class) 16 | public class MockStaticTest { 17 | @ObjectFactory 18 | public ITestObjectFactory getObjectFactory() { 19 | return new PowerMockObjectFactory(); 20 | } 21 | @Test 22 | public void test4() { 23 | mockStatic(StringUtils.class); 24 | when(StringUtils.isEmpty("abc")).thenReturn(Boolean.FALSE); 25 | assertTrue(StringUtils.isEmpty("abc")); 26 | } 27 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/test/mock/MockTest.java: -------------------------------------------------------------------------------- 1 | package test.mock; 2 | 3 | import org.apache.commons.lang3.StringUtils; 4 | import org.mockito.MockitoAnnotations; 5 | import org.powermock.api.mockito.PowerMockito; 6 | import org.powermock.core.classloader.annotations.Mock; 7 | import org.powermock.core.classloader.annotations.PrepareForTest; 8 | import org.testng.Assert; 9 | import org.testng.annotations.BeforeMethod; 10 | import org.testng.annotations.Test; 11 | 12 | import java.io.File; 13 | import java.util.List; 14 | import java.util.Map; 15 | 16 | import static org.mockito.Mockito.mock; 17 | import static org.mockito.Mockito.verify; 18 | import static org.powermock.api.mockito.PowerMockito.mockStatic; 19 | import static org.powermock.api.mockito.PowerMockito.when; 20 | import static org.testng.Assert.assertEquals; 21 | import static org.testng.Assert.assertTrue; 22 | 23 | //import static org.mockito.Mockito.when; 24 | 25 | @PrepareForTest(StringUtils.class) 26 | public class MockTest { 27 | 28 | @Test 29 | public void test() { 30 | List list = mock(List.class); 31 | when(list.get(0)).thenReturn(1); 32 | assertEquals(list.get(0), 1); 33 | } 34 | 35 | @Test 36 | public void testVoid() { 37 | MyInterface myInterface = mock(MyInterface.class); 38 | try { 39 | myInterface.put(new Object()); 40 | } catch (Exception e) { 41 | e.printStackTrace(); 42 | } 43 | } 44 | 45 | @Mock 46 | private List list; 47 | 48 | @BeforeMethod 49 | private void beforeMethod() { 50 | MockitoAnnotations.initMocks(this); 51 | } 52 | 53 | @Test 54 | public void test2() { 55 | when(list.get(0)).thenReturn(1); 56 | assertEquals(list.get(0), 1); 57 | assertEquals(list.get(0), 1); 58 | } 59 | 60 | @Test 61 | public void test3() { 62 | mock(StringUtils.class); 63 | when(StringUtils.isEmpty("abc")).thenReturn(true); 64 | assertTrue(StringUtils.isEmpty("abc")); 65 | } 66 | 67 | @Test 68 | public void test4() { 69 | mockStatic(StringUtils.class); 70 | when(StringUtils.isEmpty("abc")).thenReturn(Boolean.FALSE); 71 | assertTrue(StringUtils.isEmpty("abc")); 72 | 73 | PowerMockito.verifyStatic(); 74 | } 75 | 76 | 77 | @Test 78 | public void testCallArgumentInstance() { 79 | File file = mock(File.class); 80 | ClassUnderTest underTest = new ClassUnderTest(); 81 | PowerMockito.when(file.exists()).thenReturn(true); 82 | Assert.assertTrue(underTest.callArgumentInstance(file)); 83 | } 84 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/test/mock/MockTest2.java: -------------------------------------------------------------------------------- 1 | package test.mock; 2 | 3 | 4 | import org.junit.Assert; 5 | import org.mockito.Mockito; 6 | import org.powermock.api.mockito.PowerMockito; 7 | import org.powermock.core.classloader.annotations.PrepareForTest; 8 | import org.powermock.modules.testng.PowerMockTestCase; 9 | import org.testng.annotations.Test; 10 | import test.mock.AFinalClass; 11 | import test.mock.AStaticClass; 12 | //import org.powermock.modules.junit4.PowerMockRunner; 13 | 14 | //@RunWith(PowerMockRunner.class) 15 | @PrepareForTest({AFinalClass.class, AStaticClass.class}) 16 | public class MockTest2 extends PowerMockTestCase { 17 | 18 | @Test 19 | public void mockFinalClassTest() { 20 | AFinalClass tested = PowerMockito.mock(AFinalClass.class); 21 | 22 | final String testInput = "A test input"; 23 | final String mockedResult = "Mocked final echo result - " + testInput; 24 | Mockito.when(tested.echoString(testInput)).thenReturn(mockedResult); 25 | 26 | // Assert the mocked result is returned from method call 27 | Assert.assertEquals(tested.echoString(testInput), mockedResult); 28 | } 29 | 30 | @Test 31 | public void mockStaticClassTest() { 32 | PowerMockito.mockStatic(AStaticClass.class); 33 | 34 | final String testInput = "A test input"; 35 | final String mockedResult = "Mocked static echo result - " + testInput; 36 | Mockito.when(AStaticClass.echoString(testInput)).thenReturn(mockedResult); 37 | 38 | // Assert the mocked result is returned from method call 39 | Assert.assertEquals(AStaticClass.echoString(testInput), mockedResult); 40 | } 41 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/test/mock/MyInterface.java: -------------------------------------------------------------------------------- 1 | package test.mock; 2 | 3 | /** 4 | * Created by 14120295 on 2015/9/15. 5 | */ 6 | public interface MyInterface { 7 | 8 | void put(Object o); 9 | } 10 | -------------------------------------------------------------------------------- /sample-test/src/test/java/test/selenium/BaiduTestByTestNg.java: -------------------------------------------------------------------------------- 1 | package test.selenium; 2 | 3 | import com.thoughtworks.selenium.SeleneseTestNgHelper; 4 | import org.testng.annotations.Test; 5 | 6 | @Test 7 | public class BaiduTestByTestNg extends SeleneseTestNgHelper { 8 | @Test 9 | public void test() throws Exception { 10 | selenium.type("id=kw", "abcd"); 11 | selenium.click("id=su"); 12 | } 13 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/test/selenium/BaiduWebTest.java: -------------------------------------------------------------------------------- 1 | package test.selenium; 2 | 3 | import org.junit.After; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | import org.openqa.selenium.By; 7 | import org.openqa.selenium.WebDriver; 8 | import org.openqa.selenium.firefox.FirefoxDriver; 9 | 10 | import java.util.concurrent.TimeUnit; 11 | 12 | import static org.junit.Assert.fail; 13 | 14 | public class BaiduWebTest { 15 | private WebDriver driver; 16 | private String baseUrl; 17 | private boolean acceptNextAlert = true; 18 | private StringBuffer verificationErrors = new StringBuffer(); 19 | 20 | @Before 21 | public void setUp() throws Exception { 22 | driver = new FirefoxDriver(); 23 | baseUrl = "http://www.baidu.com/"; 24 | driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 25 | } 26 | 27 | @Test 28 | public void testAWeb() throws Exception { 29 | driver.findElement(By.id("kw")).clear(); 30 | driver.findElement(By.id("kw")).sendKeys("abcd"); 31 | driver.findElement(By.id("su")).click(); 32 | } 33 | 34 | @After 35 | public void tearDown() throws Exception { 36 | driver.quit(); 37 | String verificationErrorString = verificationErrors.toString(); 38 | if (!"".equals(verificationErrorString)) { 39 | fail(verificationErrorString); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /sample-test/src/test/java/test/selenium/BaiduWebTest2.java: -------------------------------------------------------------------------------- 1 | package test.selenium; 2 | 3 | import com.thoughtworks.selenium.Selenium; 4 | import org.openqa.selenium.firefox.FirefoxDriver; 5 | import org.openqa.selenium.WebDriver; 6 | import com.thoughtworks.selenium.webdriven.WebDriverBackedSelenium; 7 | import org.junit.After; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | import static org.junit.Assert.*; 11 | import java.util.regex.Pattern; 12 | import static org.apache.commons.lang3.StringUtils.join; 13 | 14 | public class BaiduWebTest2 { 15 | private Selenium selenium; 16 | 17 | @Before 18 | public void setUp() throws Exception { 19 | WebDriver driver = new FirefoxDriver(); 20 | String baseUrl = "http://www.baidu.com/"; 21 | selenium = new WebDriverBackedSelenium(driver, baseUrl); 22 | } 23 | 24 | @Test 25 | public void testBB() throws Exception { 26 | selenium.type("id=kw", "abcd"); 27 | selenium.click("id=su"); 28 | } 29 | 30 | @After 31 | public void tearDown() throws Exception { 32 | selenium.stop(); 33 | } 34 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/test/selenium/BaiduWebTest3.java: -------------------------------------------------------------------------------- 1 | package test.selenium; 2 | 3 | import com.thoughtworks.selenium.*; 4 | import org.junit.After; 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | import static org.junit.Assert.*; 8 | import java.util.regex.Pattern; 9 | 10 | public class BaiduWebTest3 { 11 | private Selenium selenium; 12 | 13 | @Before 14 | public void setUp() throws Exception { 15 | selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.baidu.com/"); 16 | selenium.start(); 17 | } 18 | 19 | @Test 20 | public void testC() throws Exception { 21 | selenium.type("id=kw", "abcd"); 22 | selenium.click("id=su"); 23 | } 24 | 25 | @After 26 | public void tearDown() throws Exception { 27 | selenium.stop(); 28 | } 29 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/test/spring/AnnotaionTest.java: -------------------------------------------------------------------------------- 1 | package test.spring; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | import org.springframework.context.annotation.Scope; 9 | import org.springframework.test.context.ContextConfiguration; 10 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 11 | 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | 15 | 16 | @ContextConfiguration 17 | @RunWith(SpringJUnit4ClassRunner.class) 18 | public class AnnotaionTest //extends AbstractTestNGSpringContextTests 19 | { 20 | 21 | @Configuration 22 | public static class Config { 23 | 24 | /** 25 | * 不起作用 26 | * 27 | * @return 28 | */ 29 | @Bean 30 | @Scope("prototype") 31 | public List list() { 32 | List list = new ArrayList(); 33 | list.add("abc"); 34 | return list; 35 | } 36 | 37 | @Bean(initMethod = "setup", destroyMethod = "cleanup") 38 | @Scope("prototype") 39 | public Course course() { 40 | return new Course(); 41 | } 42 | } 43 | 44 | @Autowired(required = false) 45 | List list; 46 | 47 | @Test 48 | public void test() { 49 | System.out.println(list.get(0)); 50 | } 51 | 52 | 53 | @Autowired(required = false) 54 | Course course; 55 | 56 | @Test 57 | public void testCourse() { 58 | System.out.println(course.getName()); 59 | } 60 | 61 | 62 | } 63 | -------------------------------------------------------------------------------- /sample-test/src/test/java/test/spring/IfProfileTest.java: -------------------------------------------------------------------------------- 1 | package test.spring; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.test.annotation.IfProfileValue; 7 | import org.springframework.test.annotation.ProfileValueSource; 8 | import org.springframework.test.annotation.ProfileValueSourceConfiguration; 9 | import org.springframework.test.context.ContextConfiguration; 10 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 11 | 12 | import java.util.Hashtable; 13 | 14 | 15 | @ProfileValueSourceConfiguration(IfProfileTest.TestProfileValueProvider.class) 16 | @ContextConfiguration 17 | @RunWith(SpringJUnit4ClassRunner.class) 18 | public class IfProfileTest //extends AbstractTestNGSpringContextTests 19 | { 20 | @Configuration 21 | public static class Config { 22 | } 23 | 24 | @Test 25 | @IfProfileValue(name = "java.vendor", value = "Sun Microsystems Inc") 26 | public void testProcessWhichRunsOnlyOnSunJvm() { 27 | System.out.println("sun.jvm"); 28 | } 29 | 30 | @Test 31 | @IfProfileValue(name = "java.vendor", value = "open jdk") 32 | public void testProcessWhichRunsOnlyOnOpenJdk() { 33 | System.out.println("openjdk.jvm"); 34 | } 35 | 36 | @Test 37 | @IfProfileValue(name = "java.vendor", values = {"Sun Microsystems Inc", "open jdk"}) 38 | public void testProcessWhichRunsOnlyAll() { 39 | System.out.println("all.jvm"); 40 | } 41 | 42 | 43 | public static class TestProfileValueProvider implements ProfileValueSource { 44 | 45 | private static Hashtable properties = new Hashtable(); 46 | 47 | static { 48 | properties.put("java.vendor", "Sun Microsystems Inc"); 49 | } 50 | 51 | public String get(String s) { 52 | return properties.get(s); 53 | } 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /sample-test/src/test/java/test/spring/ProfileTest.java: -------------------------------------------------------------------------------- 1 | package test.spring; 2 | 3 | public class ProfileTest { 4 | } 5 | -------------------------------------------------------------------------------- /sample-test/src/test/java/test/testframework/DataProviderTest.java: -------------------------------------------------------------------------------- 1 | package test.testframework; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.testng.annotations.DataProvider; 6 | import org.testng.annotations.Test; 7 | 8 | import java.lang.reflect.Method; 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | import static org.testng.Assert.assertEquals; 13 | 14 | public class DataProviderTest { 15 | private static final Logger LOGGER = LoggerFactory.getLogger(DataProviderTest.class); 16 | 17 | public int calRank(int age) { 18 | if (age < 10) { 19 | return 0; 20 | } 21 | 22 | if (age < 20) { 23 | return 1; 24 | } 25 | 26 | if (age < 30) { 27 | return 2; 28 | } 29 | 30 | return 3; 31 | } 32 | 33 | 34 | @Test 35 | public void testCalRank() throws Exception { 36 | assertEquals(calRank(9), 0); 37 | assertEquals(calRank(10), 1); 38 | assertEquals(calRank(20), 2); 39 | assertEquals(calRank(30), 3); 40 | //设计测试样例 41 | assertEquals(calRank(100), 3); 42 | } 43 | 44 | @Test(dataProvider = "testCalRank2Data") 45 | public void testCalRank2(int age, int expected) throws Exception { 46 | LOGGER.debug("age:{}, expected:{}", age, expected); 47 | assertEquals(calRank(age), expected); 48 | } 49 | 50 | @DataProvider(name = "testCalRank2Data") 51 | public Object[][] testCalRank2Data(Method method) { 52 | LOGGER.debug("{}", method.getName()); 53 | List objectList = new ArrayList(); 54 | int expected = 0; 55 | objectList.add(new Object[]{9, expected}); 56 | expected = 1; 57 | objectList.add(new Object[]{10, expected}); 58 | 59 | expected = 2; 60 | objectList.add(new Object[]{20, expected}); 61 | 62 | expected = 3; 63 | objectList.add(new Object[]{30, expected}); 64 | 65 | expected = 3; 66 | objectList.add(new Object[]{2, expected}); 67 | return objectList.toArray(new Object[0][0]); 68 | } 69 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/test/testng/FindNumbers.java: -------------------------------------------------------------------------------- 1 | package test.testng; 2 | 3 | public class FindNumbers { 4 | 5 | public static void main(String[] args) { 6 | int a1; 7 | int a2; 8 | int a3 = 9; 9 | int b1; 10 | int b2; 11 | int b3; 12 | int c1; 13 | int c2; 14 | int c3; 15 | 16 | int n = 100; 17 | int s = 1; 18 | 19 | 20 | for (int ia1 = s; ia1 < n; ia1++) { 21 | a1 = ia1; 22 | for (int ia2 = s; ia2 < n; ia2++) { 23 | a2 = ia2; 24 | if (a1 + a2 - a3 != 4) { 25 | continue; 26 | } 27 | // for (int ia3 = s; ia3 < n; ia3++) { 28 | a3 = 9; 29 | if (a1 + a2 - a3 != 4) { 30 | continue; 31 | } 32 | 33 | for (int ib1 = s; ib1 < n; ib1++) { 34 | b1 = ib1; 35 | for (int ib2 = s; ib2 < n; ib2++) { 36 | b2 = ib2; 37 | // for (int ib3 = s; ib3 < n; ib3++) { 38 | // b3 = ib3; 39 | // if (b1 - b2 * b3 != 4) { 40 | // continue; 41 | // } 42 | 43 | b3 = (b1 - 4) / b2; 44 | // for (int ic1 = 1; ic1 < n; ic1++) { 45 | // c1 = ic1; 46 | // if (a1 + b1 / c1 != 4) { 47 | // continue; 48 | // } 49 | 50 | c1 = (4 - a1) / b1; 51 | 52 | c2 = (a2 - 4) / b2; 53 | // for (int ic2 = s; ic2 < n; ic2++) { 54 | // c2 = ic2; 55 | if (a2 - b2 * c2 != 4) { 56 | continue; 57 | } 58 | // for (int ic3 = s; ic3 < n; ic3++) { 59 | // c3 = ic3; 60 | if ((a3 - b3 - 4) != (c1 + c2 - 4)) { 61 | continue; 62 | } 63 | c3 = c1 + c2 - 4; 64 | 65 | // 66 | // if (c1 + c2 - c3 != 4) { 67 | // continue; 68 | // } 69 | // if (a3 - b3 - c3 != 4) { 70 | // continue; 71 | // } 72 | 73 | 74 | System.out.println("result" + a1 + "," + a2 + "," + a3 + "," + b1 + "," + b2 + "," + b3 + "," + c1 + "," + c2 + "," + c3); 75 | // } 76 | // } 77 | // } 78 | // } 79 | } 80 | } 81 | } 82 | } 83 | } 84 | } 85 | 86 | -------------------------------------------------------------------------------- /sample-test/src/test/java/test/testng/TestAllFetures.java: -------------------------------------------------------------------------------- 1 | package test.testng; 2 | 3 | import org.testng.annotations.DataProvider; 4 | import org.testng.annotations.Parameters; 5 | import org.testng.annotations.Test; 6 | 7 | import java.util.Random; 8 | 9 | import static org.testng.Assert.assertEquals; 10 | import static org.testng.Assert.assertTrue; 11 | 12 | @Test 13 | public class TestAllFetures { 14 | @Test 15 | public void test() { 16 | assertTrue(true); 17 | } 18 | 19 | @Test(groups = {"functiontest"}) 20 | public void testOpenPage() { 21 | assertTrue(true); 22 | } 23 | 24 | @Test 25 | @Parameters("test1") 26 | public void ParaTest(String test1) { 27 | System.out.println("This is " + test1); 28 | } 29 | 30 | 31 | @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "NullPoint") 32 | public void testException() { 33 | throw new IllegalArgumentException("NullPoint"); 34 | } 35 | 36 | 37 | @Test(enabled = false) 38 | public void testIgnore() { 39 | System.out.println("This test case will ignore"); 40 | } 41 | 42 | 43 | @Test(dataProvider = "provideNumbers") 44 | public void test(int number, int expected) { 45 | assertEquals(number + 10, expected); 46 | } 47 | 48 | @DataProvider(name = "provideNumbers") 49 | public Object[][] provideData() { 50 | return new Object[][]{ 51 | {10, 20}, 52 | {100, 110}, 53 | {200, 210} 54 | }; 55 | } 56 | 57 | 58 | @Test(threadPoolSize = 3, invocationCount = 6, timeOut = 500) 59 | public void f1() { 60 | log("start"); 61 | try { 62 | int sleepTime = new Random().nextInt(1000); 63 | if (sleepTime > 500) log(" should fail"); 64 | Thread.sleep(sleepTime); 65 | } catch (Exception e) { 66 | log(" *** INTERRUPTED"); 67 | } 68 | log("end"); 69 | } 70 | 71 | private void log(String s) { 72 | System.out.println("[" + Thread.currentThread().getId() + "] " + s); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /sample-test/src/test/java/test/utilstest/User.java: -------------------------------------------------------------------------------- 1 | package test.utilstest; 2 | 3 | public class User { 4 | 5 | private long id; 6 | private String first; 7 | private String last; 8 | 9 | public User(long id, String first, String last) { 10 | this.id = id; 11 | this.first = first; 12 | this.last = last; 13 | } 14 | } -------------------------------------------------------------------------------- /sample-test/src/test/java/test/utilstest/UtilsTest.java: -------------------------------------------------------------------------------- 1 | package test.utilstest; 2 | 3 | import org.testng.annotations.Test; 4 | 5 | import static org.unitils.reflectionassert.ReflectionAssert.assertReflectionEquals; 6 | 7 | public class UtilsTest { 8 | 9 | @Test 10 | public void test() { 11 | User user1 = new User(1, "John", "Doe"); 12 | User user2 = new User(1, "John", "Doe"); 13 | // assertEquals(user1, user2); // 失败,不会执行下面的 14 | 15 | assertReflectionEquals(user1, user2); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /sample-test/src/test/resources/clover.license: -------------------------------------------------------------------------------- 1 | OrpNMxOETaLDvMFJjttrWquuPnnGoTAWVbRwXrCXccBFan 2 | mi2K8urA2>Pavg1xpsc8s0LP2KhjfS1HEJZbGncGhrz0Ud 3 | RmpQvxornqqoMrmpMonTVOMoRMRpqPqNoMSSSxSSsPnmpm 4 | UUnqpvvmsqmmmmmUUnqpvvmsqmmmmmUUvuwUUn -------------------------------------------------------------------------------- /sample-test/src/test/resources/config/application.properties: -------------------------------------------------------------------------------- 1 | spring.freemarker.checkTemplateLocation=false -------------------------------------------------------------------------------- /sample-test/src/test/resources/controller/LoginController/testLogin.csv: -------------------------------------------------------------------------------- 1 | name,password,answer,expected,备注 2 | admin,admin,pwd,index,正确 3 | ,,,login,没有校验码 4 | ,,answer,login,没有用户名 5 | admin2,,pwd,login,用户名不存在 6 | admin,,answer,login, 没有密码 7 | admin,admin2,pwd,login,密码错误 8 | -------------------------------------------------------------------------------- /sample-test/src/test/resources/dao/datasource-dev.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | stat 39 | 40 | 41 | 42 | 20 43 | 44 | 45 | 1 46 | 47 | 48 | 60000 49 | 50 | 51 | 1 52 | 53 | 54 | 55 | 60000 56 | 57 | 58 | 300000 59 | 60 | 61 | 62 | SELECT 'x' 63 | 64 | 65 | true 66 | 67 | 68 | false 69 | 70 | 71 | false 72 | 73 | 74 | 75 | true 76 | 77 | 78 | 20 79 | 80 | 81 | -------------------------------------------------------------------------------- /sample-test/src/test/resources/dao/init-data.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /sample-test/src/test/resources/dao/mockdao.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /sample-test/src/test/resources/dao/user/Users.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /sample-test/src/test/resources/dao/user/Users2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /sample-test/src/test/resources/dao/user/left.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /sample-test/src/test/resources/data/schema.sql: -------------------------------------------------------------------------------- 1 | drop TABLE Users if EXISTS ; 2 | CREATE TABLE Users ( 3 | name VARCHAR(20) PRIMARY KEY , 4 | password VARCHAR(10) NOT NULL, 5 | age int DEFAULT 0 6 | ) ; 7 | 8 | -------------------------------------------------------------------------------- /sample-test/src/test/resources/data/test-data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO USERS (name, password, age) VALUES ('admin', 'admin', 1); -------------------------------------------------------------------------------- /sample-test/src/test/resources/service/UserService/testCalRank.csv: -------------------------------------------------------------------------------- 1 | 年龄,期望值 2 | 9,0 3 | 10,1 4 | 20,2 5 | 30,3 6 | 100,3 7 | -------------------------------------------------------------------------------- /sample-test/src/test/resources/service/mockservice.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /sample-test/src/test/resources/testng.xml: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /show_test.bat: -------------------------------------------------------------------------------- 1 | mvn test 2 | 3 | 4 | mvn sonar:sonar --------------------------------------------------------------------------------