1

I have this error when I run a test:

nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.bonanza.api.IWorkflowService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

and this is the servlet-xml I load when running the classes:

  <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:context="http://www.springframework.org/schema/context"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:jpa="http://www.springframework.org/schema/data/jpa"
            xmlns:util="http://www.springframework.org/schema/util"
            xmlns:tx="http://www.springframework.org/schema/tx"
            xmlns:mvc="http://www.springframework.org/schema/mvc"
            xsi:schemaLocation="
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/data/jpa
            http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:annotation-config />
        <context:component-scan base-package="com.bonanza.*" />
        <jpa:repositories base-package="com.bonanza.*" />
        <!-- transaction management -->
        <mvc:annotation-driven />
    
        <bean id="workflowService"
                class="com.bonanza.api.IWorkflowService" abstract="true"/>
..
</beans>
Nuñito Calzada
  • 4,394
  • 47
  • 174
  • 301
  • Is this a legacy project you're updating or a new one you're creating? – chrylis -cautiouslyoptimistic- Oct 04 '20 at 23:53
  • a legacy project – Nuñito Calzada Oct 05 '20 at 08:16
  • @NunyetdeCanCalçada Can you provide your Java class and if you are using an @Autowired? Your XML file does look good as currently stated. – rslj Oct 06 '20 at 22:37
  • 1
    As far as I know, beans marked as "abstract" are not instantiated by Spring. Usually abstract beans are used to group common properties for child beans and reduce XML code. First step would be removing `abstract="true"` in XML config and check which error will be shown next. Also it seems that `com.bonanza.api.IWorkflowService` is an interface (according to its name), actually bean definition in XML should point to an interface implementation. I agree, it would be useful to see IWorkflowService declaration. – Alexandra Dudkina Oct 07 '20 at 04:47

1 Answers1

0

I think your problem is that you are trying to use an interface instead of a concrete class to inject the dependency of type com.bonanza.api.IWorkflowService required other beans in your application.

In order to solve the problem, you have several options.

On one hand, you can of course provide an actual implementation for that interface.

Or, on the other hand, you can provide a mock object for that interface. You can use Mockito for that, and modify your Spring XML configuration with something similar to the following code:

<beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:context="http://www.springframework.org/schema/context"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:jpa="http://www.springframework.org/schema/data/jpa"
            xmlns:util="http://www.springframework.org/schema/util"
            xmlns:tx="http://www.springframework.org/schema/tx"
            xmlns:mvc="http://www.springframework.org/schema/mvc"
            xsi:schemaLocation="
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/data/jpa
            http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:annotation-config />
        <context:component-scan base-package="com.bonanza.*" />
        <jpa:repositories base-package="com.bonanza.*" />
        <!-- transaction management -->
        <mvc:annotation-driven />
    
        <bean id="workflowService" class="org.mockito.Mockito" factory-method="mock">
            <constructor-arg value="com.bonanza.api.IWorkflowService" />
        </bean>
..
</beans>

If required, you can customize the mock behavior in your configuration (from your other question):

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("classpath:backoffice-servlet.xml")
public class TimeControllerTests {

    @Autowired
    private WebApplicationContext wac;

    @Autowired
    private IWorkflowService workflowService;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
        // given(this.workflowService...).willReturn(...);
    }
    
    @Test
    public void should_OK() throws Exception {

        mockMvc.perform(get("/time/2")
                .contentType(APPLICATION_JSON))
                .andExpect(status().isOk());
    }
}
jccampanero
  • 50,989
  • 3
  • 20
  • 49