1

I am new to spring boot and I am writing Junit for my REST Api.so many annotation are required by my class to run Junit. So I planned to put all annotation in one custom annotation and use that custom annotation in my Junit class. But now my Autowire is stopped working.

I tried to create custom annotation and used that in my Junit class but now @Autowire annotation has stopped to work.

Below is code of custom annotation:

@Inherited

@Target(ElementType.TYPE)

@Retention(RetentionPolicy.CLASS)

@Documented

@Category(UnitTest.class)

@RunWith(SpringRunner.class)

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {AptDataApiApplicationRepo.class })

@TestPropertySource("classpath:application-test.properties")

public @interface RepoTestConfig {

}

// My JUnit class code

@RepoTestConfig

public class HolidayRepoTest {

    @Autowired
    private IHolidayRepo iHolidayRepo;
}

// here spring throwing an exception when spring context is trying to get up

Exception coming is :

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.demo.repository.IHolidayRepo' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

@Note: When I am putting all annotation in HolidayRepoTest class then every thing is fine.

5
  • Read docs.oracle.com/javase/8/docs/api/index.html?java/lang/…. You need to use docs.oracle.com/javase/8/docs/api/index.html?java/lang/…. Commented Jun 1, 2019 at 7:44
  • @JBNizet, I Have tried both RETENTION policies CLASS and RUNTIME, But no success. Commented Jun 1, 2019 at 7:48
  • 1
    I don't think @RunWith(SpringRunner.class) (and Category) can be part of a meta annotation. It's not a Spring annotation. It's a JUnit one. Commented Jun 1, 2019 at 7:54
  • @JBNizet,thanks It working now.after moving both to my Junit class Commented Jun 1, 2019 at 7:57
  • JUnit 5 supports meta-annotations so your approach will work in the future should you upgrade from JUnit 4. Commented Jun 1, 2019 at 8:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.