一、eclipse 安装sts插件
Help-->Eclipse MraketPlace
二、安装完成新建一个demo 项目
注意:如果安装没有问题但是新建项目的时候找不到spring相关,只需要将jdk改成1.8版本的就可以了。
新建项目完成后创建一个helloController,注意目录结构
HelloController
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "hello,world!";
}
}
运行主程序,访问http://localhost:8080/test/hello 返回
编写单元测试用例
package com.example.demo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.example.demo.controller.HelloController;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DemoApplication.class)
@WebAppConfiguration
public class DemoApplicationTests {
@Autowired
HelloController helloController;
@Test
public void getHello() throws Exception {
System.out.println(helloController.hello());
}
}
在控制台查看结果即可。