Skip to content

Commit

Permalink
mockito.homework package with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kacper-cholewinski committed Dec 23, 2024
1 parent ba178ef commit a50618b
Show file tree
Hide file tree
Showing 11 changed files with 267 additions and 0 deletions.
1 change: 1 addition & 0 deletions kodilla-advanced-tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ repositories {
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.3'
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.9.3'
testImplementation 'org.mockito:mockito-core:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.3'
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.kodilla.mockito;

public interface MobilePhone {

boolean needsCharging();
double getFreeStorage();
void launchApplication(String applicationName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.kodilla.mockito;

import com.kodilla.notification.Client;
import com.kodilla.notification.Notification;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class NotificationService {

private Set<Client> clients = new HashSet<>();

public void addSubscriber(Client client) {
clients.add(client);
}

public void sendNotification(Notification notification) {
clients.forEach(client -> client.receive(notification));
}

public void removeSubscriber(Client client) {
clients.remove(client);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.kodilla.mockito.homework;

public interface Alert {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.kodilla.mockito.homework;

public interface Subscriber {
void receive(Alert alert);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.kodilla.mockito.homework;

import com.kodilla.notification.Client;
import com.kodilla.notification.Notification;

import java.util.*;
import java.util.stream.Collectors;

public class WeatherAlertService {
Map<String, Set<Subscriber>> subscriptions = new ArrayList<>(Arrays.asList("Warszawa", "Kraków", "Katowice"))
.stream()
.collect(Collectors.toMap(localization -> localization, localization -> new HashSet<>()));

public void addSubscriberToLocalization(Subscriber subscriber, String localization) {
if (subscriptions.containsKey(localization)) {
subscriptions.get(localization).add(subscriber);
}
}

public void removeSubscriberFromLocalization(Subscriber subscriber, String localization) {
if (subscriptions.containsKey(localization)) {
subscriptions.get(localization).remove(subscriber);
}
}

public void removeSubscriberFromAllLocalizations(Subscriber subscriber) {
subscriptions.values().forEach(subscribers -> subscribers.remove(subscriber));
}

public void sendNotificationToAllSubscribers(Alert alert) {
Set<Subscriber> allSubscribers = subscriptions.values().stream().flatMap(subscribers -> subscribers.stream()).collect(Collectors.toSet());
allSubscribers.forEach(subscriber -> subscriber.receive(alert));
}

public void sendNotificationToLocalSubscribers(Alert alert, String localization) {
if (subscriptions.containsKey(localization)) {
subscriptions.get(localization).forEach(subscriber -> subscriber.receive(alert));
}
}

public void removeLocalization(String localization) {
subscriptions.remove(localization);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.kodilla.notification;

public interface Client {
void receive(Notification notification);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.kodilla.notification;

public interface Notification {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.kodilla.mockito;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

public class MobilePhoneTestSuite {
MobilePhone myPhone = Mockito.mock(MobilePhone.class);

@Test
public void testDefaultBehaviourOfTestDouble() {
Assertions.assertFalse(myPhone.needsCharging());
Assertions.assertEquals(0.0, myPhone.getFreeStorage());
}

@Test
public void testExpectation() {
Assertions.assertFalse(myPhone.needsCharging());
Mockito.when(myPhone.needsCharging()).thenReturn(true);
Assertions.assertTrue(myPhone.needsCharging());
}

@Test
public void shouldCallLaunchApplication() {
myPhone.launchApplication("Tetris4D");
Mockito.verify(myPhone).launchApplication("Tetris4D");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.kodilla.mockito;

import com.kodilla.notification.Client;
import com.kodilla.notification.Notification;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

public class NotificationServiceTestSuite {

NotificationService notificationService = new NotificationService();
Client client = Mockito.mock(Client.class);
Client secondClient = Mockito.mock(Client.class);
Client thirdClient = Mockito.mock(Client.class);
Notification notification = Mockito.mock(Notification.class);

@Test
public void notSubscribedClientShouldNotReceiveNotification() {
notificationService.sendNotification(notification);

Mockito.verify(client, Mockito.never()).receive(notification);
}

@Test
public void subscribedClientShouldReceiveNotification() {
notificationService.addSubscriber(client);

notificationService.sendNotification(notification);

Mockito.verify(client).receive(notification);
}

@Test
public void notificationShouldBeSentToAllSubscribedClients() {
addSubscribers(client, secondClient, thirdClient);

notificationService.sendNotification(notification);

Mockito.verify(client).receive(notification);
Mockito.verify(secondClient).receive(notification);
Mockito.verify(thirdClient).receive(notification);
}


@Test
public void shouldSendOnlyOneNotificationToMultiTimeSubscriber() {
addSubscribers(client, client, client);

notificationService.sendNotification(notification);

Mockito.verify(client).receive(notification);
}

@Test
public void unsubscribedClientShouldNotReceiveNotification() {
notificationService.addSubscriber(client);
notificationService.removeSubscriber(client);

notificationService.sendNotification(notification);

Mockito.verify(client, Mockito.never()).receive(notification);
}

private void addSubscribers(Client... clients) {
for(Client currentClient : clients) {
notificationService.addSubscriber(currentClient);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.kodilla.mockito.homework;

import com.kodilla.notification.Client;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

public class WeatherAlertServiceTestSuite {
WeatherAlertService service = new WeatherAlertService();
Subscriber firstSubscriber = Mockito.mock(Subscriber.class);
Subscriber secondSubscriber = Mockito.mock(Subscriber.class);
Subscriber thirdSubscriber = Mockito.mock(Subscriber.class);
Alert alert = Mockito.mock(Alert.class);

@Test
public void activeSubscriberShouldReceiveAlerts() {
service.addSubscriberToLocalization(firstSubscriber, "Warszawa");
service.addSubscriberToLocalization(firstSubscriber, "Kraków");
service.addSubscriberToLocalization(secondSubscriber, "Kraków");
service.sendNotificationToLocalSubscribers(alert, "Warszawa");
service.sendNotificationToLocalSubscribers(alert, "Kraków");

Mockito.verify(firstSubscriber, Mockito.times(2)).receive(alert);
Mockito.verify(secondSubscriber, Mockito.times(1)).receive(alert);
}

@Test
public void locallyRemovedSubscriberShouldNotReceiveAlerts() {
service.addSubscriberToLocalization(firstSubscriber, "Warszawa");
service.removeSubscriberFromLocalization(firstSubscriber, "Warszawa");
service.sendNotificationToLocalSubscribers(alert, "Warszawa");

Mockito.verify(firstSubscriber, Mockito.never()).receive(alert);
}

@Test
public void completelyRemovedSubscriberShouldNotReceiveAlerts() {
service.addSubscriberToLocalization(firstSubscriber, "Warszawa");
service.addSubscriberToLocalization(firstSubscriber, "Kraków");
service.removeSubscriberFromAllLocalizations(firstSubscriber);
service.sendNotificationToLocalSubscribers(alert, "Warszawa");
service.sendNotificationToLocalSubscribers(alert, "Kraków");
service.sendNotificationToAllSubscribers(alert);

Mockito.verify(firstSubscriber, Mockito.never()).receive(alert);
}

@Test
public void locallyInactiveSubscriberShouldNotReceiveAlerts() {
service.addSubscriberToLocalization(firstSubscriber, "Kraków");
service.addSubscriberToLocalization(secondSubscriber, "Warszawa");
service.sendNotificationToLocalSubscribers(alert, "Warszawa");
service.sendNotificationToLocalSubscribers(alert, "Kraków");
Mockito.verify(firstSubscriber, Mockito.times(1)).receive(alert);
Mockito.verify(secondSubscriber, Mockito.times(1)).receive(alert);
}

@Test
public void allSubscribersShouldReceiveGeneralAlerts() {
service.addSubscriberToLocalization(firstSubscriber, "Kraków");
service.addSubscriberToLocalization(secondSubscriber, "Warszawa");
service.sendNotificationToAllSubscribers(alert);
Mockito.verify(firstSubscriber, Mockito.times(1)).receive(alert);
Mockito.verify(secondSubscriber, Mockito.times(1)).receive(alert);
}

@Test
public void subscriberShouldNotReceiveAlertsForRemovedLocalization() {
service.addSubscriberToLocalization(secondSubscriber, "Warszawa");
service.removeLocalization("Warszawa");
service.sendNotificationToLocalSubscribers(alert, "Warszawa");
Mockito.verify(firstSubscriber, Mockito.never()).receive(alert);
}
}

0 comments on commit a50618b

Please sign in to comment.