Skip to content

Commit 18eaec6

Browse files
committed
Simplify MenuItem tests assertions
Use assertThrows and assertFalse to make things more obvious.
1 parent 7add671 commit 18eaec6

File tree

2 files changed

+22
-54
lines changed

2 files changed

+22
-54
lines changed

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Item.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2016 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -15,8 +15,8 @@
1515

1616
import static org.junit.Assert.assertEquals;
1717
import static org.junit.Assert.assertNull;
18+
import static org.junit.Assert.assertThrows;
1819
import static org.junit.Assert.assertTrue;
19-
import static org.junit.Assert.fail;
2020

2121
import java.io.IOException;
2222
import java.io.InputStream;
@@ -66,12 +66,7 @@ public void test_setTextLjava_lang_String() {
6666
assertEquals("a", testStr, item.getText());
6767
item.setText("");
6868
assertTrue("b", item.getText().isEmpty());
69-
try {
70-
item.setText(null);
71-
fail("No exception thrown for string == null");
72-
}
73-
catch (IllegalArgumentException e) {
74-
}
69+
assertThrows("No exception thrown for string == null", IllegalArgumentException.class, () -> item.setText(null));
7570
}
7671

7772
/* custom */

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_MenuItem.java

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2016 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -17,8 +17,8 @@
1717
import static org.junit.Assert.assertFalse;
1818
import static org.junit.Assert.assertNotNull;
1919
import static org.junit.Assert.assertNull;
20+
import static org.junit.Assert.assertThrows;
2021
import static org.junit.Assert.assertTrue;
21-
import static org.junit.Assert.fail;
2222

