測(cè)試性能: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課堂上進(jìn)行了兩次測(cè)試MyTest,第一個(gè) - 不起作用,第二個(gè) - 工作正常: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()); }}目標(biāo)是在不使用 xml 的情況下自動(dòng)裝配類testProperties中的字段。但目前是. 注釋已經(jīng)到位,但我缺少什么?...MyTestnull@Component@ComponentScan
1 回答

吃雞游戲
TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
您的測(cè)試可能需要擴(kuò)展?AbstractTestNGSpringContextTests才能自動(dòng)訪問ApplicationContext并使自動(dòng)裝配工作。
添加回答
舉報(bào)
0/150
提交
取消