Skip to content

Commit 3d4ef5a

Browse files
vaadin-botArtur-
andauthored
test: Migrate flow-dnd tests to JUnit 6 (#23627) (#23797)
Co-authored-by: Artur Signell <artur@vaadin.com>
1 parent 0724af7 commit 3d4ef5a

File tree

3 files changed

+126
-119
lines changed

3 files changed

+126
-119
lines changed

flow-dnd/src/test/java/com/vaadin/flow/component/dnd/AbstractDnDUnitTest.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
import java.util.Properties;
2020
import java.util.function.Supplier;
2121

22-
import org.junit.Assert;
23-
import org.junit.Before;
24-
import org.junit.Test;
22+
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.Test;
2524
import org.mockito.ArgumentMatchers;
2625
import org.mockito.Mockito;
2726

@@ -36,13 +35,15 @@
3635
import com.vaadin.flow.server.VaadinSession;
3736
import com.vaadin.flow.server.startup.ApplicationConfiguration;
3837

39-
public abstract class AbstractDnDUnitTest {
38+
import static org.junit.jupiter.api.Assertions.assertTrue;
39+
40+
abstract class AbstractDnDUnitTest {
4041

4142
protected MockUI ui;
4243
protected boolean compatibilityMode;
4344

44-
@Before
45-
public void setup() {
45+
@BeforeEach
46+
void setup() {
4647
ApplicationConfiguration appConfig = Mockito
4748
.mock(ApplicationConfiguration.class);
4849
Mockito.when(appConfig.getPropertyNames())
@@ -72,12 +73,13 @@ public void setup() {
7273
}
7374

7475
@Test
75-
public void testExtension_activated_usageStatisticsEntryAdded() {
76+
void testExtension_activated_usageStatisticsEntryAdded() {
7677
runStaticCreateMethodForExtension(new RouterLink());
7778

78-
Assert.assertTrue("No usage statistics for generic dnd reported",
79+
assertTrue(
7980
UsageStatistics.getEntries().anyMatch(
80-
entry -> entry.getName().contains("generic-dnd")));
81+
entry -> entry.getName().contains("generic-dnd")),
82+
"No usage statistics for generic dnd reported");
8183
}
8284

8385
protected abstract void runStaticCreateMethodForExtension(

flow-dnd/src/test/java/com/vaadin/flow/component/dnd/DragSourceTest.java

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717

1818
import java.util.concurrent.atomic.AtomicReference;
1919

20-
import org.junit.Assert;
21-
import org.junit.Test;
20+
import org.junit.jupiter.api.Test;
2221

2322
import com.vaadin.flow.component.Component;
2423
import com.vaadin.flow.component.ComponentUtil;
@@ -27,7 +26,13 @@
2726
import com.vaadin.flow.component.dnd.internal.DndUtil;
2827
import com.vaadin.flow.router.RouterLink;
2928

30-
public class DragSourceTest extends AbstractDnDUnitTest {
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
import static org.junit.jupiter.api.Assertions.assertFalse;
31+
import static org.junit.jupiter.api.Assertions.assertNull;
32+
import static org.junit.jupiter.api.Assertions.assertThrows;
33+
import static org.junit.jupiter.api.Assertions.assertTrue;
34+
35+
class DragSourceTest extends AbstractDnDUnitTest {
3136

3237
@Tag("div")
3338
class TestComponent extends Component implements DragSource<TestComponent> {
@@ -40,13 +45,13 @@ protected void runStaticCreateMethodForExtension(Component component) {
4045
}
4146

4247
@Test
43-
public void testDragSource_mixinInterface() {
48+
void testDragSource_mixinInterface() {
4449
TestComponent component = new TestComponent();
4550
ui.add(component);
4651
component.setDraggable(true);
4752

48-
Assert.assertEquals("component element not set draggable", "true",
49-
component.getElement().getProperty("draggable"));
53+
assertEquals("true", component.getElement().getProperty("draggable"),
54+
"component element not set draggable");
5055

5156
AtomicReference<DragStartEvent<TestComponent>> startEventCapture = new AtomicReference<>();
5257
AtomicReference<DragEndEvent<TestComponent>> endEventCapture = new AtomicReference<>();
@@ -57,25 +62,24 @@ public void testDragSource_mixinInterface() {
5762
component, true);
5863
ComponentUtil.fireEvent(component, startEvent);
5964

60-
Assert.assertEquals(startEvent, startEventCapture.get());
61-
Assert.assertEquals(component,
62-
UI.getCurrent().getActiveDragSourceComponent());
63-
Assert.assertTrue(startEvent.isFromClient());
65+
assertEquals(startEvent, startEventCapture.get());
66+
assertEquals(component, UI.getCurrent().getActiveDragSourceComponent());
67+
assertTrue(startEvent.isFromClient());
6468

6569
DragEndEvent<TestComponent> endEvent = new DragEndEvent<TestComponent>(
6670
component, false, DropEffect.MOVE.name().toLowerCase());
6771
ComponentUtil.fireEvent(component, endEvent);
6872

6973
DragEndEvent<TestComponent> endEvent2 = endEventCapture.get();
70-
Assert.assertEquals(endEvent, endEvent2);
71-
Assert.assertNull(UI.getCurrent().getActiveDragSourceComponent());
72-
Assert.assertEquals(DropEffect.MOVE, endEvent2.getDropEffect());
73-
Assert.assertTrue(endEvent2.isSuccessful());
74-
Assert.assertFalse(endEvent2.isFromClient());
74+
assertEquals(endEvent, endEvent2);
75+
assertNull(UI.getCurrent().getActiveDragSourceComponent());
76+
assertEquals(DropEffect.MOVE, endEvent2.getDropEffect());
77+
assertTrue(endEvent2.isSuccessful());
78+
assertFalse(endEvent2.isFromClient());
7579
}
7680

7781
@Test
78-
public void testDragSource_dragStartEvent_canSetDragData() {
82+
void testDragSource_dragStartEvent_canSetDragData() {
7983
TestComponent component = new TestComponent();
8084
ui.add(component);
8185
component.setDraggable(true);
@@ -86,58 +90,58 @@ public void testDragSource_dragStartEvent_canSetDragData() {
8690
});
8791
component.addDragEndListener(DragEndEvent::clearDragData);
8892

89-
Assert.assertNull(component.getDragData());
93+
assertNull(component.getDragData());
9094

9195
DragStartEvent<TestComponent> startEvent = new DragStartEvent<TestComponent>(
9296
component, true);
9397
ComponentUtil.fireEvent(component, startEvent);
9498

95-
Assert.assertEquals("Drag data not set from event", dragData,
96-
component.getDragData());
99+
assertEquals(dragData, component.getDragData(),
100+
"Drag data not set from event");
97101

98102
ComponentUtil.fireEvent(component,
99103
new DragEndEvent<>(component, true, "none"));
100104

101-
Assert.assertNull(component.getDragData());
105+
assertNull(component.getDragData());
102106
}
103107

104108
@Test
105-
public void testDragSource_staticBuilder_wrapsComponent() {
109+
void testDragSource_staticBuilder_wrapsComponent() {
106110
RouterLink component = new RouterLink();
107111

108112
DragSource<RouterLink> dragSource = DragSource.create(component);
109113

110-
Assert.assertEquals("component element not set draggable", "true",
111-
component.getElement().getProperty("draggable"));
114+
assertEquals("true", component.getElement().getProperty("draggable"),
115+
"component element not set draggable");
112116

113-
Assert.assertEquals(component, dragSource.getDragSourceComponent());
114-
Assert.assertEquals(EffectAllowed.UNINITIALIZED,
117+
assertEquals(component, dragSource.getDragSourceComponent());
118+
assertEquals(EffectAllowed.UNINITIALIZED,
115119
dragSource.getEffectAllowed());
116120

117121
dragSource.setEffectAllowed(EffectAllowed.COPY_MOVE);
118122

119-
Assert.assertEquals(
123+
assertEquals(
120124
component.getElement()
121125
.getProperty(DndUtil.EFFECT_ALLOWED_ELEMENT_PROPERTY),
122126
EffectAllowed.COPY_MOVE.getClientPropertyValue());
123127

124128
DragSource.configure(component, false);
125-
Assert.assertNull(component.getElement().getProperty("draggable"));
129+
assertNull(component.getElement().getProperty("draggable"));
126130

127131
DropTarget.configure(component);
128-
Assert.assertNull(component.getElement().getProperty("draggable"));
132+
assertNull(component.getElement().getProperty("draggable"));
129133

130134
DragSource.configure(component, true);
131-
Assert.assertEquals("component element not set draggable", "true",
132-
component.getElement().getProperty("draggable"));
135+
assertEquals("true", component.getElement().getProperty("draggable"),
136+
"component element not set draggable");
133137

134138
DropTarget.configure(component);
135-
Assert.assertEquals("component element not set draggable", "true",
136-
component.getElement().getProperty("draggable"));
139+
assertEquals("true", component.getElement().getProperty("draggable"),
140+
"component element not set draggable");
137141
}
138142

139143
@Test
140-
public void testDragSource_serverSideEvents_correctData() {
144+
void testDragSource_serverSideEvents_correctData() {
141145
RouterLink component = new RouterLink();
142146
ui.add(component);
143147
DragSource<RouterLink> dragSource = DragSource.create(component);
@@ -151,53 +155,53 @@ public void testDragSource_serverSideEvents_correctData() {
151155
component, true);
152156
ComponentUtil.fireEvent(component, startEvent);
153157

154-
Assert.assertEquals(startEvent, startEventCapture.get());
155-
Assert.assertEquals(component,
156-
UI.getCurrent().getActiveDragSourceComponent());
157-
Assert.assertTrue(startEvent.isFromClient());
158+
assertEquals(startEvent, startEventCapture.get());
159+
assertEquals(component, UI.getCurrent().getActiveDragSourceComponent());
160+
assertTrue(startEvent.isFromClient());
158161

159162
DragEndEvent<RouterLink> endEvent = new DragEndEvent<RouterLink>(
160163
component, false, DropEffect.MOVE.name().toLowerCase());
161164
ComponentUtil.fireEvent(component, endEvent);
162165

163166
DragEndEvent<RouterLink> endEvent2 = endEventCapture.get();
164-
Assert.assertEquals(endEvent, endEvent2);
165-
Assert.assertNull(UI.getCurrent().getActiveDragSourceComponent());
166-
Assert.assertEquals(DropEffect.MOVE, endEvent2.getDropEffect());
167-
Assert.assertTrue(endEvent2.isSuccessful());
168-
Assert.assertFalse(endEvent2.isFromClient());
167+
assertEquals(endEvent, endEvent2);
168+
assertNull(UI.getCurrent().getActiveDragSourceComponent());
169+
assertEquals(DropEffect.MOVE, endEvent2.getDropEffect());
170+
assertTrue(endEvent2.isSuccessful());
171+
assertFalse(endEvent2.isFromClient());
169172

170173
endEvent = new DragEndEvent<RouterLink>(component, true, "None");
171174
ComponentUtil.fireEvent(component, endEvent);
172175

173176
endEvent2 = endEventCapture.get();
174-
Assert.assertEquals(endEvent, endEvent2);
175-
Assert.assertNull(UI.getCurrent().getActiveDragSourceComponent());
176-
Assert.assertEquals(DropEffect.NONE, endEvent2.getDropEffect());
177-
Assert.assertFalse(endEvent2.isSuccessful());
178-
Assert.assertTrue(endEvent2.isFromClient());
177+
assertEquals(endEvent, endEvent2);
178+
assertNull(UI.getCurrent().getActiveDragSourceComponent());
179+
assertEquals(DropEffect.NONE, endEvent2.getDropEffect());
180+
assertFalse(endEvent2.isSuccessful());
181+
assertTrue(endEvent2.isFromClient());
179182
}
180183

181-
@Test(expected = IllegalStateException.class)
182-
public void testDragSource_notAttachedToUIAndCatchesDragStartEvent_throws() {
184+
@Test
185+
void testDragSource_notAttachedToUIAndCatchesDragStartEvent_throws() {
183186
RouterLink component = new RouterLink();
184-
DragSource<RouterLink> dragSource = DragSource.create(component);
187+
DragSource.create(component);
185188

186189
DragStartEvent<RouterLink> startEvent = new DragStartEvent<RouterLink>(
187190
component, true);
188-
ComponentUtil.fireEvent(component, startEvent);
191+
assertThrows(IllegalStateException.class,
192+
() -> ComponentUtil.fireEvent(component, startEvent));
189193
}
190194

191195
@Test
192-
public void testDragSource_notAttachedToUIAndCatchesDragEndEvent_doesNotThrow() {
196+
void testDragSource_notAttachedToUIAndCatchesDragEndEvent_doesNotThrow() {
193197
RouterLink component = new RouterLink();
194198
ui.add(component);
195199
DragSource<RouterLink> dragSource = DragSource.create(component);
196200

197201
DragStartEvent<RouterLink> startEvent = new DragStartEvent<RouterLink>(
198202
component, true);
199203
ComponentUtil.fireEvent(component, startEvent);
200-
Assert.assertEquals(component, ui.getActiveDragSourceComponent());
204+
assertEquals(component, ui.getActiveDragSourceComponent());
201205

202206
// the drop event could remove the component if in same UI
203207
ui.remove(component);
@@ -207,7 +211,7 @@ public void testDragSource_notAttachedToUIAndCatchesDragEndEvent_doesNotThrow()
207211
// should not throw for removing the active drag source
208212
ComponentUtil.fireEvent(component, endEvent);
209213

210-
Assert.assertNull(ui.getActiveDragSourceComponent());
214+
assertNull(ui.getActiveDragSourceComponent());
211215
}
212216

213217
}

0 commit comments

Comments
 (0)