pom中加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
<scope>test</scope>
</dependency>
测试类的注解
IDEA工具中可以快捷生成测试类(光标放在指定类名上,Alt+Enter即可生成测试类)
测试类上注解只需要
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = TestRunner.class)
@SpringBootTest(classes=.class) = @SpringApplicationConfiguration(1.4.0之后不再使用)
在测试类中引入需要注入的Bean即可成功,再加上断言 单元测试编写成功
————–华丽丽的分界线————
But……我们的项目service的单元测试中mapper接口怎么也无法注入,报错信息如下:大概意思就是说我们的mapper接口注入失败blabla的。
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.pinganfu.dayu.service.LoginServiceTest': Unsatisfied dependency expressed through field 'userMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.pinganfu.dayu.dal.mapper.UserMapper' avai