這篇文章給大家介紹springboot中怎么實現單元測試,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
1. 在類上使用注解:
@RunWith(SpringRunner.class)
@SpringBootTest
@RunWith(SpringRunner.class)@SpringBootTestpublic class RobotsApplicationTests { @Autowired DataSource dataSource; @Test public void test(){ System.out.println(dataSource.getClass()); }}
2. 繼承CommandLineRunner接口
CommandLineRunner:表示在項目啟動完成后 會執行該功能,只需將測試的內容寫在其run()方法中,如:
@SpringBootApplication@EnableScheduling@ComponentScan(basePackages={"com.cmit.hall.plat","com.cmit.hall.pub"}) @ServletComponentScan(value= {"com.cmit.hall.pub.interceptor","com.cmit.hall.plat.config","com.cmit.hall.pub.session"})@EnableRedisHttpSession(maxInactiveIntervalInSeconds=1800)public class PlatApp implements CommandLineRunner { @Autowired DataSource dataSource; public static void main(String[] args) { SpringApplication.run(PlatApp.class, args); } @Override public void run(String... args) throws Exception { System.out.println(">>>>>>>>>>>>>>>服務啟動執行,執行加載數據等操作<<<<<<<<<<<<<"); System.out.println("DATASOURCE = " + dataSource); }}
關于springboot中怎么實現單元測試就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。