- 建立第一个hello类
package xucompany.demoss.hello;
import org.springframework.stereotype.Service;
@Service
public class HelloService {
public void sayHello(){
System.out.println("你好,世界");
}
}
2:进行单元测试
package xucompany.demoss.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import xucompany.demoss.hello.HelloService;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloServiceTest {
@Resource
HelloService helloService;
@Test
public void HelloServiceTest() {
helloService.sayHello();
}
}
3:@RunWith(SpringRunner.class)的用法
1: Springboot的@RunWith(SpringRunner.class)
注解的意义在于Test测试类要使用注入的类,比如@Autowired注入的类, 有了@RunWith(SpringRunner.class)这些类才能实例化到spring容器中,自动注入才能生效