What do we need to start a Test driven Eclipse Project.

Well, first of all we need Eclipse RCP!
Than we create two PlugIn project MockitoViz and MockitoVizTest. (It is better to create create two projects so you don’t mix productive code with test code)

I saw an opening sequence of introduction to TDD from Kent Beck on vimeo. (by the way Kent Beck is the Father of XP, TDD and JUnit) And he introduced some interesting idea to me.

Mostly if I would start with Test first approach, I would start with theoretical classes that I would create and instantiate in my test (Programming by intention style), the test will not be compilable so I will have to create this classes. So in other word you create a consumer for something that does not yet exist and it’s kind of picky because you directly making assumptions about something.

Kent Beck started to implement the logic directly in the test and then extracted it to a Class under test. So if I would transfer it to my Consumer metaphor:

Do something your self, than delegate it to somebody else and check if he doing it write.

Now back to the real implementation and why I mentioned this stuff.
I created two projects and I know that I would need “org.eclipse.jdt.core” plugin for code evaluation. So I start with a test for parsing Java Code. I add two dependencies to my test Plugin:
- already mentioned “org.eclipse.jdt.core”
- and JUnit 4.

Now I implement something like this:

package de.bomzhi.jdt.exploration;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.junit.Test;
public class ExploreParser {
	public void parseSimpleClassDeclaration() throws Exception {
		String javaCode = "class A {}";
		ASTParser newParser = ASTParser.newParser(AST.JLS3);
		ASTNode ast = newParser.createAST(new NullProgressMonitor());
		assertThat(ast, is(notNullValue()));

By implementing this code I see that I need another two plugins:
- “org.eclipse.core.resources” because of IProgressMonitor that is used in “createAST” method
- and “org.eclipse.core.runtime” that is used internally by ASTParser

So here you see a client that converts a string to AST (Abstract Syntax Tree) and checks if he was successful. It has no expectations about the AST it becomes. The client is just happy that AST is not null.

By the way I am using the assertThat function for checking the assumption. assertThat was introduced in JUnit 4 and is based on matchers from Hamcrest framework.
Matchers provides pretty cool DSL for assertions. The error messages are also very readable.

Never the less, in this first short cast we created a first test which learns us how to use Eclipse ASTParser.

Leave a Reply



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Before you submit form:
Human test by Not Captcha

© 2011 Max Blog Suffusion theme by Sayontan Sinha