loved lab04

This commit is contained in:
schrom01 2022-04-27 22:05:13 +02:00
parent 335657a304
commit 530035ce1b
3 changed files with 94 additions and 45 deletions

View File

@ -65,14 +65,30 @@ public class Heart {
* InvalidValvePositionException occurs during diastole or systole
* TODO Implement a pause mechanism based on the current heart rate
*/
public void executeHeartBeat() throws HeartBeatDysfunctionException, Valve.IllegalValveStateException, InvalidValvePositionException {
public void executeHeartBeat() throws HeartBeatDysfunctionException {
//TODO implement
for(int i = 0; i < 2; i ++){
if(state == State.DIASTOLE) {
try {
executeDiastole();
} catch (Valve.IllegalValveStateException e) {
e.printStackTrace();
throw new HeartBeatDysfunctionException(e.getMessage());
} catch (InvalidValvePositionException e) {
e.printStackTrace();
throw new HeartBeatDysfunctionException(e.getMessage());
}
}
else if(state == State.SYSTOLE){
try {
executeSystole();
} catch (Valve.IllegalValveStateException e) {
e.printStackTrace();
throw new HeartBeatDysfunctionException(e.getMessage());
} catch (InvalidValvePositionException e) {
e.printStackTrace();
throw new HeartBeatDysfunctionException(e.getMessage());
}
}
}

View File

@ -91,7 +91,7 @@ class HeartTest {
@Test
void testExecuteHartBeatErrorBehaviourWithStubbing() {
//TODO implement
Half half = mock(Half.class, withSettings().useConstructor(Half.Side.LEFT));
Half half = spy(new Half(Half.Side.LEFT));
Heart heart = new Heart(half, new Half(Half.Side.RIGHT));
half.initializeState(heart.getState());
when(half.isAtrioventricularValveOpen()).thenReturn(false);
@ -109,60 +109,77 @@ class HeartTest {
*
*/
@Test
void testValvesBehavior() throws Valve.IllegalValveStateException, Heart.HeartBeatDysfunctionException, Heart.InvalidValvePositionException {
void testValvesBehavior() throws Valve.IllegalValveStateException, Heart.HeartBeatDysfunctionException {
//TODO implement
Half half1 = mock(Half.class, withSettings().useConstructor(Half.Side.LEFT));
Half half2 = mock(Half.class, withSettings().useConstructor(Half.Side.RIGHT));
Heart heart = new Heart(half1, half2);
Half halfLeft = spy(new Half(Half.Side.LEFT));
Half halfRight = spy(new Half(Half.Side.RIGHT));
Heart heart = new Heart(halfLeft, halfRight);
heart.executeHeartBeat();
InOrder inOrderHalf1 = inOrder(half1);
InOrder inOrderHalfLeft = inOrder(halfLeft);
inOrderHalf1.verify(half1).closeAtrioventricularValve();
inOrderHalf1.verify(half1).openSemilunarValve();
inOrderHalf1.verify(half1).relaxAtrium();
inOrderHalf1.verify(half1).relaxVentricle();
inOrderHalfLeft.verify(halfLeft).openAtrioventricularValve();
inOrderHalfLeft.verify(halfLeft).closeSemilunarValve();
inOrderHalfLeft.verify(halfLeft).relaxAtrium();
inOrderHalfLeft.verify(halfLeft).relaxVentricle();
inOrderHalf1.verify(half1).closeAtrioventricularValve();
inOrderHalf1.verify(half1).openSemilunarValve();
inOrderHalf1.verify(half1).contractAtrium();
inOrderHalf1.verify(half1).contractVentricle();
inOrderHalfLeft.verify(halfLeft).closeAtrioventricularValve();
inOrderHalfLeft.verify(halfLeft).openSemilunarValve();
inOrderHalfLeft.verify(halfLeft).contractAtrium();
inOrderHalfLeft.verify(halfLeft).contractVentricle();
inOrderHalf1.verifyNoMoreInteractions();
inOrderHalfLeft.verifyNoMoreInteractions();
InOrder inOrderHalf2 = inOrder(half2);
InOrder inOrderHalfRight = inOrder(halfRight);
inOrderHalf2.verify(half2).closeAtrioventricularValve();
inOrderHalf2.verify(half2).openSemilunarValve();
inOrderHalf2.verify(half2).relaxAtrium();
inOrderHalf2.verify(half2).relaxVentricle();
inOrderHalfRight.verify(halfRight).openAtrioventricularValve();
inOrderHalfRight.verify(halfRight).closeSemilunarValve();
inOrderHalfRight.verify(halfRight).relaxAtrium();
inOrderHalfRight.verify(halfRight).relaxVentricle();
inOrderHalf2.verify(half2).closeAtrioventricularValve();
inOrderHalf2.verify(half2).openSemilunarValve();
inOrderHalf2.verify(half2).contractAtrium();
inOrderHalf2.verify(half2).contractVentricle();
inOrderHalfRight.verify(halfRight).closeAtrioventricularValve();
inOrderHalfRight.verify(halfRight).openSemilunarValve();
inOrderHalfRight.verify(halfRight).contractAtrium();
inOrderHalfRight.verify(halfRight).contractVentricle();
inOrderHalf2.verifyNoMoreInteractions();
inOrderHalfRight.verifyNoMoreInteractions();
}
/**
* This is code used for the lecture slide.
*/
@Test
void testForSlide() throws Valve.IllegalValveStateException, Heart.InvalidValvePositionException {
Half mockedHalf = mock(Half.class);
Heart heart = new Heart(mockedHalf, new Half(Half.Side.RIGHT));
heart.setState(State.SYSTOLE);
heart.initalizeState();
when(mockedHalf.isAtrioventricularValveOpen()).thenReturn(false);
when(mockedHalf.isSemilunarValveOpen()).thenReturn(true);
heart.executeSystole();
verify(mockedHalf).contractVentricle();
verify(mockedHalf, times(1)).contractAtrium();
void testDiastoleExceptionAtrioventricularValve(){
Half half = spy(new Half(Half.Side.LEFT));
Heart heart = new Heart(half, new Half(Half.Side.RIGHT));
when(half.isAtrioventricularValveOpen()).thenReturn(true);
assertThrows(Heart.InvalidValvePositionException .class, // verification using lambda
() -> heart.executeDiastole());
}
@Test
void testDiastoleExceptionSemilunarValve(){
Half half = spy(new Half(Half.Side.LEFT));
Heart heart = new Heart(half, new Half(Half.Side.RIGHT));
when(half.isSemilunarValveOpen()).thenReturn(false);
assertThrows(Heart.InvalidValvePositionException .class, // verification using lambda
() -> heart.executeDiastole());
}
@Test
void testSystoleExceptionAtrioventricularValve(){
Half half = spy(new Half(Half.Side.LEFT));
Heart heart = new Heart(half, new Half(Half.Side.RIGHT));
when(half.isAtrioventricularValveOpen()).thenReturn(false);
assertThrows(Heart.InvalidValvePositionException .class, // verification using lambda
() -> heart.executeSystole());
}
@Test
void testSystoleExceptionSemilunarValve(){
Half half = spy(new Half(Half.Side.LEFT));
Heart heart = new Heart(half, new Half(Half.Side.RIGHT));
when(half.isSemilunarValveOpen()).thenReturn(true);
assertThrows(Heart.InvalidValvePositionException .class, // verification using lambda
() -> heart.executeSystole());
}
}

View File

@ -1,6 +1,8 @@
package ch.zhaw.prog2.heartbeat;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import static org.junit.jupiter.api.Assertions.*;
/*
@ -8,13 +10,25 @@ import static org.junit.jupiter.api.Assertions.*;
*/
public class PacemakerTest {
@Mock
Pacemaker pacemaker = new Pacemaker(new Heart());
/**
* Test if setHeartRate does throw correct exception when rate is rejected (because frequency is out of range)
*/
@Test
void testSetHeartRateRejectsFrequenciesOutOfRange() {
//TODO implement
fail();
for(int i = -10; i < 29; i ++){
int finalI = i;
assertThrows(IllegalArgumentException .class, // verification using lambda
() -> pacemaker.setHeartRate(finalI));
}
for(int i = 221; i < 230; i ++){
int finalI1 = i;
assertThrows(IllegalArgumentException .class, // verification using lambda
() -> pacemaker.setHeartRate(finalI1));
}
}
@ -24,7 +38,9 @@ public class PacemakerTest {
@Test
void testSetHeartRateAppliesFrequenciesInsideRange() {
//TODO implement
fail();
for(int i = 30; i <= 220; i ++){
assertEquals(i, pacemaker.setHeartRate(i));
}
}
}