From 0468b27f170b386bfdd4ca6f21b093c111961e23 Mon Sep 17 00:00:00 2001 From: monkeyk7 Date: Sun, 22 Apr 2018 14:21:56 +0800 Subject: [PATCH] Update unit test --- others/how_to_use.txt | 16 +++++----- .../java/com/monkeyk/sos/ContextTest.java | 14 +++++++-- .../SpringOauthServerApplicationTests.java | 2 ++ .../TestApplicationContextInitializer.java | 21 -------------- .../resources/application-test.properties | 29 +++++++++++++++++++ src/test/resources/log4j.properties | 8 ----- src/test/resources/test.properties | 8 ----- 7 files changed, 51 insertions(+), 47 deletions(-) delete mode 100644 src/test/java/com/monkeyk/sos/TestApplicationContextInitializer.java create mode 100644 src/test/resources/application-test.properties delete mode 100644 src/test/resources/log4j.properties delete mode 100644 src/test/resources/test.properties diff --git a/others/how_to_use.txt b/others/how_to_use.txt index fab600d..b8f59b1 100644 --- a/others/how_to_use.txt +++ b/others/how_to_use.txt @@ -1,25 +1,27 @@ 使用的主要技术与版本号 -*Spring (4.1.6.RELEASE) -*Spring Security (4.0.1.RELEASE) -*spring-security-oauth2 (2.0.7.RELEASE) +*Spring-Boot (2.0.1.RELEASE) +*spring-security-oauth2 (2.3.0.RELEASE) 如何使用? -1.项目是Maven管理的, 需要本地安装maven(开发用的maven版本号为3.1.0), 还有MySql(开发用的mysql版本号为5.6) +1.项目是Maven管理的, 需要本地安装maven(开发用的maven版本号为3.3.3), 还有MySql(开发用的mysql版本号为5.6) 2.下载(或clone)项目到本地 -3.创建MySQL数据库(如数据库名oauth2), 并运行相应的SQL脚本(脚本文件位于others/database目录), +3.创建MySQL数据库(数据库名oauth2_boot), 并运行相应的SQL脚本(脚本文件位于others/database目录), 运行脚本的顺序: initial_db.ddl -> oauth.ddl -> initial_data.ddl -4.修改spring-oauth-server.properties(位于src/resources目录)中的数据库连接信息(包括username, password等) +4.修改application.properties(位于src/resources目录)中的数据库连接信息(包括username, password等) 5.将本地项目导入到IDE(如Intellij IDEA)中,配置Tomcat(或类似的servlet运行服务器), 并启动Tomcat(默认端口为8080) 另: 也可通过maven package命令将项目编译为war文件(spring-oauth-server.war), - 将war放在Tomcat中并启动(注意: 这种方式需要将spring-oauth-server.properties加入到classpath中并正确配置数据库连接信息). + 将war放在Tomcat中并启动(注意: 这种方式需要将application.properties加入到classpath中并正确配置数据库连接信息). 6.参考oauth_test.txt(位于others目录)的内容并测试之(也可在浏览器中访问相应的地址,如: http://localhost:8080/spring-oauth-server). +7. 运行单元测试时请先创建数据库 oauth2_boot_test, 并依次运行SQL脚本. + 运行脚本的顺序: initial_db.ddl -> oauth.ddl + diff --git a/src/test/java/com/monkeyk/sos/ContextTest.java b/src/test/java/com/monkeyk/sos/ContextTest.java index 0f27159..df06b2e 100644 --- a/src/test/java/com/monkeyk/sos/ContextTest.java +++ b/src/test/java/com/monkeyk/sos/ContextTest.java @@ -1,17 +1,25 @@ package com.monkeyk.sos; -import org.springframework.test.context.ContextConfiguration; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests; +import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.transaction.BeforeTransaction; /** * @author Shengzhao Li */ -@ContextConfiguration(locations = {""}, initializers = {TestApplicationContextInitializer.class}) +@RunWith(SpringRunner.class) +@SpringBootTest +@TestPropertySource(locations = "classpath:application-test.properties") public abstract class ContextTest extends AbstractTransactionalJUnit4SpringContextTests { + @BeforeTransaction - public void beforeTest() { + public void before() throws Exception { } + + } \ No newline at end of file diff --git a/src/test/java/com/monkeyk/sos/SpringOauthServerApplicationTests.java b/src/test/java/com/monkeyk/sos/SpringOauthServerApplicationTests.java index f8a94ab..a14726b 100644 --- a/src/test/java/com/monkeyk/sos/SpringOauthServerApplicationTests.java +++ b/src/test/java/com/monkeyk/sos/SpringOauthServerApplicationTests.java @@ -3,10 +3,12 @@ package com.monkeyk.sos; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest +@TestPropertySource(locations = "classpath:application-test.properties") public class SpringOauthServerApplicationTests { @Test diff --git a/src/test/java/com/monkeyk/sos/TestApplicationContextInitializer.java b/src/test/java/com/monkeyk/sos/TestApplicationContextInitializer.java deleted file mode 100644 index 9f63aec..0000000 --- a/src/test/java/com/monkeyk/sos/TestApplicationContextInitializer.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.monkeyk.sos; - -import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; -import org.springframework.context.ApplicationContextInitializer; -import org.springframework.context.support.AbstractApplicationContext; -import org.springframework.core.io.ClassPathResource; - -/** - * @author Shengzhao Li - */ -public class TestApplicationContextInitializer implements ApplicationContextInitializer { - - @Override - public void initialize(AbstractApplicationContext applicationContext) { - PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer(); - //load test.properties - propertyPlaceholderConfigurer.setLocation(new ClassPathResource("test.properties")); - - applicationContext.addBeanFactoryPostProcessor(propertyPlaceholderConfigurer); - } -} \ No newline at end of file diff --git a/src/test/resources/application-test.properties b/src/test/resources/application-test.properties new file mode 100644 index 0000000..eeaed5d --- /dev/null +++ b/src/test/resources/application-test.properties @@ -0,0 +1,29 @@ +# +# +spring.application.name=spring-oauth-server +# +# MySQL +##################### +spring.datasource.driver-class-name=com.mysql.jdbc.Driver +spring.datasource.url=jdbc:mysql://localhost:3306/oauth2_boot_test?autoReconnect=true&autoReconnectForPools=true&useUnicode=true&characterEncoding=utf8 +spring.datasource.username=andaily +spring.datasource.password=andaily +#Datasource properties +spring.datasource.type=com.zaxxer.hikari.HikariDataSource +spring.datasource.hikari.maximum-pool-size=20 +spring.datasource.hikari.minimum-idle=2 +# +# MVC +spring.mvc.ignore-default-model-on-redirect=false +spring.http.encoding.enabled=true +spring.http.encoding.charset=UTF-8 +spring.http.encoding.force=true +spring.mvc.locale=zh_CN +spring.mvc.view.prefix=/WEB-INF/jsp/ +spring.mvc.view.suffix=.jsp +# +# +# Logging +# +logging.level.root=INFO + diff --git a/src/test/resources/log4j.properties b/src/test/resources/log4j.properties deleted file mode 100644 index 0e6392a..0000000 --- a/src/test/resources/log4j.properties +++ /dev/null @@ -1,8 +0,0 @@ -log4j.rootLogger=INFO, stdout - -log4j.appender.stdout=org.apache.log4j.ConsoleAppender -#log4j.appender.stdout.Threshold=INFO -#log4j.appender.stdout.ImmediateFlush=true -log4j.appender.stdout.Target=System.out -log4j.appender.stdout.layout=org.apache.log4j.PatternLayout -log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n diff --git a/src/test/resources/test.properties b/src/test/resources/test.properties deleted file mode 100644 index adaaba2..0000000 --- a/src/test/resources/test.properties +++ /dev/null @@ -1,8 +0,0 @@ -#JDBC configuration information -jdbc.driverClassName=com.mysql.jdbc.Driver -############ -# localhost -############ -jdbc.url=jdbc:mysql://localhost:3306/oauth2_test?autoReconnect=true&autoReconnectForPools=true&useUnicode=true&characterEncoding=utf8 -jdbc.username=andaily -jdbc.password=andaily \ No newline at end of file