Reputation: 3437
I have the following JUnit Test Suite, when I try to load my tests, the dependencies I have autowired in the classes I am testing do not seem to get loaded in, and I get the following error message:
package com.uk.jacob.service;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.uk.jacob.model.Ping;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = PingerService.class)
@WebAppConfiguration
public class PingerServiceTests {
@Test
public void testPingerServiceReturnsOkWhenServiceIsUp(){
PingerService pingerService = new PingerService();
Ping ping = pingerService.ping("http://devnews.today");
assertEquals(true, ping.ok);
}
@Test
public void testPingerServiceReturnsOkWhenServiceIsDown(){
PingerService pingerService = new PingerService();
Ping ping = pingerService.ping("https://jacob.uk.comz");
assertEquals(false, ping.ok);
}
}
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.uk.jacob.service.PingerService.setHttpAdapter(com.uk.jacob.adapter.HttpAdapter); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.uk.jacob.adapter.HttpAdapter] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
PingerService:
package com.uk.jacob.service;
import java.io.IOException;
import java.net.HttpURLConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.uk.jacob.adapter.HttpAdapter;
import com.uk.jacob.model.Ping;
@Component
public class PingerService {
HttpAdapter httpAdapter;
public Ping ping(String urlToPing) {
Ping ping = new Ping();
try {
HttpURLConnection connection = httpAdapter.createHttpURLConnection(urlToPing);
if(connectionIsOk(connection)){
ping.ok = true;
}
} catch (Exception e) {
ping.ok = false;
}
return ping;
}
private boolean connectionIsOk(HttpURLConnection connection) throws IOException {
return connection.getResponseCode() == 200;
}
@Autowired
public void setHttpAdapter(HttpAdapter httpAdapter){
this.httpAdapter = httpAdapter;
}
}
HttpAdapter:
package com.uk.jacob.adapter;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import org.springframework.stereotype.Component;
@Component
public class HttpAdapter {
public HttpURLConnection createHttpURLConnection(String urlToPing) throws IOException{
URL url = new URL(urlToPing);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
return connection;
}
}
Upvotes: 0
Views: 255
Reputation: 9516
You are creating your pingerService like
PingerService pingerService = new PingerService();
in the testclass, so its not a spring bean, so spring will not inject nothing there, it wont work. Instead add the PingerService to your spring configuration : Annotate it with @Component and put it somewhere where it can be found in the classpath or create it with a @Bean annotated method in a your spring configuration class.
This leads to the second problem :
@SpringApplicationConfiguration(classes = PingerService.class)
You have to provide here a configuration class, and not a single service. The configuration class must instantiate the spring beans, in your case at least PingerService and HttpAdapter.
Have a look at Spring java config (older version)
regarding your comment : For the config class, would an annotated @SpringBootApplication class be sufficient?
Yes that would be sufficient, if the PingerService and HttpAdapter are located in subpackages of that SpringBootApplication annotated class, so they can be found by the ComponentScan.
A ComponentScan is configured automatically if you use @SpringBootApplication
Upvotes: 1