2323
import org.eclipse.swt.SWT;
2424
import org.eclipse.swt.events.ArmListener;
@@ -52,12 +52,7 @@ public void test_ConstructorLorg_eclipse_swt_widgets_MenuI() {
5252
MenuItem mItem = new MenuItem(menu, SWT.NULL);
5353
assertNotNull(mItem);
5454

55-
try {
56-
new MenuItem(null, SWT.NULL);
57-
fail("No exception thrown");
58-
}
59-
catch (IllegalArgumentException e) {
60-
}
55+
assertThrows(IllegalArgumentException.class, () -> new MenuItem(null, SWT.NULL));
6156
mItem = new MenuItem(menu, SWT.CHECK);
6257
assertEquals(SWT.CHECK, mItem.getStyle());
6358
mItem.dispose();
@@ -90,21 +85,15 @@ public void test_addArmListenerLorg_eclipse_swt_events_ArmListener() {
9085
listenerCalled = false;
9186
ArmListener listener = e -> listenerCalled = true;
9287

93-
try {
94-
menuItem.addArmListener(null);
95-
fail("No exception thrown for addArmListener with null argument");
96-
} catch (IllegalArgumentException e) {
97-
}
88+
assertThrows("No exception thrown for addArmListener with null argument", IllegalArgumentException.class,
89+
() -> menuItem.addArmListener(null));
9890

9991
menuItem.addArmListener(listener);
10092
menuItem.notifyListeners(SWT.Arm, new Event());
10193
assertTrue(listenerCalled);
10294

103-
try {
104-
menuItem.removeArmListener(null);
105-
fail("No exception thrown for removeArmListener with null argument");
106-
} catch (IllegalArgumentException e) {
107-
}
95+
assertThrows("No exception thrown for removeArmListener with null argument", IllegalArgumentException.class,
96+
() -> menuItem.removeArmListener(null));
10897
listenerCalled = false;
10998
menuItem.removeArmListener(listener);
11099
menuItem.notifyListeners(SWT.Arm, new Event());
@@ -116,21 +105,15 @@ public void test_addHelpListenerLorg_eclipse_swt_events_HelpListener() {
116105
listenerCalled = false;
117106
HelpListener listener = e -> listenerCalled = true;
118107

119-
try {
120-
menuItem.addHelpListener(null);
121-
fail("No exception thrown for addHelpListener with null argument");
122-
} catch (IllegalArgumentException e) {
123-
}
108+
assertThrows("No exception thrown for addHelpListener with null argument", IllegalArgumentException.class,
109+
() -> menuItem.addHelpListener(null));
124110

125111
menuItem.addHelpListener(listener);
126112
menuItem.notifyListeners(SWT.Help, new Event());
127113
assertTrue(listenerCalled);
128114

129-
try {
130-
menuItem.removeHelpListener(null);
131-
fail("No exception thrown for removeHelpListener with null argument");
132-
} catch (IllegalArgumentException e) {
133-
}
115+
assertThrows("No exception thrown for removeHelpListener with null argument", IllegalArgumentException.class,
116+
() -> menuItem.removeHelpListener(null));
134117
listenerCalled = false;
135118
menuItem.removeHelpListener(listener);
136119
menuItem.notifyListeners(SWT.Help, new Event());
@@ -150,21 +133,15 @@ public void widgetDefaultSelected(SelectionEvent e) {
150133
}
151134
};
152135

153-
try {
154-
menuItem.addSelectionListener(null);
155-
fail("No exception thrown for addSelectionListener with null argument");
156-
} catch (IllegalArgumentException e) {
157-
}
136+
assertThrows("No exception thrown for addSelectionListener with null argument", IllegalArgumentException.class,
137+
() -> menuItem.addSelectionListener(null));
158138

159139
menuItem.addSelectionListener(listener);
160140
menuItem.notifyListeners(SWT.Selection, new Event());
161141
assertTrue(listenerCalled);
162142

163-
try {
164-
menuItem.removeSelectionListener(null);
165-
fail("No exception thrown for removeSelectionListener with null argument");
166-
} catch (IllegalArgumentException e) {
167-
}
143+
assertThrows("No exception thrown for removeSelectionListener with null argument", IllegalArgumentException.class,
144+
() -> menuItem.removeSelectionListener(null));
168145
listenerCalled = false;
169146
menuItem.removeSelectionListener(listener);
170147
menuItem.notifyListeners(SWT.Selection, new Event());
@@ -206,7 +183,7 @@ public void test_isEnabled() {
206183
menuItem.setEnabled(true);
207184
assertTrue(menuItem.isEnabled());
208185
menuItem.setEnabled(false);
209-
assertEquals(menuItem.isEnabled(), false);
186+
assertFalse(menuItem.isEnabled());
210187
}
211188

212189
@Test
@@ -220,7 +197,7 @@ public void test_setEnabledZ() {
220197
menuItem.setEnabled(true);
221198
assertTrue(menuItem.getEnabled());
222199
menuItem.setEnabled(false);
223-
assertEquals(menuItem.getEnabled(), false);
200+
assertFalse(menuItem.getEnabled());
224201
}
225202

226203
@Override
@@ -250,7 +227,7 @@ public void test_setSelectionZ() {
250227
for (int itemStyle : itemStyles) {
251228
MenuItem mItem = new MenuItem(menu, itemStyle);
252229
mItem.setSelection(false);
253-
assertEquals(mItem.getSelection(), false);
230+
assertFalse(mItem.getSelection());
254231
mItem.setSelection(true);
255232
assertTrue(mItem.getSelection());
256233
mItem.dispose();
@@ -262,11 +239,7 @@ public void test_setSelectionZ() {
262239
public void test_setTextLjava_lang_String() {
263240
menuItem.setText("ABCDEFG");
264241
assertEquals("ABCDEFG", menuItem.getText());
265-
try {
266-
menuItem.setText(null);
267-
fail("No exception thrown for addArmListener with null argument");
268-
} catch (IllegalArgumentException e) {
269-
}
242+
assertThrows(IllegalArgumentException.class, () -> menuItem.setText(null));
270243
menuItem.setText("ABCDEFG");
271244
menuItem.setAccelerator(SWT.MOD1 + 'A');
272245
assertTrue(menuItem.getText().startsWith("ABCDEFG"));

0 commit comments

Comments
 (0)