From 6fc8878599167d12ea5429ad2d7e7f6b1d41a783 Mon Sep 17 00:00:00 2001 From: Mirco Date: Thu, 13 Jun 2024 12:57:52 +0200 Subject: [PATCH] Test added --- .gitignore | 3 ++- .../java/com/ModeratorElection/MainView.java | 9 +++++---- .../com/ModeratorElection/MainViewTest.java | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 src/test/java/com/ModeratorElection/MainViewTest.java diff --git a/.gitignore b/.gitignore index 65ca50d..c32047f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ build/ .gradle/ -.idea/ \ No newline at end of file +.idea/ +frontend/generated/ \ No newline at end of file diff --git a/src/main/java/com/ModeratorElection/MainView.java b/src/main/java/com/ModeratorElection/MainView.java index 7e86209..c290e46 100644 --- a/src/main/java/com/ModeratorElection/MainView.java +++ b/src/main/java/com/ModeratorElection/MainView.java @@ -38,11 +38,8 @@ public class MainView extends VerticalLayout { Button electNow = new Button("Elect!", buttonClickEvent -> { try { - ArrayList checkboxSelectedMembers = new ArrayList(checkboxGroup.getSelectedItems()); - int sizeArrayListSelectedMembers = checkboxSelectedMembers.size(); - int random = (int) (Math.random() * sizeArrayListSelectedMembers); Notification moderatorNotification = new Notification() - .show("Nächster Moderator: " + checkboxSelectedMembers.get(random)); + .show("Nächster Moderator: " + electMember(selectMemberItemsNameslist)); moderatorNotification.setPosition(Notification.Position.BOTTOM_CENTER); } catch(Exception exceptionEvent) { Notification errorNotification = new Notification(); @@ -73,4 +70,8 @@ public class MainView extends VerticalLayout { add(electNow); } + + private String electMember(List members){ + return members.get((int)(Math.random() * members.size())); + } } diff --git a/src/test/java/com/ModeratorElection/MainViewTest.java b/src/test/java/com/ModeratorElection/MainViewTest.java new file mode 100644 index 0000000..9e5e214 --- /dev/null +++ b/src/test/java/com/ModeratorElection/MainViewTest.java @@ -0,0 +1,18 @@ +package com.ModeratorElection; + +import com.vaadin.flow.component.checkbox.CheckboxGroup; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +//@SpringBootTest +public class MainViewTest { + @Test + void mainViewConstructor() { + MainView sut = new MainView("Harry,Sven,Ida"); + CheckboxGroup cgroupTest = (CheckboxGroup) sut.getChildren().filter(child -> child instanceof CheckboxGroup).findAny().orElse(null); + int cgroupTestSelectedItemsSize = cgroupTest.getSelectedItems().size(); + Assertions.assertEquals(3,cgroupTestSelectedItemsSize); + } + +}