Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringBoot应用程序在我运行它时工作,但在我测试它时会崩溃_Java_Spring_Tdd - Fatal编程技术网

Java SpringBoot应用程序在我运行它时工作,但在我测试它时会崩溃

Java SpringBoot应用程序在我运行它时工作,但在我测试它时会崩溃,java,spring,tdd,Java,Spring,Tdd,我有一个带有一个控制器的简单spring应用程序 @RestController public class UserController { // @Autowired // UserServiceImpl userService; @RequestMapping(value="/getUser", method = RequestMethod.GET) public String getUser(){ // return userService.greetUser();

我有一个带有一个控制器的简单spring应用程序

@RestController
public class UserController {

//  @Autowired
//  UserServiceImpl userService;

  @RequestMapping(value="/getUser", method = RequestMethod.GET)
  public String getUser(){
//    return userService.greetUser();
    return "Hello user";
  }
当我启动它时,它就工作了。如果我取消注释
@Autowired
并使用
UserService
使用第一个返回语句运行,它也可以工作

我的服务接口

@Service
public interface UserService {
  String greetUser();
  void insertUsers(List<User> users);
}
但当我测试它时,应用程序会出现以下错误

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.demo.service.UserServiceImpl' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.demo.service.UserServiceImpl' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
考试班

@RunWith(SpringRunner.class)
@WebMvcTest
public class DemoApplicationTests {

  @Autowired
  private MockMvc mockMvc;

    @Test
  public void shouldReturnHelloString() throws Exception{
      this.mockMvc
      .perform(get("/getUser"))
      .andDo(print())
      .andExpect(status().isOk())
      .andExpect(content().string("Hello user"));
  }
}
另外,如果我删除

//  @Autowired
//  UserServiceImpl userService;

并使用第二个return语句运行测试,测试将无错误地执行。我知道问题出在
userserviceinpl
,但我不知道是什么问题。我需要更正什么?

您应该尝试通过接口而不是实现来自动连接bean

@Service
public class UserServiceImpl implements UserService{

  @Override
  public String greetUser() {
    return "Hello user";
  }
}
@Autowired
UserService userService;

此外,您还应该从
UserService
界面中删除
@Service
,这没有帮助。误差基本保持不变。只是现在
没有“com.example.demo.service.UserService”类型的合格bean
您是否已将@ComponentScan添加到配置中?是否将其添加到包含main方法的类中。为什么它以前不工作?如果您添加此注释,Spring容器可以检测并注册您的bean或组件。如果没有这个注释,spring会尝试查找@Bean方法,但我猜您的代码中不包含这样的方法,因此spring什么也找不到,并抛出一个exception@Evgenii
@ComponentScan
有助于检测带有
@Component
@Service
@Repository
注释的组件
@Service
public interface UserService {
  String greetUser();
  void insertUsers(List<User> users);
}