第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

@Autowired 字段在測試中始終為空,我錯過了什么?

@Autowired 字段在測試中始終為空,我錯過了什么?

海綿寶寶撒 2024-01-28 16:30:05
測試性能:package com.sandbox.test;import lombok.Getter;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component;@Component@PropertySource("classpath:new-test.properties")public class TestProperties {    @Getter    @Value("${homepage.url}")    private String homePageUrl;}配置:package com.sandbox.test;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan(basePackages = {"com.sandbox"})public class SpringContext {}new-test.properties位于 /src/test/resources 中的文件內(nèi)容:homepage.url=https://tst.mysite.com課堂上進行了兩次測試MyTest,第一個 - 不起作用,第二個 - 工作正常:package com.sandbox.test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.test.context.ContextConfiguration;import org.testng.Assert;import org.testng.annotations.Test;@ContextConfiguration(classes = {SpringContext.class})public class MyTest {    @Autowired    private TestProperties testProperties;    @Test    public void thisDoesntWork() {        Assert.assertNotNull(testProperties);        System.out.println(testProperties.getHomePageUrl());    }    @Test    public void thisWorks() {        AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext(SpringContext.class);        TestProperties testProps = appContext.getBean(TestProperties.class);        Assert.assertNotNull(testProps);        System.out.println(testProps.getHomePageUrl());    }}目標是在不使用 xml 的情況下自動裝配類testProperties中的字段。但目前是. 注釋已經(jīng)到位,但我缺少什么?...MyTestnull@Component@ComponentScan
查看完整描述

1 回答

?
吃雞游戲

TA貢獻1829條經(jīng)驗 獲得超7個贊

您的測試可能需要擴展?AbstractTestNGSpringContextTests才能自動訪問ApplicationContext并使自動裝配工作。



查看完整回答
反對 回復(fù) 2024-01-28
  • 1 回答
  • 0 關(guān)注
  • 168 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號