spring+dubbox的单元测试和生产系统

这个过程比较折腾,先是一路搭建过来能够正确运行基于spring-test创建的单元测试,但发现在tomcat中无法正确运行,提示的错误不再赘述了,可能是中间某些dependency的版本冲突造成的。于是重新按照自己记录的过程(参见前几篇博客),重新走一遍整个过程,在tomcat上可以正确运行了,但郁闷的是基于spring-test编写的单元测试无法运行了,加入全部dependency后,又尝试了多种方法,单元测试反复提示

javax.validation.ValidationException: HV000183: Unable to load 'javax.el.ExpressionFactory'

既然tomcat中可以正常运行,那就是tomcat中的lib在单元测试中没有加入,于是尝试过逐个加入tomcat/lib/*.jar,但并无效果~~如下方法经测试无效:

1. 增加dependency: tomcat-el-api

        
            org.apache.tomcat
            tomcat-el-api
            8.5.27
            provided
        
        
            org.apache.tomcat
            tomcat-jasper-el
            8.5.27
            provided
        

2. 增加dependency: javax.el


   javax.el
   javax.el-api
   2.2.4
 provided
org.glassfish.web javax.el 2.2.4
 provided

3. tomcat/lib/*.jar逐个加入。

其实还有一种情况,因为后面增加的lib都是通过provided或者test scope增加的,而tomcat/lib是先行读入的,所以两者的优先顺序是不一样的,这可能造成在tomcat上可以正确运行,但在单元测试中可能永远通不过。后来在stackoverflow上偶然发现一个提到hibernate-validator版本的问题,发现真的是这样~~将hibernate-validator版本从6.0.7.Final降到5.2.5.Final可以正确运行了,比5.2.5.Final高的版本都不行!!完整的pom.xml如下:


将dubbo-config和spring-config分开,其中dubbo-config如下:




	
	
	

	
	

	
	


spring-config如下:




	
	
	
	

	
	

	
	
		
		
	
	
	


	

	
		
		
		
		

		
		
		
		
	

	
		
		
	

	
		
	

	

	
		
			
			
				
			
			
			
			
			
			
				
					
						
							
						
					
				
			
		
	

	
		
	

	
	


web.xml基本没变化,就是增加了dubbo-config.xml,内容如下:




	
	
		contextConfigLocation
		/WEB-INF/spring/root-context.xml, /WEB-INF/spring/appServlet/servlet-context.xml, /WEB-INF/spring/appServlet/dubbo-context.xml
	

	
		com.alibaba.dubbo.remoting.http.servlet.BootstrapListener
	

	
		org.springframework.web.context.ContextLoaderListener
	

	
		dispatcher
		com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet
		1
	

	
		dispatcher
		/*
	


root-config.xml因为采用了eclipse插件spring-sts创建的spring mvc project,其内容除了主标签外都是空的:



	
	
		


单元测试代码示例:

package com.abel.bmserver.mapper

import com.abel.bigmeter.service.UserService
import com.abel.bigwater.model.LoginRequest
import com.abel.bmserver.impl.UserServiceImpl
import com.alibaba.fastjson.JSON
import org.junit.Test
import org.junit.runner.RunWith
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
import org.springframework.util.DigestUtils
import java.time.LocalDateTime
import kotlin.test.assertTrue

@RunWith(SpringJUnit4ClassRunner::class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml",
        "file:src/main/webapp/WEB-INF/spring/root-context.xml")
class MapperTest: AbstractJUnit4SpringContextTests() {
    private val log: Logger = LoggerFactory.getLogger(MapperTest::class.java)

    @Autowired
    private var userMapper: UserMapper? = null

    @Autowired
    private var userService: UserServiceImpl? = null

    @Test
    fun testUserMapper() {
        log.info("prepare to test UserMapper...")

        val userList = userMapper?.listUser("1", null, null)
        log.info("User count: ${userList?.count()}")
    }

    @Test
    fun testUserMapperNameStr() {
        log.info("prepare to test userMapper with nameStr")

        val userList = userMapper?.listUser("1", "a%", null)
        log.info("user count like Li%: ${userList?.count()}")
        for (user in userList!!) {
            log.info("user ${user.name} (${user.id})")
        }
    }

    /**
     * Test for login from web.
     */
    @Test
    fun testUserLoginSid() {
        log.info("prepare to test user-login with sid.")
        val lr = LoginRequest()
        lr.sessionId = "4b4cb9f0-6dba-4511-8912-055ac5dc1d62"
        val r1 = userService?.login(lr)
        log.warn("user login with sessionId result: ${r1?.single?.id} (${r1?.single?.firmName}, ${r1?.single?.userName})")
    }

    @Test
    fun testUserLoginSidFail() {
        log.info("prepare to test user-login with non-exist sid.")
        val lr = LoginRequest()
        lr.sessionId = "1111-2222"
        val r1 = userService?.login(lr)
        log.warn("user login with sessionId result: ${r1?.single?.id} (${r1?.code}, ${r1?.error})")
        assertTrue(r1?.code ?: 0> 0, "non-exist sessionId: ${lr.sessionId}")
    }

    @Test
    fun testUserLoginUser() {
        log.info("prepare to test user-login with user.")

        val user = userMapper?.selectUser("anti")

        val lr = LoginRequest()
        lr.userId = "anti"
        lr.timestamp = LocalDateTime.now().toString()
        lr.clientHash = DigestUtils.md5DigestAsHex((user?.passHash + lr.timestamp).toByteArray())
        lr.devId = "JUnit"
        log.info("gson login request:\n ${JSON.toJSONString(lr)}")

        val r1 = userService?.login(lr)
        log.info("gson:\n ${JSON.toJSONString(r1)}")
        log.warn("user login with userId & client-hash result: ${r1?.single?.id} (${r1?.single?.firmName}, ${r1?.single?.userName})")
    }

    @Test
    fun testUserLoginUserFail() {
        log.info("test user-login with user fail")

        val lr = LoginRequest()
        lr.userId = "anti"
        lr.timestamp = LocalDateTime.now().toString()
        lr.clientHash = DigestUtils.md5DigestAsHex(("1234" + lr.timestamp).toByteArray())
        lr.devId = "JUnit"

        val r1 = userService?.login(lr)
        log.warn("user login with userId & client-hash result: ${r1?.single?.id} (${r1?.code}, ${r1?.error})")
        assertTrue(r1?.code ?: 0> 0, "incorrect client-hash: ${lr.sessionId}")
    }
}

