pom文件中添加依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
创建Test类
类上添加注解 @SpringBootTest、@RunWith
具体方法要添加 @Test 注解
根路径要与启动类相同
如果不相同,就要写
@SpringBootTest(classes = VsecApplication.class)
如果报错,找不到上下文
@SpringBootTest(classes = VsecApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
完整代码
@SpringBootTest(classes = VsecApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
//如果此文件所在目录跟启动类所在目录不一致就要加上classes = FirmwareApplication.class
//webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT是解决找不到spring上下文对象
public class Excel2DbTest {
@Autowired
private InfoMapper infoMapper;
@Test
public void excel2Db (){
infoMapper.insert(info);
}