1. 程式人生 > >Spring mvc Junit單元測試異常解決

Spring mvc Junit單元測試異常解決

今天在做單元測試的時候出現了各種異常情況。可是專案通過Tomcat容器釋出執行沒有任何問題,專案框架是spring 4.3.3 + mybatis。

問題一:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sodbConfig' defined in file [E:\Cas-sso\workspaces\haycStudioCas\target\classes\spring\spring-config.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [java.lang.Integer] for property 'apiReqTimeout'; nested exception is java.lang.NumberFormatException: For input string: "${hayc.api.request.timeout}"
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128) ~[spring-test-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60) ~[spring-test-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:108) ~[spring-test-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:251) ~[spring-test-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) ~[spring-test-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) ~[spring-test-4.3.3.RELEASE.jar:4.3.3.RELEASE]
... 25 common frames omitted
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [java.lang.Integer] for property 'apiReqTimeout'; nested exception is java.lang.NumberFormatException: For input string: "${xxxx.api.request.timeout}"
at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:596) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:603) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:216) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1532) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1491) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1231) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
... 39 common frames omitted
Caused by: java.lang.NumberFormatException: For input string: "${hayc.api.request.timeout}"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_161]
at java.lang.Integer.parseInt(Integer.java:569) ~[na:1.8.0_161]
at java.lang.Integer.valueOf(Integer.java:766) ~[na:1.8.0_161]
at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:208) ~[spring-core-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.propertyeditors.CustomNumberEditor.setAsText(CustomNumberEditor.java:113) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:468) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:441) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:199) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:576) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
... 45 common frames omitted


問題描述:是由於專案中使用了property-placeholder 特性,專案部署釋出都一切正常,可是在使用junit的時候卻出了問題。


解決辦法:將上述方式改為如下

<bean id="propertyPlaceHolderConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:application.properties</value>
                <value>classpath:xxxxConfig.properties</value>
            </list>
        </property>
    </bean>

