Skip to content

Content of file GameMemoryCapacityTest.java

package de.fhdw.gaming.ipspiel23.memory;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

/**
 * Tests for the {@link GameMemoryCapacity} class.
 */
public class GameMemoryCapacityTest {

    /**
     * Tests the {@link GameMemoryCapacity#unlimited()} method.
     */
    @Test
    void testUnlimited() {
        final IGameMemoryCapacity capacity = GameMemoryCapacity.unlimited();

        assertTrue(capacity.isUnlimited());
        assertFalse(capacity.isDefault());
        assertEquals(-1, capacity.getCapacity());
    }

    /**
     * Tests the {@link GameMemoryCapacity#of(int)} method.
     */
    @Test
    void testOf() {
        final int customCapacity = 10;
        final IGameMemoryCapacity capacity = GameMemoryCapacity.of(customCapacity);

        assertFalse(capacity.isUnlimited());
        assertFalse(capacity.isDefault());
        assertEquals(customCapacity, capacity.getCapacity());
    }

    /**
     * Tests the {@link GameMemoryCapacity#useDefault()} method.
     */
    @Test
    void testUseDefault() {
        final IGameMemoryCapacity capacity = GameMemoryCapacity.useDefault();

        assertFalse(capacity.isUnlimited());
        assertTrue(capacity.isDefault());
        assertEquals(5, capacity.getCapacity());
    }

    /**
     * Tests that an {@link IllegalArgumentException} is thrown when passing an invalid capacity to the
     * {@link GameMemoryCapacity#of(int)} method.
     */
    @Test
    void testInvalidCapacity() {
        final Integer invalidCapacity = -1;

        IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
            GameMemoryCapacity.of(invalidCapacity);
        });

        assertEquals("Capacity must be greater than or equal to 0", exception.getMessage());
    }
}
JUnit 5 tests should be package-private.
Reports JUnit 5 test classes and methods that are not package-private. Contrary to JUnit 4 tests, which required public visibility to be run by the engine, JUnit 5 tests can also be run if they're package-private. Marking them as such is a good practice to limit their visibility. Test methods are identified as those which use `@Test`, `@RepeatedTest`, `@TestFactory`, `@TestTemplate` or `@ParameterizedTest`.
    
        
            
class MyTest { // not public, that's fine
    @Test
    public void testBad() { } // should not have a public modifier

    @Test
    protected void testAlsoBad() { } // should not have a protected modifier

    @Test
    private void testNoRun() { } // should not have a private modifier

    @Test
    void testGood() { } // package private as expected
}

        
    
See PMD documentation.