最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決@springboottest注解無法加載src/main/resources目錄下文件

 更新時間:2022年01月19日 10:03:30   作者:FollowYourHeart2015  
這篇文章主要介紹了解決@springboottest注解無法加載src/main/resources目錄下文件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Springboot微服務框架是目前越來越流行的框架,省去了很多繁瑣的xml配置。最近新啟了個項目,采用SpringBoot框架從頭搭建,中間也遇到過各種坑,現(xiàn)在先描述一下 Junit4單元測試之坑吧。

結論

@SpringBootTest注解,只會加載test路徑下的資源文件(即xml配置),并不會加載main路徑下的資源文件,這點很坑。。。

環(huán)境及問題描述

1.SpringBoot版本:2.0.0.RELEASE

2.通過項目入口類啟動Application.main() OK,能正常處理web請求

3.啟動單元測試,執(zhí)行測試類

提示Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.xx.xxx.service.SsoService' available

如下:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-02-02 16:26:10.537 ERROR 7812 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

A component required a bean of type 'com.xx.xxx.service.SsoService' that could not be found.


Action:

Consider defining a bean of type 'com.xx.xxx.service.SsoService' in your configuration.

2019-02-02 16:26:10.541 ERROR 7812 --- [           main] o.s.test.context.TestContextManager      : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@93b025] to prepare test instance [com.jd.id.activity.ControllerTest@b9178]

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125) ~[spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:107) ~[spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190) ~[spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132) ~[spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:242) ~[spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227) [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246) [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) [spring-test-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137) [junit-4.12.jar:4.12]
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) [junit-rt.jar:na]
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51) [junit-rt.jar:na]
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) [junit-rt.jar:na]
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) [junit-rt.jar:na]

…………

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.xx.xxx.service.SsoService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1509)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:506)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:484)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:618)
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:177)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:318)
    ... 41 more


Process finished with exit code -1

問題分析

1.首先com.xx.xxx.service.SsoService該類存在

并且項目正常啟動時,并沒有這個錯誤提示,那肯定是單元測試配置的有問題了,首先檢查下相關的配置,@RunWith(SpringRunner.class)使用SpringRunner以便在測試開始的時候自動創(chuàng)建spring的應用上下文,沒毛病。

@SpringBootTest(classes = 啟動類.class)配置也沒問題,網(wǎng)上有討論說springboot 1.4以下版本,要使用@SpringApplicationConfiguration(classes = 啟動類.class),而我的項目版本是2.0,顯然這個配置也沒毛病。

代碼如下:

package com.xx.xxx.activity;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

/**
 * 單元測試基類
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
//由于是Web項目,Junit需要模擬ServletContext,因此我們需要給我們的測試類加上@WebAppConfiguration。
@WebAppConfiguration
public class BaseTest
{
    @Before
    public void init() {
        System.out.println("開始測試----------------");
    }

    @After
    public void destory() {
        System.out.println("結束測試----------------");
    }
}

2.再看下pom文件的配置

引用了對應的test包,也沒毛病

? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? ? <artifactId>spring-boot-starter-test</artifactId>
? ? ? ? ? ? <version>${springboot.version}</version>
? ? ? ? ? ? <scope>test</scope>
? ? ? ? </dependency>

3.這個類是在src/main/resources目錄下的資源文件里配置

即xml中配置的bean,而src/test下并沒有resources目錄,莫非是這個原因?于是乎在test目錄下創(chuàng)建resources路徑,并把xml文件拷貝至test對應的路徑下,結果能夠正常進行單元測試了,數(shù)據(jù)也寫入到數(shù)據(jù)庫中,終于松了口氣 -

準備就此作罷,不想在這個問題上浪費時間了,但是再做考慮,這樣做欠妥,畢竟我每次改完xml文件的配置,必須得拷貝到test目錄對應的文件夾下,這拷來拷去,并不是最好的解決方案啊,能不能打包時,把src/main/resources的資源文件也拷貝到src/test/resources中?這樣就不用手工拷貝了。

答案是:當然有了,看pom.xml配置:

<build>
        <finalName>id-web-activity-root</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
        <!--單元測試時引用src/main/resources下的資源文件-->
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
            </testResource>
            <testResource>
                <directory>src/main/resources</directory>
            </testResource>
        </testResources>
    </build>

這時再去執(zhí)行單元測試,正常執(zhí)行,同時test-classes路徑下已經(jīng)有了xml資源配置文件,如下圖:

打包前

打包后

單元測試的坑算是填了,再接著填其他坑吧。。。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • java實現(xiàn)策略模式使用示例

    java實現(xiàn)策略模式使用示例

    在使用圖像處理軟件處理圖片后,需要選擇一種格式進行保存。然而各種格式在底層實現(xiàn)的算法并不相同,這剛好適合策略模式。編寫程序,演示如何使用策略模式與簡單工廠模式組合進行開發(fā)
    2014-02-02
  • Java中的CopyOnWriteArrayList解析

    Java中的CopyOnWriteArrayList解析

    這篇文章主要介紹了Java中的CopyOnWriteArrayList解析,ArrayList是非線程安全的,也就是說在多個線程下進行讀寫,會出現(xiàn)異常,既然是非線程安全,那我們就使用一些機制把它變安全不就好了,需要的朋友可以參考下
    2023-12-12
  • java實現(xiàn)左旋轉字符串

    java實現(xiàn)左旋轉字符串

    這篇文章主要為大家詳細介紹了java實現(xiàn)左旋轉字符串,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • java開發(fā)hutool HttpUtil網(wǎng)絡請求工具使用demo

    java開發(fā)hutool HttpUtil網(wǎng)絡請求工具使用demo

    這篇文章主要為大家介紹了hutool之HttpUtil網(wǎng)絡請求工具使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • SpringBoot中的自動裝配原理詳解

    SpringBoot中的自動裝配原理詳解

    本文將通過在Spring中集成MyBatis和在SpringBoot中集成MyBatis為大家簡單梳理自動配置過程,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-11-11
  • 基于Spring-Security自定義登陸錯誤提示信息

    基于Spring-Security自定義登陸錯誤提示信息

    這篇文章主要介紹了Spring-Security自定義登陸錯誤提示信息,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 解決執(zhí)行maven命令時提示Process terminated的問題

    解決執(zhí)行maven命令時提示Process terminated的問題

    這篇文章主要介紹了解決執(zhí)行maven命令時提示Process terminated的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • 為何修改equals方法時還要重寫hashcode方法的原因分析

    為何修改equals方法時還要重寫hashcode方法的原因分析

    這篇文章主要介紹了為何修改equals方法時還要重寫hashcode方法的原因分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java中Final關鍵字的使用技巧及其性能優(yōu)勢詳解

    Java中Final關鍵字的使用技巧及其性能優(yōu)勢詳解

    這篇文章主要介紹了Java中Final關鍵字的使用技巧及其性能優(yōu)勢詳解,Java中的final關鍵字用于修飾變量、方法和類,可以讓它們在定義后不可更改,從而提高程序的穩(wěn)定性和可靠性,此外,final關鍵字還有一些使用技巧和性能優(yōu)勢,需要的朋友可以參考下
    2023-10-10
  • 詳解JNI到底是什么

    詳解JNI到底是什么

    JNI是Java Native Interface的縮寫,通過使用 Java本地接口書寫程序,可以確保代碼在不同的平臺上方便移植。從Java1.1開始,JNI標準成為java平臺的一部分,它允許Java代碼和其他語言寫的代碼進行交互
    2021-06-06

最新評論

河源市| 新河县| 琼中| 阿瓦提县| 安陆市| 吴桥县| 政和县| 长垣县| 曲周县| 壤塘县| 自治县| 伽师县| 聊城市| 汶上县| 马尔康县| 福清市| 西昌市| 五寨县| 元氏县| 营山县| 博湖县| 沙湾县| 泸水县| 湖北省| 遵义市| 大埔区| 白山市| 池州市| 平塘县| 贡山| 凤凰县| 吉木乃县| 诸城市| 华亭县| 儋州市| 武鸣县| 彩票| 伊金霍洛旗| 安仁县| 时尚| 阜新市|