Skip to content

Commit

Permalink
Merge pull request #48 from BME-MIT-IET/subject_unit_test_david
Browse files Browse the repository at this point in the history
subject test
  • Loading branch information
Tamassch authored May 19, 2024
2 parents 3cedff5 + 01da877 commit 54ed11c
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/main/java/virologist/model/Subject.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import virologist.view.Observer;
import java.util.ArrayList;
import java.util.List;

/**
* Egy megfigyelt osztály, melynek változásaira fel lehet iratkozniuk Observereknek
Expand All @@ -10,7 +11,15 @@ public class Subject {
/**
* Observerek - megfigyelők listája
*/
ArrayList<Observer> observers = new ArrayList<>();
List<Observer> observers;

public Subject() {
observers = new ArrayList<>();
}

public Subject(List<Observer> observers) {
this.observers = observers;
}

/**
* Egy megfigyelő regisztrálása
Expand Down
84 changes: 84 additions & 0 deletions src/test/java/virologist/model/SubjectTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package virologist.model;

import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import virologist.view.Observer;
import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.*;
import java.util.ArrayList;

class SubjectTest {
private Subject subject;
List<Observer> observers;
Observer observer1;
Observer observer2;
Observer observer3;

@BeforeEach
public void init() {
observer1 = mock(Observer.class);
observer2 = mock(Observer.class);
observer3 = mock(Observer.class);
}

@SuppressWarnings("unchecked")
@Test
void attachTest_not_contained() {
// Arrange
observers = mock(ArrayList.class);
when(observers.contains(observer1)).thenReturn(false);
subject = new Subject(observers);

// Act
subject.attach(observer1);

// Assert
verify(observers, times(1)).add(observer1);
}

@SuppressWarnings("unchecked")
@Test
void attachTest_already_contained() {
// Arrange
observers = mock(ArrayList.class);
when(observers.contains(observer1)).thenReturn(true);
subject = new Subject(observers);

// Act
subject.attach(observer1);

// Assert
verify(observers, times(0)).add(observer1);
}

@SuppressWarnings("unchecked")
@Test
void detachTest() {
// Arrange
observers = mock(ArrayList.class);
subject = new Subject(observers);

// Act
subject.detach(observer1);

// Assert
verify(observers, times(1)).remove(observer1);
}

@Test
void notifyAllObserversTest() {
// Arrange
observers = new ArrayList<>();
observers.add(observer1);
observers.add(observer2);
subject = new Subject(observers);

// Act
subject.notifyAllObservers();

// Assert
verify(observer1, times(1)).update();
verify(observer2, times(1)).update();
verify(observer3, times(0)).update();
}
}

0 comments on commit 54ed11c

Please sign in to comment.