Skip to content

Commit 4d72b6e

Browse files
test: improve coverage for TypeComparator and MethodComparator
Added tests to cover the sorting logic inside pure utility classes `TypeComparator` and `MethodComparator`, bringing their coverage up to 100%. Uses Mockito to mock Eclipse JDT `IType` and `IMethod` interfaces to ensure isolation and speed. Follows the project's requirement of using JUnit 4 exclusively. Co-authored-by: RoiSoleil <3462260+RoiSoleil@users.noreply.github.com>
1 parent 6231dac commit 4d72b6e

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.moreunit.util;
2+
3+
import static org.mockito.Mockito.mock;
4+
import static org.mockito.Mockito.when;
5+
import static org.junit.Assert.assertTrue;
6+
import static org.junit.Assert.assertEquals;
7+
8+
import org.eclipse.jdt.core.IMethod;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
public class MethodComparatorTest
13+
{
14+
private MethodComparator methodComparator;
15+
16+
@Before
17+
public void setUp()
18+
{
19+
methodComparator = new MethodComparator();
20+
}
21+
22+
@Test
23+
public void should_order_methods_by_element_name()
24+
{
25+
IMethod methodA = mock(IMethod.class);
26+
when(methodA.getElementName()).thenReturn("methodA");
27+
28+
IMethod methodB = mock(IMethod.class);
29+
when(methodB.getElementName()).thenReturn("methodB");
30+
31+
assertTrue(methodComparator.compare(methodA, methodB) < 0);
32+
assertTrue(methodComparator.compare(methodB, methodA) > 0);
33+
}
34+
35+
@Test
36+
public void should_return_zero_for_equal_methods()
37+
{
38+
IMethod methodA = mock(IMethod.class);
39+
when(methodA.getElementName()).thenReturn("methodA");
40+
41+
IMethod methodB = mock(IMethod.class);
42+
when(methodB.getElementName()).thenReturn("methodA");
43+
44+
assertEquals(0, methodComparator.compare(methodA, methodB));
45+
}
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.moreunit.util;
2+
3+
import static org.mockito.Mockito.mock;
4+
import static org.mockito.Mockito.when;
5+
import static org.junit.Assert.assertTrue;
6+
import static org.junit.Assert.assertEquals;
7+
8+
import org.eclipse.jdt.core.IType;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
public class TypeComparatorTest
13+
{
14+
private TypeComparator typeComparator;
15+
16+
@Before
17+
public void setUp()
18+
{
19+
typeComparator = new TypeComparator();
20+
}
21+
22+
@Test
23+
public void should_order_types_by_fully_qualified_name()
24+
{
25+
IType typeA = mock(IType.class);
26+
when(typeA.getFullyQualifiedName()).thenReturn("org.example.AClass");
27+
28+
IType typeB = mock(IType.class);
29+
when(typeB.getFullyQualifiedName()).thenReturn("org.example.BClass");
30+
31+
assertTrue(typeComparator.compare(typeA, typeB) < 0);
32+
assertTrue(typeComparator.compare(typeB, typeA) > 0);
33+
}
34+
35+
@Test
36+
public void should_return_zero_for_equal_types()
37+
{
38+
IType typeA = mock(IType.class);
39+
when(typeA.getFullyQualifiedName()).thenReturn("org.example.AClass");
40+
41+
IType typeB = mock(IType.class);
42+
when(typeB.getFullyQualifiedName()).thenReturn("org.example.AClass");
43+
44+
assertEquals(0, typeComparator.compare(typeA, typeB));
45+
}
46+
}

0 commit comments

Comments
 (0)