@Configuration
@ComponentScan
@ImportResource("classpath*: soundsystem/BlankDisc.xml")
public class TrackCounterConfig {
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TrackCounterConfig.class)
public class TrackCounterTest {}
在javaConfig中用@ImportResource把xml導入進去,然後junit測試時報錯,提示錯誤大概是xml中的內容被忽略。
但是如果把配置只寫在Javaconfig或者Xml中,junit測試就能正確運行
後來我嘗試:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TrackCounterConfig.class,locations = "classpath*: soundsystem/BlankDisc.xml")
public class TrackCounterTest {}
在junit @ContextConfiguration中雙導入,也同樣報錯,提示錯誤是classes和locations不能同時使用,這時我猜測應該是junit的作者偷懶!!!假如你在@ContextConfiguration中寫的是Classes,junit掃描的時候就只會掃描class文件,而忽略所有的xml文件,location時也是同理。(我用的是IDEA+junit4情況是如此,其他用Ecplise或者junit5就不知道是不是這樣了)
還有在
locations = "classpath*: soundsystem/BlankDisc.xml
中classpath後面儘量寫個*,不然會報錯。
在寫Spring AOP xml配置時,要導入幾個aspectj的包,不然也會報錯,詳情請看