Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am implementing a gap buffer and am trying to write a test for the insert method, the test currently looks something like this:

gapBuffer.insert('a')
assertEquals(gapBuffer.getText(), "a")
gapBuffer.insert('c')
assertEquals(gapBuffer.getText(), "ac")
gapBuffer.insert('d')
assertEquals(gapBuffer.getText(), "acd")
gapBuffer.moveCursorTo(1)
gapBuffer.insert('d')
assertEquals(gapBuffer.getText(), "abcd")

Now the problem is that this method is not just testing the insert method because it depends on the correctness of the getText() and moveCursorTo() methods

Note: the getText() and moveCursorTo() methods are not trivial but do have tests of their own

share|improve this question

closed as unclear what you're asking by Jerry Coffin, Mat's Mug, Malachi, Simon Forsberg, Edward Jul 2 '14 at 14:12

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

2  
what makes you think that's a problem? –  Winston Ewert Dec 7 '11 at 4:48

1 Answer 1

up vote 3 down vote accepted

It's usually hard to test a setter without a getter and vice versa. Back Door Manipulation in xUnit Test Patterns discuss this in detail.

Some smells from the same book/site:

  • Eager Test: A single test verifies too much functionality.
  • Missing Assertion Message: A test fails. Upon examining the output of the Test Runner, we cannot determine exactly which assertion had failed.

I'd create at least three tests from the one above:

@Test
public void testInsert() {
    gapBuffer.insert('a')
    assertEquals(gapBuffer.getText(), "a")
}

@Test
public void testInsertTwice() {
    gapBuffer.insert('a')
    gapBuffer.insert('c')
    assertEquals(gapBuffer.getText(), "ac")
}

@Test
public void testMoveCursorToAndInsert() {
    gapBuffer.insert("acd")
    gapBuffer.moveCursorTo(1)
    gapBuffer.insert('d')
    assertEquals(gapBuffer.getText(), "abcd")
}

It helps Defect Localization.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.