在學習springboot的過程中 每次都要寫controller來訪問測試比較麻煩,其他我們可以通過springboot的測試直接測試
步驟
- 導入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>
- 創建一個SpringBoot應用,並在下面創建一個Bean
- 寫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();
}
}