When I test my Spring boot service I don't know how I can inyect 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.
Aucun commentaire:
Enregistrer un commentaire