Skip to content

Commit b4e5320

Browse files
committed
Use assertThrows in Menu test
1 parent 588e560 commit b4e5320

File tree

1 file changed

+19
-47
lines changed

1 file changed

+19
-47
lines changed

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

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2017 IBM Corporation and others.
2+
* Copyright (c) 2000, 2026 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.jupiter.api.Assertions.assertEquals;
1818
import static org.junit.jupiter.api.Assertions.assertFalse;
1919
import static org.junit.jupiter.api.Assertions.assertNull;
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
2021
import static org.junit.jupiter.api.Assertions.assertTrue;
21-
import static org.junit.jupiter.api.Assertions.fail;
2222

2323
import org.eclipse.swt.SWT;
2424
import org.eclipse.swt.events.HelpListener;
@@ -57,16 +57,11 @@ public void test_ConstructorLorg_eclipse_swt_widgets_Control() {
5757

5858
@Test
5959
public void test_ConstructorLorg_eclipse_swt_widgets_DecorationsI() {
60-
Menu newMenu;
6160
Shell nullShell = null;
62-
try {
63-
newMenu = new Menu(nullShell, SWT.NULL);
64-
newMenu.dispose();
65-
fail("No exception thrown for parent == null");
66-
} catch (IllegalArgumentException e) {
67-
}
61+
assertThrows(IllegalArgumentException.class, () -> new Menu(nullShell, SWT.NULL),
62+
"No exception thrown for parent == null");
6863

69-
newMenu = new Menu(shell, SWT.NULL);
64+
new Menu(shell, SWT.NULL);
7065
}
7166

7267
@Test
@@ -76,39 +71,27 @@ public void test_ConstructorLorg_eclipse_swt_widgets_Menu() {
7671

7772
@Test
7873
public void test_ConstructorLorg_eclipse_swt_widgets_MenuItem() {
79-
Menu newMenu;
80-
MenuItem mItem = null;
81-
try {
82-
newMenu = new Menu(mItem);
83-
newMenu.dispose();
84-
fail("No exception thrown for parent == null");
85-
} catch (IllegalArgumentException e) {
86-
}
74+
assertThrows(IllegalArgumentException.class, () ->
75+
new Menu((MenuItem)null),"No exception thrown for parent == null");
8776

88-
mItem = new MenuItem(menu, SWT.NULL);
89-
newMenu = new Menu(mItem);
77+
MenuItem mItem = new MenuItem(menu, SWT.NULL);
78+
new Menu(mItem);
9079
}
9180

9281
@Test
9382
public void test_addHelpListenerLorg_eclipse_swt_events_HelpListener() {
9483
listenerCalled = false;
9584
HelpListener listener = e -> listenerCalled = true;
9685

97-
try {
98-
menu.addHelpListener(null);
99-
fail("No exception thrown for addHelpListener with null argument");
100-
} catch (IllegalArgumentException e) {
101-
}
86+
assertThrows(IllegalArgumentException.class, () -> menu.addHelpListener(null),
87+
"No exception thrown for addHelpListener with null argument");
10288

10389
menu.addHelpListener(listener);
10490
menu.notifyListeners(SWT.Help, new Event());
10591
assertTrue(listenerCalled);
10692

107-
try {
108-
menu.removeHelpListener(null);
109-
fail("No exception thrown for removeHelpListener with null argument");
110-
} catch (IllegalArgumentException e) {
111-
}
93+
assertThrows(IllegalArgumentException.class, () -> menu.removeHelpListener(null),
94+
"No exception thrown for removeHelpListener with null argument");
11295

11396
listenerCalled = false;
11497
menu.removeHelpListener(listener);
@@ -130,11 +113,8 @@ public void menuHidden(MenuEvent e) {
130113
}
131114
};
132115

133-
try {
134-
menu.addMenuListener(null);
135-
fail("No exception thrown for addMenuListener with null argument");
136-
} catch (IllegalArgumentException e) {
137-
}
116+
assertThrows(IllegalArgumentException.class, () -> menu.addMenuListener(null),
117+
"No exception thrown for addMenuListener with null argument");
138118

139119
menu.addMenuListener(menuListener);
140120
menu.notifyListeners(SWT.Show, new Event());
@@ -144,11 +124,8 @@ public void menuHidden(MenuEvent e) {
144124
menu.notifyListeners(SWT.Hide, new Event());
145125
assertTrue(listenerCalled);
146126

147-
try {
148-
menu.removeMenuListener(null);
149-
fail("No exception thrown for removeMenuListener with null argument");
150-
} catch (IllegalArgumentException e) {
151-
}
127+
assertThrows(IllegalArgumentException.class, () -> menu.removeMenuListener(null),
128+
"No exception thrown for removeMenuListener with null argument");
152129

153130
listenerCalled = false;
154131
menu.removeMenuListener(menuListener);
@@ -317,13 +294,8 @@ public void test_setLocationII() {
317294

318295
@Test
319296
public void test_setLocationLorg_eclipse_swt_graphics_Point() {
320-
menu.setLocation(new Point(0,0));
321-
try {
322-
menu.setLocation(null);
323-
fail("No exception thrown for null argument");
324-
}
325-
catch (IllegalArgumentException e) {
326-
}
297+
menu.setLocation(new Point(0, 0));
298+
assertThrows(IllegalArgumentException.class, () -> menu.setLocation(null), "No exception thrown for null argument");
327299
}
328300

329301
/* custom */

0 commit comments

Comments
 (0)