Spring Testing不满意的依赖NoSuchBeanDefinitionException

问题描述 投票:0回答:2

当我尝试运行测试时,它们都失败了,因为它们找不到我的一个类的bean。

这里是我在上下文中使用的代码:

我得到的例外是:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testProtoAdminController' : Unsatisfied dependency expressed through constructor parameter 2; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'TestProtoCopyService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

TestProtoAdminControllerTest

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = TestProtoAdminController.class)
public class TestProtoAdminControllerTest {

//Some used services

 @Before
public void setUp() {
    authenticatedMockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}

@Test
@WithMockUser
public void testCopyProto() throws Exception {
    authenticatedMockMvc.perform(post("/api/admin/{id}/copy", 1)
            .contentType(MediaType.APPLICATION_JSON)
            .content(asJson(new TestProtoBaseVo()))).andExpect(status().isOk());
}

//Some more tests which are not important in this case

TestProtoCopyService

@Service
public class TestProtoCopyServiceImpl implements TestProtoCopyService {

    //Other services and repositories I have to use.
    //Methods

}

TestProtoCopyService

public interface TestProtoCopyService {
    @Transactional
    void copyTestProto(long testProtoId, String sourceTenant, String targetTenant);
}

TestProtoAdminController

@RestController
@RequestMapping("/*")
public class TestProtoAdminController {
private TestProtoCopyService testProtoCopyService;

public TestProtoAdminController(TestProtoCopyService testProtoCopyService {
    this.testProtoCopyService = testProtoCopyService;
}
spring spring-mvc spring-test
2个回答
0
投票

[使用@WebMvcTest时,Spring将准备一切以测试您的Web层。这并不意味着您的所有bean都已被扫描,并且属于该测试应用程序上下文的一部分并准备注入。

[通常,通常用@MockBean模拟控制器的服务类,然后使用Mockito指定其行为:

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = TestProtoAdminController.class)
public class TestProtoAdminControllerTest {

  @MockBean
  private TestProtoCopyService mockedService

  // the rest

  @Before
  public void setUp() {
    authenticatedMockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
  }

  @Test
  @WithMockUser
  public void testCopyProto() throws Exception {

    authenticatedMockMvc.perform(post("/api/admin/{id}/copy", 1)
            .contentType(MediaType.APPLICATION_JSON)
            .content(asJson(new TestProtoBaseVo()))).andExpect(status().isOk());
  }

[如果您希望Spring Boot用每个bean引导整个应用程序上下文,请考虑使用@SpringBootTest。使用此注释,您可以将任何bean注入您的应用程序。这里的缺点是您需要为测试提供整个基础结构(数据库/队列/等)。


0
投票

如果可以将代码发布到自动连接TestProtoCopyService的地方,以获取有关该问题的更多上下文。由于某种原因,它找不到合格的bean,可以尝试以下操作(如果尚未这样做):

@Autowired
@Qualifier("TestProtoCopyServiceImpl")
TestProtoCopyService testProto;
© www.soinside.com 2019 - 2024. All rights reserved.