使用注解
@RunWith(SpringRunner.class)
@SpringBootTest(classes = StudyTestApplication.class)
注意当测试类包名与启动类包名相同或者在其子包下。使用@SpringBootTest 不需要在括号内注明启动类
package test;
import com.studytest.StudyTestApplication;
import com.studytest.service.UserService;
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.SpringRunner;
@RunWith(SpringRunner.class)
//包名与启动类不同,在SpringBootTest中注明启动类
@SpringBootTest(classes = StudyTestApplication.class)
//@SpringBootTest
public class UserServiceTest {
@Autowired
UserService userService;
@Test
public void testAdd() {
System.out.println("testAdd");
userService.add();
}
}