增加 maven 依赖
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
HelloService.java
- package com.zns.service;
- import org.springframework.stereotype.Service;
- @Service
- public class HelloService {
- public String getName(){
- return"hello";
- }
- }
HelloServiceTest.java
- package com.zns.test;
- import javax.annotation.Resource;
- import org.junit.Assert;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
- import com.zns.MyApplication;
- import com.zns.service.HelloService;
- @RunWith(SpringJUnit4ClassRunner.class)
- @SpringBootTest(classes = MyApplication.class)
- public class HelloServiceTest {
- @Resource
- private HelloService helloService;
- @Test
- public void testGetName() {
- Assert.assertEquals("hello", helloService.getName());
- }
- }
这时候就可以进行测试了, 右键 - RunAs - Junit Test.
来源: http://www.bubuko.com/infodetail-2957327.html