Testing Dog Audio - UQcsse3200/2024-studio-2 GitHub Wiki

Unit Testing the DogSoundPlayer Class

To ensure that the DogSoundPlayer class functions as intended, a series of unit tests were created. These tests focus on the functionality of playing and stopping sounds, specifically for dog panting and barking sounds. Below is a detailed explanation of how these tests were developed.

1. Mocking Sound Objects

Since DogSoundPlayer depends on external sound files, Mockito was used to create mock objects for the panting and barking sounds. This allows simulation of sound behavior without needing the actual sound files. The mock() method is used to create these mock sound objects:

mockPantingSound = Mockito.mock(Sound.class);
mockBarkingSound = Mockito.mock(Sound.class);

2. Setting Up the DogSoundPlayer

In the setUp() method, the DogSoundPlayer class is instantiated, passing the mocked sound objects as parameters. This isolates the unit tests from external dependencies, allowing the focus to remain on verifying the class behavior:

dogSoundPlayer = new DogSoundPlayer(mockPantingSound, mockBarkingSound);

3. Testing Panting Sound Functionality

Several tests ensure that the panting sound behaves correctly in different situations.

public void testPlayPantingSound()

Playing the Panting Sound: This test verifies that the panting sound is triggered correctly by calling the playPantingSound() method. The test mocks the return value of the loop() method to simulate the sound playing and checks that the method is called with the correct volume:

@Test
public void testPlayPantingSound() {
    float volume = 0.5f;
    long mockPantingSoundId = 1L;
    when(mockPantingSound.loop(volume)).thenReturn(mockPantingSoundId);
    
    dogSoundPlayer.playPantingSound(volume);

    verify(mockPantingSound, times(1)).loop(volume);
}

stopPantingSound()

Stopping the Panting Sound: A corresponding test ensures that the panting sound can be stopped using the stopPantingSound() method. This method stops the specific instance of the sound identified by its ID:

@Test
public void testStopPantingSound() {
    float volume = 0.5f;
    long mockPantingSoundId = 1L;
    when(mockPantingSound.loop(volume)).thenReturn(mockPantingSoundId);

    dogSoundPlayer.playPantingSound(volume);
    dogSoundPlayer.stopPantingSound();

    verify(mockPantingSound, times(1)).stop(mockPantingSoundId);
}

4. Testing UpdatePantingSound Functionality

The updatePantingSound() method checks the dog's movement state and plays or stops the panting sound accordingly. Several tests were written to cover both possible states.

Playing Sound When Moving: This test ensures that the panting sound plays when the dog is moving. The loop() method is expected to be called only when the movement flag is set to true:

@Test
public void testUpdatePantingSound_PlaySoundWhenMoving() {
    float volume = 0.5f;
    long mockPantingSoundId = 1L;
    when(mockPantingSound.loop(volume)).thenReturn(mockPantingSoundId);

    dogSoundPlayer.updatePantingSound(true, volume);

    verify(mockPantingSound, times(1)).loop(volume);
}

Stopping Sound When Not Moving: This test ensures that the panting sound is stopped when the dog is not moving. It verifies that the stop() method is called to halt the sound:

@Test
public void testUpdatePantingSound_StopSoundWhenNotMoving() {
    float volume = 0.5f;
    long mockPantingSoundId = 1L;
    when(mockPantingSound.loop(volume)).thenReturn(mockPantingSoundId);

    dogSoundPlayer.playPantingSound(volume);
    dogSoundPlayer.updatePantingSound(false, volume);

    verify(mockPantingSound, times(1)).stop(mockPantingSoundId);
}

5. Testing Barking Sound Functionality

Similar to the panting sound, a test was created to ensure that the barking sound is played correctly when the playBarkingSound() method is called. This method directly plays the sound using the play() method:

@Test
public void testPlayBarkingSound() {
    float volume = 0.7f;
    dogSoundPlayer.playBarkingSound(volume);

    verify(mockBarkingSound, times(1)).play(volume);
}

6. Edge Cases for Panting Sound

Two additional tests cover edge cases for the panting sound:

Playing Panting Sound When Already Playing: This test ensures that the panting sound is not played multiple times if it is already looping. The sound should only be played once:

@Test
public void testPlayPantingSound_AlreadyPlaying() {
    float volume = 0.5f;
    long mockPantingSoundId = 1L;
    when(mockPantingSound.loop(volume)).thenReturn(mockPantingSoundId);

    dogSoundPlayer.playPantingSound(volume);
    dogSoundPlayer.playPantingSound(volume);

    verify(mockPantingSound, times(1)).loop(volume);
}

Stopping Panting Sound When Not Playing: This test ensures that the stopPantingSound() method does nothing if the panting sound is not currently playing:

@Test
public void testStopPantingSound_NotPlaying() {
    dogSoundPlayer.stopPantingSound();

    verify(mockPantingSound, times(0)).stop(anyLong());
}

Back