SpringBoot使用Junit
2017-04-10 00:00
489 查看
SpringBoot使用Junit
相关代码:github OSCchina
引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
测试
1.我们需要测试的类@Service public class HelloWorldService { public void sayHello() { System.out.println("Hello world"); } }
2.测试
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = App.class) public class HelloWorldServiceTest { @Autowired private HelloWorldService helloWorldService; @Test public void sayHello() throws Exception { helloWorldService.sayHello(); } }
注解说明
1.@RunWith(SpringJUnit4ClassRunner.class)用来引入SpringJunit支持.2.@SpringBootTest(classes = App.class)用来加载我们的启动类,也就是启动Spring容器,这是1.4之后的
注解,我们也可以用SpringApplicationConfiguration这个注解.
3.@WebAppConfiguration,如果是web项目,我们再测试时需要加载ServletContext的话,就需要加上这个注解.
相关文章推荐
- SpringBoot 使用JUnit 单元测试
- 在Eclipse中使用JUnit进行单元测试
- 如何使用Junit编写和组织测试程序
- JbuilderX使用Junit学习笔记
- junit的使用
- Eclipse中JUnit使用基础
- 使用Junit时源代码和测试代码的组织(摘自Daping_Zhang 的 Blog )
- junit使用简明手册
- Eclipse学习4-在Eclipse中使用JUnit进行单元测试(下)
- JUnit的使用
- 使用ant编译、运行junit、以及检查编码
- 利用 Ant 和 JUnit 进行增量开发-使用单元测试来逐步改进代码
- 使用ant编译、运行junit、以及检查编码
- JbuilderX使用Junit学习笔记
- 在Eclipse中使用JUnit
- junit使用简明手册
- 在Eclipse中使用JUnit(翻译)
- JUnit的框架设计及其使用的设计模式
- 在Eclipse中使用JUnit(翻译)
- 单元测试之道 -使用JUnit