springboottest测试依赖和使用方式

  • A+
所属分类:百科知识

这篇文章主要介绍了springboottest测试依赖和使用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

springboottest测试依赖和使用

1
2
3
4
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>

创建测试类

注意加运行启动注解,和springbootest注解

1
2
3
4
5
6
7
8
9
10
11
12
13
@RunWith(SpringRunner.class)
@SpringBootTest
public class User01MapperTest {
 
    @Autowired
    User01Mapper user01Mapper;
 
    @Test
    public void testQuery(){
        User01 user = user01Mapper.selectByPrimaryKey("张三");
        System.out.println(user);
    }
}

maven无法使用spring test注解

看pom.xml中是否已经引入了spring test的依赖

如果没有,加入以下依赖

1
2
3
4
5
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.1.3.RELEASE</version>
</dependency>

注意:加入新的依赖后,要maven->update project,导入一下spring-test的jar包

如果已经有依赖了,还是不能使用Spring-test的注解,那么看看libraries中的spring-test.jar包,如果是黑色的,如下图。

springboottest测试依赖和使用方式

把依赖中的test去掉,test表示只能在src下的test文件夹使用。

1
2
3
4
5
6
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.1.3.RELEASE</version>
    <!-- <scope>test</scope> -->
</dependency>

以上为个人经验,希望能给大家一个参考。

大家都在看:

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: