Developer Developer - 2 months ago 17
Java Question

Reason for different behavior of the similar junit tests

I am using the following two tests for the common pangram program. But test2 passes while test3 fails.

@Test
public void test2(){
Pangram4 pangram4 = new Pangram4(" b cd x rs ijk pno f vu");
Set<Character> actual = pangram4.getMissingAlphabets();
Set <Character>expected = new HashSet<Character>();
expected.add('a');
expected.add('e');
expected.add('g');
expected.add('h');
expected.add('l');
expected.add('m');
expected.add('q');
expected.add('t');
expected.add('w');
expected.add('y');
expected.add('z');

assertEquals(expected,actual);
}


@Test
public void test3(){
Pangram4 pangram4 = new Pangram4("The quick browndoga lazy.");
Set<Character> actual = pangram4.getMissingAlphabets();
Set<Character> expected = new HashSet<Character>();
expected.add('f');
expected.add('o');
expected.add('x');
expected.add('j');
expected.add('u');
expected.add('m');
expected.add('p');
expected.add('s');
expected.add('o');
expected.add('v');
expected.add('e');
expected.add('r');
assertEquals(expected, actual);
}


What can be the reason? I have only given the test methods here, not the entire junit class. Please use any pangram program with getMissingLetters() method that returns the Set and change test method accordingly.

Answer

In your test case test3(), you are "expecting" an o:

expected.add('o');

The letter o is in the test case string, "The quick browndoga lazy.". The result is that the expected set contains an o while the actual set does not. This results in a false result.

As @JasonC mentioned, there is also the same problem with the r.

Comments