Spring Download: "No qualifying bean type ... found" when autowiring class class

I am writing a component using Spring Boot and Spring Boot JPA. I have a setting like this:

Interface:

public interface Something {
    // method definitions
}

Implementation:

@Component
public class SomethingImpl implements Something {
    // implementation
}

Now I have a JUnit test that works with SpringJUnit4ClassRunner, and I want to test mine SomethingImplwith this.

When i do

@Autowired
private Something _something;

it works but

@Autowired
private SomethingImpl _something;

makes the test discard NoSuchBeanDefinitionExceptionwith a messageNo qualifying bean of type [com.example.SomethingImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

But in the test case, I want to explicitly enter mine SomethingImpl, because this is the class I want to test. How to achieve this?

+4
source share
3 answers

bean, @Qualifier:

@Autowired
@Qualifier("SomethingImpl")
private Something _something;
+4

, javax.inject DI:

@Named("myConcreteThing")
public class SomethingImpl implements Something { ... }

:

@Inject
@Named("myConcreteThing")
private Something _something;

@EnableAutoConfiguration @ComponentScan.

+4

I think you need to add @Service to the class implementation .. sort of

@Service public class SomethingImpl implements Something { // implementation }

+2
source

All Articles