Unit testing Java Classes with EasyMock Framework
HI, I have to unit test a Java class which implements DocumentListener int开发者_StackOverflowerface. We are using Eclipse and Junit with EasyMock Framework. I'm a newbie to Unit testing and hence would appreciate a sample code using EasyMock.
The java class is:
public class ClassToBeTested implements DocumentListener
{
private static final Color COLOR = Color.BLUE;
/** Painter. */
private Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter (COLOR);
private int maxMessageSize;
private JTextComponent component;
/*** The Constructor.
* @param maxSize - The Maximum message size
*/
public ClassToBeTested(final int maxSize)
{
super();
this.maxMessageSize = maxSize;
}
/**
* Decorate the component.
* @param c - The component to decorate
*/
public final void decorate(final JTextComponent c)
{
//TODO throw exception if already decorating
this.component = c;
component.getDocument().addDocumentListener(this);
}
/**
* Remove Update.
* @param e - The event
*/
@Override
public final void removeUpdate(final DocumentEvent e)
{
handle(e);
}
/**
* Insert Update.
* @param e - The event
*/
@Override
public final void insertUpdate(final DocumentEvent e)
{
handle(e);
}
/**
* Changed Update.* @param e - The event
*/
@Override
public final void changedUpdate(final DocumentEvent e)
{
handle(e);
}
/**
* Handle the event.
* @param e - The event
*/
public void handle(final DocumentEvent e)
{
Document doc = e.getDocument();
try {
String text = e.getDocument().getText(0, doc.getLength());
if (text.length() >= maxMessageSize)
{
try
{
component.getHighlighter().addHighlight( maxMessageSize, text.length() + 1, painter);
} catch (BadLocationException ex)
{
System.out.println(ex.getMessage());
}
} else
{
component.getHighlighter().removeAllHighlights();
}
} catch (BadLocationException e1)
{
System.out.println(e1.getMessage());
}
}
}
EasyMock documentation covers it pretty well: http://easymock.org/EasyMock3_0_Documentation.html
Anyway here is a basic test:
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.reset;
import javax.swing.event.DocumentEvent;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Highlighter;
import javax.swing.text.JTextComponent;
import org.junit.Before;
import org.junit.Test;
public class ClassToBeTestedTest {
private ClassToBeTested classToBeTested;
private JTextComponent component = createMock(JTextComponent.class);
private int maxSize = 20;
private Document document = createMock(Document.class);
@Before
public void setUp() throws Exception {
classToBeTested = new ClassToBeTested(maxSize);
setMocksForSetUp();
classToBeTested.decorate(component);
}
private void setMocksForSetUp() {
expect(component.getDocument()).andReturn(document);
document.addDocumentListener(classToBeTested);
replay(component, document);
}
@Test
public void testHandle() throws BadLocationException {
DocumentEvent event = createMock(DocumentEvent.class);
setMockForHandle(event);
classToBeTested.handle(event);
}
private void setMockForHandle(DocumentEvent event) throws BadLocationException {
reset(document, component);
expect(event.getDocument()).andReturn(document).times(2);
expect(document.getLength()).andReturn(16);
expect(document.getText(0, 16)).andReturn("Junit text");
Highlighter highlighter = createMock(Highlighter.class);
expect(component.getHighlighter()).andReturn(highlighter);
highlighter.removeAllHighlights();
replay(event, document, component, highlighter);
}
}
精彩评论