In my Spring Boot application I'm trying to implement Twitter oAuth support. Following this example http://spring.io/guides/gs/accessing-twitter/ I have successfully added Twitter access to my application, so now I have:
HomeController:
@Controller
@RequestMapping("/")
public class HelloController {
private Twitter twitter;
private ConnectionRepository connectionRepository;
@Inject
public HelloController(Twitter twitter, ConnectionRepository connectionRepository) {
this.twitter = twitter;
this.connectionRepository = connectionRepository;
}
@RequestMapping(method=RequestMethod.GET)
public String helloTwitter(Model model) {
if (connectionRepository.findPrimaryConnection(Twitter.class) == null) {
return "redirect:/connect/twitter";
}
TwitterProfile userProfile = twitter.userOperations().getUserProfile();
model.addAttribute(userProfile);
CursoredList<TwitterProfile> friends = twitter.friendOperations().getFriends();
model.addAttribute("friends", friends);
return "hello";
}
}
Application class:
@PropertySources({ @PropertySource(value = "classpath:application.properties") })
@ComponentScan("com.example")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
So far everything works fine. I can start Application without any issues.
Also, I have a lot of integration tests, for example:
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringApplicationConfiguration(classes = Application.class)
@IntegrationTest("server.port: 0")
public class SearchControllerTest {
@Value("${local.server.port}")
protected int port;
@Test
public void testSearchResultsWithDefaultPageAndSize() {
// @formatter:off
given()
.when()
.get(format("http://localhost:%d/api/v1.0/search/%s", port, "RDBMa mosyl"))
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.body("totalCount", equalTo(4))
.body("entries.size", equalTo(4));
// @formatter:on
}
....
}
Now, after adding of Twitter social functionality, all my tests failed with following exception:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.social.twitter.api.Twitter] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
... 43 common frames omitted
Why does this work with Application and fails with tests ? What is wrong with my tests configuration ?
The reason is you don't have a src/main/resources/application.properties or config/application.properties, which defines spring.social.twitter.appId
and spring.social.twitter.appSecret
.
来源:https://stackoverflow.com/questions/29512365/no-qualifying-bean-of-type-org-springframework-social-twitter-api-twitter-foun