Browse Source

Update unit test

2.0.0
monkeyk7 7 years ago
parent
commit
0468b27f17
  1. 16
      others/how_to_use.txt
  2. 14
      src/test/java/com/monkeyk/sos/ContextTest.java
  3. 2
      src/test/java/com/monkeyk/sos/SpringOauthServerApplicationTests.java
  4. 21
      src/test/java/com/monkeyk/sos/TestApplicationContextInitializer.java
  5. 29
      src/test/resources/application-test.properties
  6. 8
      src/test/resources/log4j.properties
  7. 8
      src/test/resources/test.properties

16
others/how_to_use.txt

@ -1,25 +1,27 @@
使用的主要技术与版本号 使用的主要技术与版本号
*Spring (4.1.6.RELEASE) *Spring-Boot (2.0.1.RELEASE)
*Spring Security (4.0.1.RELEASE) *spring-security-oauth2 (2.3.0.RELEASE)
*spring-security-oauth2 (2.0.7.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)项目到本地 2.下载(或clone)项目到本地
3.创建MySQL数据库(数据库名oauth2), 并运行相应的SQL脚本(脚本文件位于others/database目录), 3.创建MySQL数据库(数据库名oauth2_boot), 并运行相应的SQL脚本(脚本文件位于others/database目录),
运行脚本的顺序: initial_db.ddl -> oauth.ddl -> initial_data.ddl 运行脚本的顺序: 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) 5.将本地项目导入到IDE(如Intellij IDEA)中,配置Tomcat(或类似的servlet运行服务器), 并启动Tomcat(默认端口为8080)
另: 也可通过maven package命令将项目编译为war文件(spring-oauth-server.war), 另: 也可通过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). 6.参考oauth_test.txt(位于others目录)的内容并测试之(也可在浏览器中访问相应的地址,如: http://localhost:8080/spring-oauth-server).
7. 运行单元测试时请先创建数据库 oauth2_boot_test, 并依次运行SQL脚本.
运行脚本的顺序: initial_db.ddl -> oauth.ddl

14
src/test/java/com/monkeyk/sos/ContextTest.java

@ -1,17 +1,25 @@
package com.monkeyk.sos; 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.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.transaction.BeforeTransaction; import org.springframework.test.context.transaction.BeforeTransaction;
/** /**
* @author Shengzhao Li * @author Shengzhao Li
*/ */
@ContextConfiguration(locations = {""}, initializers = {TestApplicationContextInitializer.class}) @RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource(locations = "classpath:application-test.properties")
public abstract class ContextTest extends AbstractTransactionalJUnit4SpringContextTests { public abstract class ContextTest extends AbstractTransactionalJUnit4SpringContextTests {
@BeforeTransaction @BeforeTransaction
public void beforeTest() { public void before() throws Exception {
} }
} }

2
src/test/java/com/monkeyk/sos/SpringOauthServerApplicationTests.java

@ -3,10 +3,12 @@ package com.monkeyk.sos;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
@TestPropertySource(locations = "classpath:application-test.properties")
public class SpringOauthServerApplicationTests { public class SpringOauthServerApplicationTests {
@Test @Test

21
src/test/java/com/monkeyk/sos/TestApplicationContextInitializer.java

@ -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<AbstractApplicationContext> {
@Override
public void initialize(AbstractApplicationContext applicationContext) {
PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
//load test.properties
propertyPlaceholderConfigurer.setLocation(new ClassPathResource("test.properties"));
applicationContext.addBeanFactoryPostProcessor(propertyPlaceholderConfigurer);
}
}

29
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

8
src/test/resources/log4j.properties

@ -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

8
src/test/resources/test.properties

@ -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
Loading…
Cancel
Save