0

嗨,我必须对实现 DocumentListener 接口的 Java 类进行单元测试。我们将 Eclipse 和 Junit 与 EasyMock 框架一起使用。我是单元测试的新手,因此很欣赏使用 EasyMock 的示例代码。

java类是:

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());
        }
    }
} 
4

1 回答 1

0

EasyMock 文档很好地涵盖了它:http: //easymock.org/EasyMock3_0_Documentation.html

无论如何,这是一个基本测试:

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);
   }
}
于 2011-08-17T09:56:57.787 回答