Spring Boot 单元测试

好程序员是会测试的,显然,我可能不太合格。

我的项目架构

    • MAVEN

    • JAVA 8

    • SPRING BOOT

    • MYBATIS / MYBATIS PLUS

    • REDIS

    • TOMCAT

    • ==

下面是在我的项目中使用的单元测试代码主体架构:

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import java.util.Date;

@Slf4j
@SpringBootTest(classes = XXXXClass.class )
@RunWith(SpringRunner.class)
@WebAppConfiguration
@ActiveProfiles(value = "xxxPro")
public class scratch_6 {

    @Autowired
    private XXXService service;

    @Test
    public void test() {

    }
}

在这个过程中可能会遇到某个配置文件找不到的情况,可以在pom.xml文件中修改一下打包构建的配置,如下:

<!--单元测试时引用src/main/resources下的资源文件-->
<testResources>
    <testResource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
            <include>bootstrap-${profileActive}.yml</include>
            <include>bootstrap.yml</include>
        </includes>
    </testResource>
</testResources>

祝:诸君好运,代码永无BUG

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

滚动至顶部