spring-cloud

springboot单元测试

2022-10-12  本文已影响0人  煮茶听雨

单元测试步骤

  1. 添加依赖

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

            <scope>test</scope>

        </dependency>

  1. 添加测试父类

指定启动类,端口随机


@RunWith(SpringRunner.class)

@SpringBootTest(classes = WksixApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

public class WksixApplicationTests {

    @Test

    public void contextLoads() {

    }

}

  1. 测试具体service,只要继承上面的测试父类即可

public class ArticleServiceImplTest extends WksixApplicationTests {

    @Autowired

    ArticleService articleService;

    @Test

    public void test(){

        articleService.testTransaction();

    }

}

@Test 导入的是 org.junit.Test

上一篇 下一篇

猜你喜欢

热点阅读