Jonathan Hagen
Jonathan Hagen

Reputation: 732

Why is my configuration test class not working?

I am trying to write unit test using junit for my service configuration class. I have existing code that works in other module, but it doesn't work on this module for some reason and I cannot figure this out. Here is my code:

ServiceConfig class:

package config.service;

import service.Service;
import service.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ServiceConfig {

    @Autowired
    private Service service;

    @Bean
    public Service service() {
        return new serviceImpl();
    }
}

Service interface:

package service;

public interface Service {

    void search() throws Exception;
}

ServiceImpl class:

package service;

public class ServiceImpl implements Service {

    @Override
    public void search() throws Exception {
        return null;
    }
}

ServiceConfigTest class:

package config.service;

import service.Service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertNotNull;

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { ServiceConfig.class })
public class ServiceConfigTest {

    @Autowired
    private Service service;

    @Test
    public void service() {
        assertNotNull(service);
    }
}

and here is the Exception:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ServiceConfig': Unsatisfied dependency expressed through field 'Service'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.nuance.powershare.dispatchreporter.service.Service' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

I don't have too much experience with spring and configuration classes. However, this seems legit to me, I basically followed the code that was already working in other module. My manager also cannot find what is wrong.

Upvotes: 0

Views: 715

Answers (1)

Nayan
Nayan

Reputation: 311

The above exception is caused, when we did not create a bean of the type it will raise an exception "Error creating bean with name 'className'.

I tried the same code it's working for me. However, you don't need to create Service Config to create a bean of ServiceImpl just annotate ServiceImpl with @Service and you can test it subsequently.

 @Service
 public class ServiceImpl implements Service {

 @Override
 public void search() throws Exception {

   }
}

and avoid using the predefined names(ex: Service) for the class name.

Upvotes: 1

Related Questions