Springboot(3)——Springboot的測試

在學習springboot的過程中 每次都要寫controller來訪問測試比較麻煩,其他我們可以通過springboot的測試直接測試

步驟

  1. 導入spring-boot-starter-test
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
</dependency>
  1. 創建一個SpringBoot應用,並在下面創建一個Bean
  2. 寫SpringBoot測試從Springboot中獲取bean就ok
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class) //這是一個Spring測試,要告訴它在哪兒加載Spring配置文件,其實告訴它應用類型就ok
public class SpringbootTest {
	@Autowired
	private TestService testService;
	@Test
	public void test() throws Exception {
		System.out.println(testService);
		testService.test();
	}
}

測試完成!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章