25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 17 ■ Be Social and Go Mobile<br />

Listing 17-3. DocumentTweet.java<br />

package com.apress.isf.spring.social;<br />

import org.springframework.beans.factory.annotation.Autowired;<br />

import org.springframework.social.twitter.api.Twitter;<br />

import org.springframework.stereotype.Component;<br />

@Component("documentTweet")<br />

public class DocumentTweet {<br />

@Autowired<br />

Twitter tweet;<br />

}<br />

public void tweet(String text){<br />

tweet.timelineOperations().updateStatus(text);<br />

}<br />

Listing 17-3 shows your DocumentTweet class that is using your “tweet” bean. You are creating a method tweet<br />

(String text) that will accept a String that will be the message posted to your Twitter account. To post the text,<br />

you need to call the timelineOperations that will give you the updateStatus.<br />

Next, you need to create a unit test. See Listing 17-4.<br />

Listing 17-4. MyDocumentsTest.java<br />

package com.apress.isf.spring.test;<br />

import org.junit.Test;<br />

import org.junit.runner.RunWith;<br />

import static org.junit.Assert.assertNotNull;<br />

import org.slf4j.Logger;<br />

import org.slf4j.LoggerFactory;<br />

import org.springframework.beans.factory.annotation.Autowired;<br />

import org.springframework.test.context.ContextConfiguration;<br />

import org.springframework.test.context.junit4.<strong>Spring</strong>JUnit4ClassRunner;<br />

import com.apress.isf.spring.social.DocumentTweet;<br />

@RunWith(<strong>Spring</strong>JUnit4ClassRunner.class)<br />

@ContextConfiguration({"classpath:META-INF/spring/mydocuments-context.xml","classpath:META-INF/<br />

spring/mydocuments-mongo-context.xml"})<br />

public class MyDocumentsTest {<br />

private static final Logger log = LoggerFactory.getLogger(MyDocumentsTest.class);<br />

@Autowired<br />

DocumentTweet documentTweet;<br />

@Test<br />

246

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!