SpringBoot项目的快速搭建可参考:构建SpringBoot项目 (分为两部分:第一部分是project的基本信息,根据当前开发环境选择;第二部是扩展部分,添加不同的jar包支持。生成项目之后直接导入本地开发工具即可)。
SpringBoot 支持junit单元测试,编写测试类之前需要添加spring-boot-starter-test 依赖,如果构建项目时已添加直接忽略这一步,pom中添加依赖:
<!-- 单元测试jar -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
接下来新建测试类TestFileInfoService:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class TestFileInfoService {
@Autowired
private FileInfoService fileInfoService;
@Test
public void TestGetFileList() {
List<FileVO> list = fileInfoService.getFileList();
Assert.assertTrue(list.size() == 4);
}
}
@RunWith :SpringJunit支持
@SpringBootTest : SpringBoot启动支持,Application为工程的启动类
Junit的基本注解和用法在这里基本上都支持,配置文件的属性值可以直接用@Value注解获取,例如:
@Value("${spring.mail.username}")
private String from;
为测试类添加@RunWith和@SpringBootTest两个注解之后就可以进行Junit测试了,这样可以在不用编写controller类的情况下进行业务逻辑的测试。