创建普通的spring boot项目
说明:不需要打勾任何选项
目录结构
BookService类
package com.example.service;
public interface BookService {
public void save();
}
BookServiceImpl类
package com.example.service.impl;
import com.example.service.BookService;
import org.springframework.stereotype.Service;
@Service
public class BookServiceImpl implements BookService {
@Override
public void save() {
System.out.println("Book Service is running...");
}
}
Springboot07TestApplicationTests类
BooService 加上@autowired
测试方法用contextLoads(), 或则save()都行
package com.example;
import com.example.service.BookService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot07TestApplicationTests {
@Autowired
private BookService bookService;
@Test
//测试一
void contextLoads() {
bookService.save();
}
@Test
//测试二
public void save(){
bookService.save();
}
}
运行结果:
contextLoads()方法
save()方法
说明:
Springboot07TestApplicationTests 类的注解@SpringBootTest作用是做配置。
如果下图中两个报名不一样,那么Springboot07TestApplicationTests的类的注解要这样写:
@SpringBootTest(classes = Springboot07TestApplication.class)



