Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting test case for Junit testing
    primarykey
    data
    text
    <p>As a developer, I'm a newbie to Unit testing and have a requirement to write a test case to unit test the following code. Could somebody help me here and also give me some pointers on how to write unit tests in eclipse. </p> <pre><code>private void handle(final DocumentEvent e) { Document doc = e.getDocument(); try { String text = e.getDocument().getText(0, doc.getLength()); if (text.length() &gt;= 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()); } } </code></pre> <p>Thanks</p> <hr> <h3>Update</h3> <p>For some reason when I running the test case, I'm not getting any coverage at all. Am I doing something wrong here?? Further researching suggests that I need to use test.perform() method to call the method I want to test.. Is that correct?? Can you please suggest something?? Here is the code: </p> <pre><code>public class TestMaxLength { static final int maxMessageSize = 125; JTextPane textPane = new JTextPane(); //***EasyMock varibles**** private JTextComponent mockComponent; private MaxLength classUnderTest; private DocumentEvent mockEvent; private Document mockDocument; private Highlighter mockHighlighter; @Before public void setUp() { mockComponent = EasyMock.createMock(JTextComponent.class); mockEvent = EasyMock.createMock(DocumentEvent.class); mockDocument = EasyMock.createMock(Document.class); EasyMock.expect(mockEvent.getDocument()).andStubReturn(mockDocument); EasyMock.expect(mockDocument.getLength()).andReturn(256); mockHighlighter = EasyMock.createMock(Highlighter.class); EasyMock.expect(mockComponent.getHighlighter()).andReturn(mockHighlighter); } @Test public void testSetLength() { MaxLength maxListener = new MaxLength(125); maxListener.decorate(textPane); } @Test public void testEmptyText() { EasyMock.expect(mockDocument.getText(0, 1)).andStubReturn(""); mockHighlighter.removeAllHighlights(); EasyMock.replay(mockComponent, mockEvent, mockDocument, mockHighlighter); classUnderTest.handle(mockEvent); EasyMock.verify(mockComponent, mockEvent, mockDocument, mockHighlighter); } } </code></pre> <hr> <p>The <code>decorate(JtextComponent jComponent)</code> method is present in the class to be tested (<code>MaxLength</code>) and is defined as :</p> <pre><code>public final void decorate(final JTextComponent c) { //TODO throw exception if already decorating this.component = c; component.getDocument().addDocumentListener(this); } </code></pre> # <p>UPDATE: </p> <p>@Peter: Managed to find out that it is not the Component class that is the problem but instead I needed asm (<a href="http://forge.ow2.org/projects/asm" rel="nofollow noreferrer">http://forge.ow2.org/projects/asm</a>). I've also change the code to combine the 2 methods into 1 method: </p> <pre><code>public void testEmptyText() { maxSizeListener.decorate(mockComponent); //mockHighlighter.removeAllHighlights(); EasyMock.replay(mockComponent, mockEvent, mockDocument, mockHighlighter); maxSizeListener.handle(mockEvent); EasyMock.verify(mockComponent, mockEvent, mockDocument, mockHighlighter); } </code></pre> <p><strong><em>But now I'm having a different error on verify:</em></strong> </p> <pre><code>java.lang.AssertionError: Expectation failure on verify: getHighlighter(): expected: 1, actual: 0 at org.easymock.internal.MocksControl.verify(MocksControl.java:184) at org.easymock.EasyMock.verify(EasyMock.java:2038) at net.TestMaxLength.testEmptyText(TestMaxLength.java:98) </code></pre> <p>This is caused when executing EasyMock.verify() statement on mockComponent.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload