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 * InvalidValvePositionException occurs during diastole or systole
* TODO Implement a pause mechanism based on the current heart rate * 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 //TODO implement
for(int i = 0; i < 2; i ++){ for(int i = 0; i < 2; i ++){
if(state == State.DIASTOLE) { if(state == State.DIASTOLE) {
executeDiastole(); 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){ else if(state == State.SYSTOLE){
executeSystole(); 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 @Test
void testExecuteHartBeatErrorBehaviourWithStubbing() { void testExecuteHartBeatErrorBehaviourWithStubbing() {
//TODO implement //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)); Heart heart = new Heart(half, new Half(Half.Side.RIGHT));
half.initializeState(heart.getState()); half.initializeState(heart.getState());
when(half.isAtrioventricularValveOpen()).thenReturn(false); when(half.isAtrioventricularValveOpen()).thenReturn(false);
@ -109,60 +109,77 @@ class HeartTest {
* *
*/ */
@Test @Test
void testValvesBehavior() throws Valve.IllegalValveStateException, Heart.HeartBeatDysfunctionException, Heart.InvalidValvePositionException { void testValvesBehavior() throws Valve.IllegalValveStateException, Heart.HeartBeatDysfunctionException {
//TODO implement //TODO implement
Half half1 = mock(Half.class, withSettings().useConstructor(Half.Side.LEFT)); Half halfLeft = spy(new Half(Half.Side.LEFT));
Half half2 = mock(Half.class, withSettings().useConstructor(Half.Side.RIGHT)); Half halfRight = spy(new Half(Half.Side.RIGHT));
Heart heart = new Heart(half1, half2); Heart heart = new Heart(halfLeft, halfRight);
heart.executeHeartBeat(); heart.executeHeartBeat();
InOrder inOrderHalf1 = inOrder(half1); InOrder inOrderHalfLeft = inOrder(halfLeft);
inOrderHalf1.verify(half1).closeAtrioventricularValve(); inOrderHalfLeft.verify(halfLeft).openAtrioventricularValve();
inOrderHalf1.verify(half1).openSemilunarValve(); inOrderHalfLeft.verify(halfLeft).closeSemilunarValve();
inOrderHalf1.verify(half1).relaxAtrium(); inOrderHalfLeft.verify(halfLeft).relaxAtrium();
inOrderHalf1.verify(half1).relaxVentricle(); inOrderHalfLeft.verify(halfLeft).relaxVentricle();
inOrderHalf1.verify(half1).closeAtrioventricularValve(); inOrderHalfLeft.verify(halfLeft).closeAtrioventricularValve();
inOrderHalf1.verify(half1).openSemilunarValve(); inOrderHalfLeft.verify(halfLeft).openSemilunarValve();
inOrderHalf1.verify(half1).contractAtrium(); inOrderHalfLeft.verify(halfLeft).contractAtrium();
inOrderHalf1.verify(half1).contractVentricle(); inOrderHalfLeft.verify(halfLeft).contractVentricle();
inOrderHalf1.verifyNoMoreInteractions(); inOrderHalfLeft.verifyNoMoreInteractions();
InOrder inOrderHalf2 = inOrder(half2); InOrder inOrderHalfRight = inOrder(halfRight);
inOrderHalf2.verify(half2).closeAtrioventricularValve(); inOrderHalfRight.verify(halfRight).openAtrioventricularValve();
inOrderHalf2.verify(half2).openSemilunarValve(); inOrderHalfRight.verify(halfRight).closeSemilunarValve();
inOrderHalf2.verify(half2).relaxAtrium(); inOrderHalfRight.verify(halfRight).relaxAtrium();
inOrderHalf2.verify(half2).relaxVentricle(); inOrderHalfRight.verify(halfRight).relaxVentricle();
inOrderHalf2.verify(half2).closeAtrioventricularValve(); inOrderHalfRight.verify(halfRight).closeAtrioventricularValve();
inOrderHalf2.verify(half2).openSemilunarValve(); inOrderHalfRight.verify(halfRight).openSemilunarValve();
inOrderHalf2.verify(half2).contractAtrium(); inOrderHalfRight.verify(halfRight).contractAtrium();
inOrderHalf2.verify(half2).contractVentricle(); inOrderHalfRight.verify(halfRight).contractVentricle();
inOrderHalf2.verifyNoMoreInteractions(); inOrderHalfRight.verifyNoMoreInteractions();
} }
/**
* This is code used for the lecture slide.
*/
@Test @Test
void testForSlide() throws Valve.IllegalValveStateException, Heart.InvalidValvePositionException { void testDiastoleExceptionAtrioventricularValve(){
Half mockedHalf = mock(Half.class); Half half = spy(new Half(Half.Side.LEFT));
Heart heart = new Heart(mockedHalf, new Half(Half.Side.RIGHT)); Heart heart = new Heart(half, new Half(Half.Side.RIGHT));
heart.setState(State.SYSTOLE); when(half.isAtrioventricularValveOpen()).thenReturn(true);
heart.initalizeState(); assertThrows(Heart.InvalidValvePositionException .class, // verification using lambda
() -> heart.executeDiastole());
when(mockedHalf.isAtrioventricularValveOpen()).thenReturn(false);
when(mockedHalf.isSemilunarValveOpen()).thenReturn(true);
heart.executeSystole();
verify(mockedHalf).contractVentricle();
verify(mockedHalf, times(1)).contractAtrium();
} }
@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; package ch.zhaw.prog2.heartbeat;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
/* /*
@ -8,13 +10,25 @@ import static org.junit.jupiter.api.Assertions.*;
*/ */
public class PacemakerTest { 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 if setHeartRate does throw correct exception when rate is rejected (because frequency is out of range)
*/ */
@Test @Test
void testSetHeartRateRejectsFrequenciesOutOfRange() { void testSetHeartRateRejectsFrequenciesOutOfRange() {
//TODO implement //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 @Test
void testSetHeartRateAppliesFrequenciesInsideRange() { void testSetHeartRateAppliesFrequenciesInsideRange() {
//TODO implement //TODO implement
fail(); for(int i = 30; i <= 220; i ++){
assertEquals(i, pacemaker.setHeartRate(i));
}
} }
} }