Research Log of Web Science Students

Computer Science is not simply programming

The Perfect Unit Test (for this class at least)

leave a comment »

I enjoyed writing this test. It tells a story.

public class WordPressPreviewTestCase extends TestCase {
	Preview learningObject;
	@Override
	protected void setUp() throws Exception {
		WordpressPreview previewer = new WordPressPreview();
		previewer.setWriter(new StringWriter());
		learningObject= previewer;
		super.setUp();
	}
	public void testPreview(){
		String link = "some link";
		String expectedPreview = "some expected preview";
		try {
			String preview = learningObject.getPreview(link);
			assertNotNull(preview);
			assertFalse("".equals(preview));
			assertEquals(expectedPreview.getClass(),preview.getClass());
		} catch (IOException e) {
			e.printStackTrace();
		}
	
	}

What was I testing? a class that gets a preview of a WordPress blog given its url.

I enjoyed making an interface for media that is being previewed in our PLE:

public interface Preview {
	public String getPreview(String link) throws IOException;
}

Now it doesn’t matter what type of medium the learning object. You can give the system a link to video a wordpress blog or maybe (hopefully if this can be done) a pdf in google docs and as long as there is an implementation for getting it and the system has the right implementation at that moment, all it has to do is invoke the getPreview method.

In the future I’ll probably implement a factory that takes in a link and gives the system the right implementation but for now I just had to enumerate all the possible cases. But that’s okay since I have a blog post bookmarked that tells me how to refactor an if-else tree. I am guessing the right refactoring is a factory.

Advertisements

Written by Jose Asuncion

September 21, 2009 at 5:46 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: