Last active
February 9, 2026 08:31
-
-
Save trikitrok/099f892f67224ebd6f65f9f9b7a2c0c5 to your computer and use it in GitHub Desktop.
Alarm tests with no relevant mutants surviving
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package unit_tests; | |
| import org.junit.jupiter.api.Test; | |
| import org.junit.jupiter.params.ParameterizedTest; | |
| import org.junit.jupiter.params.provider.ValueSource; | |
| import tirepressuremonitoringsystem.Alarm; | |
| import java.util.*; | |
| import java.util.stream.Stream; | |
| import static org.assertj.core.api.Assertions.assertThat; | |
| class AlarmTest { | |
| private AlarmForTesting alarm; | |
| @Test | |
| void alarm_activates_when_sampled_pressure_is_too_low() { | |
| alarm = alarmSampling(16.99); | |
| alarm.check(); | |
| checkShownMessagesWere("Alarm activated!"); | |
| } | |
| @ParameterizedTest | |
| @ValueSource(doubles = {17.00, 21.00}) | |
| void alarm_does_not_activate_when_sampled_pressure_is_safe(double sampledPressure) { | |
| alarm = alarmSampling(sampledPressure); | |
| alarm.check(); | |
| checkNoMessagesWereShown(); | |
| } | |
| @Test | |
| void alarm_activates_when_sampled_pressure_is_too_high() { | |
| alarm = alarmSampling(21.01); | |
| alarm.check(); | |
| checkShownMessagesWere("Alarm activated!"); | |
| } | |
| @Test | |
| void activated_alarm_does_not_notify_activation_again_when_sampled_pressure_is_not_safe() { | |
| alarm = anActivatedAlarmSampling(25.0); | |
| alarm.check(); | |
| checkShownMessagesWere("Alarm activated!"); | |
| } | |
| @Test | |
| public void deactivated_alarm_after_activation_activates_when_sampled_pressure_is_not_safe() { | |
| alarm = aDeactivatedAfterActivationAlarmSampling(50.0); | |
| alarm.check(); | |
| checkShownMessagesWere("Alarm activated!", "Alarm deactivated!", "Alarm activated!"); | |
| } | |
| private void checkNoMessagesWereShown() { | |
| checkShownMessagesWere(); | |
| } | |
| private void checkShownMessagesWere(String... expectedMessages) { | |
| assertThat(alarm.shownMessages).isEqualTo(Arrays.asList(expectedMessages)); | |
| } | |
| private AlarmForTesting aDeactivatedAfterActivationAlarmSampling(double sampledValue) { | |
| var alarm = anActivatedAlarmSampling(18.00, sampledValue); | |
| alarm.check(); | |
| return alarm; | |
| } | |
| private AlarmForTesting anActivatedAlarmSampling(Double... sampledValues) { | |
| var alarm = alarmSampling(Stream.concat(Stream.of(200.00), Arrays.stream(sampledValues)).toArray(Double[]::new)); | |
| alarm.check(); | |
| return alarm; | |
| } | |
| private AlarmForTesting alarmSampling(Double... values) { | |
| return new AlarmForTesting(values); | |
| } | |
| static class AlarmForTesting extends Alarm { | |
| private final Queue<Double> sampledValues; | |
| public List<String> shownMessages; | |
| public AlarmForTesting(Double... values) { | |
| this.shownMessages = new ArrayList<>(); | |
| this.sampledValues = new ArrayDeque<>(Arrays.asList(values)); | |
| } | |
| @Override | |
| protected void notify(String message) { | |
| shownMessages.add(message); | |
| } | |
| @Override | |
| protected double sampleValue() { | |
| return sampledValues.remove(); | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment