欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

結(jié)論

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

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

1.SpringBoot版本:2.0.0.RELEASE

2.通過項(xiàng)目入口類啟動(dòng)Application.main() OK,能正常處理web請(qǐng)求

3.啟動(dòng)單元測試,執(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該類存在

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

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

代碼如下:

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項(xiàng)目,Junit需要模擬ServletContext,因此我們需要給我們的測試類加上@WebAppConfiguration。
@WebAppConfiguration
public class BaseTest
{
    @Before
    public void init() {
        System.out.println("開始測試----------------");
    }

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

2.再看下pom文件的配置

引用了對(duì)應(yīng)的test包,也沒毛病

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

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

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

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

答案是:當(dāng)然有了,看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>
        <!--單元測試時(shí)引用src/main/resources下的資源文件-->
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
            </testResource>
            <testResource>
                <directory>src/main/resources</directory>
            </testResource>
        </testResources>
    </build>

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

打包前

打包后

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

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

相關(guān)文章

最新評(píng)論