找不到类型为[org.springframework.social.twitter.api.Twitter]的限定bean

时间:2015-04-08 10:38:33

标签: spring spring-mvc spring-boot spring-social

在我的Spring Boot应用程序中,我试图实现Twitter oAuth支持。 按照这个例子http://spring.io/guides/gs/accessing-twitter/我已成功添加了对我的应用程序的Twitter访问权限,所以现在我有:

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";
    }

}

申请类:

@PropertySources({ @PropertySource(value = "classpath:application.properties") })
@ComponentScan("com.example")
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

到目前为止一切正常。我可以毫无问题地启动应用程序。

另外,我有很多集成测试,例如:

@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
    }
....
}

现在,在添加了Twitter社交功能之后,我的所有测试都失败了以下异常:

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

为什么这适用于Application并且在测试时失败?我的测试配置有什么问题?

1 个答案:

答案 0 :(得分:4)

原因是你没有src / main / resources / application.properties或config / application.properties,它们定义了spring.social.twitter.appIdspring.social.twitter.appSecret