English 中文(简体)
单位测试 简便模拟框架的 Java类
原标题:Unit testing Java Classes with EasyMock Framework

HI, I have to unit test a Java class which implements DocumentListener interface. 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.

j类:

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());
        }
    }
} 
问题回答

简便的磁强文件涵盖面很强:

这里的任何进展都是基本考验:

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




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签