問題二:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSocketHandlerMapping' defined in class path resource [org/springframework/web/socket/config/annotation/DelegatingWebSocketConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'webSocketHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No suitable default RequestUpgradeStrategy found
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1128) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1023) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128) ~[spring-test-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60) ~[spring-test-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:108) ~[spring-test-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:251) ~[spring-test-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) ~[spring-test-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) ~[spring-test-4.3.3.RELEASE.jar:4.3.3.RELEASE]
... 31 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'webSocketHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No suitable default RequestUpgradeStrategy found
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
... 48 common frames omitted
Caused by: java.lang.IllegalStateException: No suitable default RequestUpgradeStrategy found
at org.springframework.web.socket.server.support.AbstractHandshakeHandler.initRequestUpgradeStrategy(AbstractHandshakeHandler.java:143) ~[spring-websocket-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.socket.server.support.AbstractHandshakeHandler.<init>(AbstractHandshakeHandler.java:109) ~[spring-websocket-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.socket.server.support.DefaultHandshakeHandler.<init>(DefaultHandshakeHandler.java:35) ~[spring-websocket-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.socket.config.annotation.AbstractWebSocketHandlerRegistration.getOrCreateHandshakeHandler(AbstractWebSocketHandlerRegistration.java:148) ~[spring-websocket-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.socket.config.annotation.AbstractWebSocketHandlerRegistration.getMappings(AbstractWebSocketHandlerRegistration.java:135) ~[spring-websocket-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.socket.config.annotation.ServletWebSocketHandlerRegistry.getHandlerMapping(ServletWebSocketHandlerRegistry.java:98) ~[spring-websocket-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.socket.config.annotation.WebSocketConfigurationSupport.webSocketHandlerMapping(WebSocketConfigurationSupport.java:35) ~[spring-websocket-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.socket.config.annotation.DelegatingWebSocketConfiguration$$EnhancerBySpringCGLIB$$37a53f87.CGLIB$webSocketHandlerMapping$3(<generated>) ~[spring-websocket-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.socket.config.annotation.DelegatingWebSocketConfiguration$$EnhancerBySpringCGLIB$$37a53f87$$FastClassBySpringCGLIB$$a0d93fc5.invoke(<generated>) ~[spring-websocket-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.socket.config.annotation.DelegatingWebSocketConfiguration$$EnhancerBySpringCGLIB$$37a53f87.webSocketHandlerMapping(<generated>) ~[spring-websocket-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_161]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
... 49 common frames omitted
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 5.416 sec <<< FAILURE!

test(com.air.security.sodb.test.Test)  Time elapsed: 0.007 sec  <<< ERROR!


問題解決:在專案中新增如下依賴

<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-websocket</artifactId>
   <version>8.5.28</version>
</dependency>

注意:我使用的spring 4.3.3,如果tomcat-embed-websocket版本為8.5.15還是有問題,而使用8.5.28就完美解決。

問題三:

Caused by: java.lang.IllegalStateException: WebApplicationObjectSupport instance [ResourceHttpRequestHandler [locations=[class path resource [img/]], resolvers=[[email protected]b8b6c0]]] does not run in a WebApplicationContext but in: [email protected]8a102f: startup date [Thu Jun 07 15:04:22 GMT+08:00 2018]; root of context hierarchy
at org.springframework.web.context.support.WebApplicationObjectSupport.getWebApplicationContext(WebApplicationObjectSupport.java:112) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.context.support.WebApplicationObjectSupport.getServletContext(WebApplicationObjectSupport.java:128) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.servlet.resource.ResourceHttpRequestHandler.initPathExtensionStrategy(ResourceHttpRequestHandler.java:310) ~[spring-webmvc-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.web.servlet.resource.ResourceHttpRequestHandler.afterSingletonsInstantiated(ResourceHttpRequestHandler.java:298) ~[spring-webmvc-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:771) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128) ~[spring-test-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60) ~[spring-test-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:108) ~[spring-test-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:251) ~[spring-test-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) ~[spring-test-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) ~[spring-test-4.3.3.RELEASE.jar:4.3.3.RELEASE]
... 31 common frames omitted
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 5.89 sec <<< FAILURE!

test(com.air.security.sodb.test.Test)  Time elapsed: 0.004 sec  <<< ERROR!


問題描述:這是由於系統spring配置檔案中使用了靜態資源配置

問題解決:將靜態資源的配置註釋掉即可


相關推薦

Spring mvc Junit單元測試異常解決

今天在做單元測試的時候出現了各種異常情況。可是專案通過Tomcat容器釋出執行沒有任何問題,專案框架是spring 4.3.3 + mybatis。問題一:Caused by: org.springframework.beans.factory.BeanCreationExc

Spring Boot Junit單元測試

摘要: Junit這種老技術,現在又拿出來說,不為別的,某種程度上來說,更是為了要說明它在專案中的重要性。 憑本人的感覺和經驗來說,在專案中完全按標準都寫Junit用例覆蓋大部分業務程式碼的,應該不會超過一半。 剛好前段時間寫了一些關於SpringBoot的帖子,正好現在把Junit再拿

Spring 整合Junit單元測試

1、在pom增加junit和spring-test <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <v

Spring Mybatis jUnit 單元測試

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:springConfig/spring-db.xml"}) 單個檔案  @ContextCon

(27)Spring Boot Junit單元測試【從零開始學Spring Boot】

Junit這種老技術,現在又拿出來說,不為別的,某種程度上來說,更是為了要說明它在專案中的重要性。 那麼先簡單說一下為什麼要寫測試用例 1. 可以避免測試點的遺漏,為了更好的進行測試,可以提高測試效率 2. 可以自動測試,可以在專案打包前進行測試校驗 3. 可以及時發現因為

Spring Mvc controller單元測試

 弄了一下午的controller測試類,(主要還是不理解)~~~~~ 現在把我測試通過的步驟儲存下來,為需要的人一份借鑑 SpringMVC controller測試較簡單,從功能角度劃分,可分為兩種。一種是呼叫請求路徑測試,另一種是直接呼叫Controller方法測試

基於Spring MVC單元測試 —— 使用JMockit

轉載地址:http://www.techv5.com/topic/696/ 下面再介紹一下通過JMockit這個Java Mock工具來進行spring的單元測試,其特點是不需指定spring的配置檔案,任何物件都可以mock出來並進行關聯。 Controller im

學習 Spring Boot:(二十九)Spring Boot Junit 單元測試

前言 JUnit 是一個迴歸測試框架,被開發者用於實施對應用程式的單元測試,加快程式編制速度,同時提高編碼的質量。 JUnit 測試框架具有以下重要特性: 測試工具 測試套件 測試執行器 測試分類 瞭解 Junit 基礎方法 加入依賴 在 p

SpringJUnit 單元測試基類:AbstractTransactionalJUnit4SpringContextTests

Spring 對 JUNIT的單元測試基類有: AbstractJUnit4SpringContextTests AbstractTransactionalJUnit4SpringContextTests 其 都 可以配合 標籤 @ContextConf

Spring MVC 單元測試異常 Caused by: org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file

read cti exe document ive pri simple fff ces Sping 3.2.8.RELEASE + sping mvc + JDK 1.8運行異常。 java.lang.IllegalStateException: Failed to

spring mvc + junit servlet 單元測試

// spring 版本:4.2.3 // junit 版本:4.12 import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfigurat

Spring整合Spring MVC及Mybatis進行Junit單元測試

我們可以在不啟動服務的情況下,進行單元測試,以便提交出高質量的程式碼。本文以一個小例子,說明在Spring中如何進行單元測試。 一:測試Controller 1:在pom.xml檔案中引入相關依賴 <properties> <!-- 設定專案編碼編碼 --&

Spring mvcJunit 單元測試 Controller中方法

               Springmvc 之Junit 單元測試 1.   首先引入測試的jar包。 1.1因為我用的ide是eclipse,現只介紹eclipse中junit的使用。首先引用eclipse中自帶的junit, 方法: 右鍵專案—>proper

使用Junit單元測試及操作MySQL數據庫時出現錯誤及解決方法

靜態 方法 簡單 註解 info 正常 mage 返回 基本上 在一次看Mybatis視頻學習過程中,教學視頻中用到了Junit單元測試,因為剛開始學習,會出現許多錯誤,每次出現錯誤都會上網搜索,但是發現基本上錯誤都是這麽幾種: 1、沒有@Test註解 2、測試方法用s

Junit單元測試+aop+spring+執行緒池,在進行Junit測試時切面中執行緒池內呼叫的方法不執行

一、問題背景: 寫了一個切面,指向某service包下的所有類及方法,當該service包下方法被呼叫時切面執行,切面中用了執行緒池ExecutorService pool = Executors.newFixedThreadPool(5);執行緒池內呼叫了dao層的方法。 二、問題描述:單

Spring Boot---(11)SpringBoot使用Junit單元測試

摘要:本文詳細的記錄了SpringBoot如何結合Junit寫測試用例,如何執行,打包執行,忽略執行等操作,SpringBoot內建了Junit測試元件,使用很方便,不用再單獨引入其他測試元件。 演示環境: SpringBoot + mybatis 開發工具:IntelliJ IDEA

spring+junit4時用Junit單元測試遇到的bug:java.lang.ExceptionInInitializerError

一、詳細錯誤日誌: 二、具體描述: 由於第一次用spring+Junit4進行單元測試,對他們瞭解不夠深入,出現這個bug時真的是手忙腳亂的,各種百度,各種嘗試就是解決不了,網上好多方法都試了就是不行,現在想來不是網上的解決方法不行,而是我遇到的問題和他們不一樣啊。網上說j

websocket導致spring boot 專案單元測試啟動失敗的問題解決

背景 一個專案中需要實時的向前端展示資料變化,因此使用websocket。但是在專案中增加websocket後,功能沒有問題,但是之前寫的單元測試程式碼都無法執行。(程式碼和報錯貼出來,如下,可以忽略) package com.hengyun.admin; import

使用WebSocket導致jUnit單元測試報No suitable default RequestUpgradeStrategy found錯誤的解決方法 小記

廢話不多說直接貼圖 我們只需要在依賴中新增 <dependency> <groupId>org.apache.tomcat.embed</groupId> &

Spring對Controller、Service、Dao進行Junit單元測試總結

Spring對Controller、Service、Dao進行Junit單元測試總結 [email protected]事務控制,避免資料庫出現髒資料(若要提交到資料庫,先注掉) 2.hibernate配置檔案 <property name="defaultAutoComm