In a Spring boot test, how can I create a bean and injects by @Autowired?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



In a Spring boot test, how can I create a bean and injects by @Autowired?



When I test my Spring boot service I don't know how I can inject an @Autowired bean.



My bean (Spring fills @Value from application.yml):


@Component
public class NavigatorProperties
@Value("$timerDelay")
private String timerDelay;

public String getTimerDelay()
return timerDelay;

public void setTimerDelay(String timerDelay)
this.timerDelay = timerDelay;




My api:


public class ListenerApi implements IRestListenerApi
@Autowired
private NavigatorProperties np;

public String doSomething (...) // This is my service method.
// Here np.getTimerDelay() will return application.yml value.
int timerDelay = Integer.decode(np.getTimerDelay());
...




This works fine and int value is correct. Here is my test:


@RunWith(SpringRunner.class)
@SpringBootTest(classes = ListenerApiTest.class, NavigatorProperties.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ListenerApiTest {
@Autowired
private NavigatorProperties np; // Can be autowired or a new Object.

// Object to test.
private ListenerApi listenerApi;

@Test
public void test01ForceNumberFormatException()
np.setTimerDelay("NumberFormatException");
// Inyect into ListenerApi


@Test
public void test02ForceNullPointerException()
np.setTimerDelay(null);
// Inyect into ListenerApi



In this test comments, how I can inyect into ListenerApi by @Autowired?



Thanks.





Can you what ListenerApiTest class file, I think bean is initialized there itself and you possibly are mixing config bean definition and annotations.
– Himanshu Bhardwaj
Aug 6 at 10:05





Not sure what you are asking. Can;t you just put @Autowired on ListenerApi
– pvpkiran
Aug 6 at 10:07


@Autowired





You can use @ContextConfiguration and point to the right @Configuration class for your stuff, then use @Autowired on the ListenerApi instance. But if this is a unit test rather than integration test, it probably makes as much sense to use constructor injection and just construct the needed items in your test...
– user2478398
Aug 6 at 10:30



@ContextConfiguration


@Configuration


@Autowired


ListenerApi




3 Answers
3



First of all, you need to annotate your dependency with org.springframework.stereotype.Component.


org.springframework.stereotype.Component


@Component
public class ListenerApi implements IRestListenerApi {



Then Inject it in wherever required :


@Autowired
private ListenerApi listenerApi;





Oh yes! This solution works with @Autowired ListenerApi... Thanks.
– Javi L
Aug 6 at 11:20





If this helps, please mark it as Accepted answer.
– Mehraj Malik
Aug 6 at 11:29



Add component scan annotation with the package to scan


@ComponentScan(basePackages = "my.package.to.scan")



your @SpringBootTest should be like:


@SpringBootTest(classes = ListenerApi.class, NavigatorProperties.class)



include every bean class that you want to inject into ListenerApiTest





Could you please add some informations, why this solves the questsion?
– TheTanic
Aug 6 at 10:23






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard