1.在pom.xml中添加junit環境的依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency>
2.在src/test/java下建立測試類
例:
@RunWith(value = SpringJUnit4ClassRunner.class)
@SpringBootTest(classes={app.class})
public class serviceTest {
@Autowired
private serviceImpl serviceimpl;
@Test
public void testAdd() {
this.serviceimpl.add();
}
}
說明:@RunWith:啟動器
SpringJUnit4ClassRunner.class:讓 junit 與 spring 環境進行整合
@SpringBootTest(classes={App.class}) 1,當前類為 springBoot 的測試類2,加載 SpringBoot 啟動類。啟動SpringBoot