至此就可以开始正式工作了,祝贺一下:D

再重新发布一下pom.xml,虽然用idea编写kotlin比较方便,但在dependency管理方面真不如eclipse,eclipse可以通过依赖层级关系来逐个查看依赖的冲突情况(由此来看maven在依赖冲突管理方面还是比较复杂的,其实到了这种情况心里还是有点小膈应):



	4.0.0
	com.abel
	bmserver
	bmserver
	war
	1.0.0-BUILD-SNAPSHOT
	
		1.8
		3.2.9.RELEASE
		1.6.10
		1.7.6
		1.2.10
	
	
		
		
			org.springframework
			spring-context
			${org.springframework-version}
			
				
				
					commons-logging
					commons-logging
				 
			
		
		
			org.springframework
			spring-webmvc
			${org.springframework-version}
		
		
			org.springframework
			spring-tx
			${org.springframework-version}
		
		
			org.springframework
			spring-jdbc
			${org.springframework-version}
		
				
		
		
			org.aspectj
			aspectjrt
			${org.aspectj-version}
			
		
		
		
			org.slf4j
			slf4j-api
			${org.slf4j-version}
		
		
			org.slf4j
			jcl-over-slf4j
			${org.slf4j-version}
			runtime
		
		
			org.slf4j
			slf4j-log4j12
			${org.slf4j-version}
			runtime
		
		
			log4j
			log4j
			1.2.17
			
				
					javax.mail
					mail
				
				
					javax.jms
					jms
				
				
					com.sun.jdmk
					jmxtools
				
				
					com.sun.jmx
					jmxri
				
			
			runtime
		

		
		
			javax.inject
			javax.inject
			1
		
				
		
		
			javax.servlet
			servlet-api
			2.5
			provided
		
		
			javax.servlet.jsp
			jsp-api
			2.1
			provided
		
		
			javax.servlet
			jstl
			1.2
		
		
		
			com.alibaba
			dubbo
			2.8.4
		
        
            javax.validation
            validation-api
            2.0.1.Final
        
        
            org.hibernate
            hibernate-validator
            5.2.5.Final
        

		
			org.jboss.resteasy
			resteasy-jaxrs
			3.1.4.Final
		
		
			org.jboss.resteasy
			resteasy-client
			3.1.4.Final
		
		
			org.jboss.resteasy
			resteasy-jackson-provider
			3.1.4.Final
		
		
			org.codehaus.jackson
			jackson-mapper-asl
			1.9.13
		
		
			com.fasterxml.jackson.core
			jackson-databind
			2.3.3
		

		
		
			junit
			junit
			4.7
			test
		

		
			org.mybatis
			mybatis
			3.4.5
		
		
			org.mybatis
			mybatis-spring
			1.3.0
		
		
			com.alibaba
			druid
			1.0.15
		
		
			mysql
			mysql-connector-java
			6.0.6
		

		
			com.abel
			bigmeter-model
			1.0
		
		
			com.abel
			bigmeter-webapi
			1.0
		
		
			org.jetbrains.kotlin
			kotlin-stdlib-jdk8
			${kotlin.version}
		
		
			org.jetbrains.kotlin
			kotlin-test
			${kotlin.version}
			test
		
		
			org.springframework
			spring-test
			${org.springframework-version}
			test
		
	

    
        
            
                maven-eclipse-plugin
                2.9
                
                    
                        org.springframework.ide.eclipse.core.springnature
                    
                    
                        org.springframework.ide.eclipse.core.springbuilder
                    
                    true
                    true
                
            
			
                org.codehaus.mojo
                exec-maven-plugin
                1.2.1
                
                    org.test.int1.Main
                
            

			
				org.codehaus.mojo
				build-helper-maven-plugin
				3.0.0
				
					
						add-source
						generate-sources
						add-source
						
							
								src/main/java
								src/main/kotlin
							
						
					
					
						test-source
						generate-test-sources
						add-test-source
						
							
								src/test/java
								src/test/kotlin
							
						
					
				
			

			
				org.jetbrains.kotlin
				kotlin-maven-plugin
				${kotlin.version}
				
					
						compile
						compile
						
							compile
						
					
					
						test-compile
						test-compile
						
							test-compile
						
					
				
				
					1.8
				
			
			
                org.apache.maven.plugins
                maven-compiler-plugin
                2.5.1
				
					
						compile
						compile
						
							compile
						
					
					
						testCompile
						test-compile
						
							testCompile
						
					
				
				
                    1.8
                    1.8
                    -Xlint:all
                    true
                    true
					UTF-8
                
            

			
				org.apache.maven.plugins
				maven-war-plugin
				2.2
				
					bmserver
				

				
					
						make-war
						package
						war
					
				
			
		
    


你可能感兴趣的:(dubbox,spring,dubbox,junit,unit,test)