1.添加依赖
<dependency>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
2.目录结构
3.编写代码
TestApp.java
import org.junit.After; import org.junit.Before; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.web.WebAppConfiguration; @RunWith(SpringRunner.class) @SpringBootTest(classes = TestApp.class) @WebAppConfiguration public class TestApp { @Before public void init() { System.out.println("Begin-----------------"); } @After public void after() { System.out.println("End-----------------"); } }
RealTest.java
import org.junit.Test; import java.util.stream.IntStream; public class RealTest extends TestApp{ @Autowired private TestService testservice; @Test public void test(){ IntStream.range(0,10).forEach(i -> System.out.println(i)); } @Test public void test1(){ IntStream.rangeClosed(0,10).forEach( i -> System.out.println(i)); } }
4.想跑那个测试方法点哪个就行了