-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for DayEndingEvent and UnmaintainedUnitsNagDialog
Added tests to verify DayEndingEvent can be canceled and to check various unit states in UnmaintainedUnitsNagDialog.
- Loading branch information
1 parent
aa73b09
commit 53e757b
Showing
3 changed files
with
198 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
25 changes: 25 additions & 0 deletions
25
MekHQ/unittests/mekhq/campaign/event/DayEndingEventTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package mekhq.campaign.event; | ||
|
||
import mekhq.campaign.Campaign; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.mock; | ||
|
||
class DayEndingEventTest { | ||
/** | ||
* Unit test to verify if a DayEndingEvent can be canceled. | ||
*/ | ||
@Test | ||
void checkDayEndingEventCancellable() { | ||
// Creates a mock instance of the Campaign class. | ||
Campaign mockCampaign = mock(Campaign.class); | ||
|
||
// Creates a new DayEndingEvent associated with the mock Campaign. | ||
DayEndingEvent dayEndingEvent = new DayEndingEvent(mockCampaign); | ||
|
||
// Asserts that the isCancellable() method of the created DayEndingEvent returns true. | ||
// If it does not, this test will fail. | ||
assertTrue(dayEndingEvent.isCancellable()); | ||
} | ||
} |
172 changes: 172 additions & 0 deletions
172
MekHQ/unittests/mekhq/gui/dialog/nagDialogs/UnmaintainedUnitsNagDialogTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package mekhq.gui.dialog.nagDialogs; | ||
|
||
import mekhq.campaign.Campaign; | ||
import mekhq.campaign.Hangar; | ||
import mekhq.campaign.unit.Unit; | ||
import mekhq.campaign.universe.Systems; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class UnmaintainedUnitsNagDialogTest { | ||
// Mock objects for the tests | ||
private Campaign campaign; | ||
private Hangar hangar; | ||
private Unit mockUnit1, mockUnit2; | ||
|
||
// System setup for all tests, runs once before all tests | ||
@BeforeAll | ||
public static void setup() { | ||
try { | ||
Systems.setInstance(Systems.loadDefault()); | ||
} catch (Exception ex) { | ||
LogManager.getLogger().error("", ex); | ||
} | ||
} | ||
|
||
// Test setup for each test, runs before each test | ||
@BeforeEach | ||
public void init() { | ||
// Initialize the mock objects | ||
campaign = mock(Campaign.class); | ||
hangar = mock(Hangar.class); | ||
mockUnit1 = mock(Unit.class); | ||
mockUnit2 = mock(Unit.class); | ||
|
||
// When the Campaign mock calls 'getHangar()' return the 'hangar' mock | ||
when(campaign.getHangar()).thenReturn(hangar); | ||
} | ||
|
||
// In the following tests, | ||
// Different combinations of unit states to set up desired behaviors in mock objects | ||
// Then the checkHanger() method of UnmaintainedUnitsNagDialog class is called, | ||
// and its response is checked against expected behavior | ||
|
||
@Test | ||
public void unmaintainedUnitExists() { | ||
when(mockUnit1.isUnmaintained()).thenReturn(false); | ||
when(mockUnit1.isSalvage()).thenReturn(false); | ||
|
||
when(mockUnit2.isUnmaintained()).thenReturn(true); | ||
when(mockUnit2.isSalvage()).thenReturn(false); | ||
|
||
List<Unit> units = List.of(mockUnit1, mockUnit2); | ||
when(hangar.getUnits()).thenReturn(units); | ||
|
||
UnmaintainedUnitsNagDialog nagDialog = new UnmaintainedUnitsNagDialog(null, campaign); | ||
assertTrue(nagDialog.checkHanger()); | ||
} | ||
|
||
@Test | ||
public void unmaintainedUnitExistsButSalvageUnit1() { | ||
when(mockUnit1.isUnmaintained()).thenReturn(false); | ||
when(mockUnit1.isSalvage()).thenReturn(true); | ||
|
||
when(mockUnit2.isUnmaintained()).thenReturn(true); | ||
when(mockUnit2.isSalvage()).thenReturn(false); | ||
|
||
List<Unit> units = List.of(mockUnit1, mockUnit2); | ||
when(hangar.getUnits()).thenReturn(units); | ||
|
||
UnmaintainedUnitsNagDialog nagDialog = new UnmaintainedUnitsNagDialog(null, campaign); | ||
assertTrue(nagDialog.checkHanger()); | ||
} | ||
|
||
@Test | ||
public void unmaintainedUnitExistsButSalvageUnit2() { | ||
when(mockUnit1.isUnmaintained()).thenReturn(false); | ||
when(mockUnit1.isSalvage()).thenReturn(false); | ||
|
||
when(mockUnit2.isUnmaintained()).thenReturn(true); | ||
when(mockUnit2.isSalvage()).thenReturn(true); | ||
|
||
List<Unit> units = List.of(mockUnit1, mockUnit2); | ||
when(hangar.getUnits()).thenReturn(units); | ||
|
||
UnmaintainedUnitsNagDialog nagDialog = new UnmaintainedUnitsNagDialog(null, campaign); | ||
assertFalse(nagDialog.checkHanger()); | ||
} | ||
|
||
@Test | ||
public void unmaintainedUnitExistsButSalvageMixed() { | ||
when(mockUnit1.isUnmaintained()).thenReturn(false); | ||
when(mockUnit1.isSalvage()).thenReturn(true); | ||
|
||
when(mockUnit2.isUnmaintained()).thenReturn(true); | ||
when(mockUnit2.isSalvage()).thenReturn(false); | ||
|
||
List<Unit> units = List.of(mockUnit1, mockUnit2); | ||
when(hangar.getUnits()).thenReturn(units); | ||
|
||
UnmaintainedUnitsNagDialog nagDialog = new UnmaintainedUnitsNagDialog(null, campaign); | ||
assertTrue(nagDialog.checkHanger()); | ||
} | ||
|
||
@Test | ||
public void noUnmaintainedUnitExists() { | ||
when(mockUnit1.isUnmaintained()).thenReturn(false); | ||
when(mockUnit1.isSalvage()).thenReturn(false); | ||
|
||
when(mockUnit2.isUnmaintained()).thenReturn(false); | ||
when(mockUnit2.isSalvage()).thenReturn(false); | ||
|
||
List<Unit> units = List.of(mockUnit1, mockUnit2); | ||
when(hangar.getUnits()).thenReturn(units); | ||
|
||
UnmaintainedUnitsNagDialog nagDialog = new UnmaintainedUnitsNagDialog(null, campaign); | ||
assertFalse(nagDialog.checkHanger()); | ||
} | ||
|
||
@Test | ||
public void noUnmaintainedUnitExistsButSalvageUnit1() { | ||
when(mockUnit1.isUnmaintained()).thenReturn(false); | ||
when(mockUnit1.isSalvage()).thenReturn(true); | ||
|
||
when(mockUnit2.isUnmaintained()).thenReturn(false); | ||
when(mockUnit2.isSalvage()).thenReturn(false); | ||
|
||
List<Unit> units = List.of(mockUnit1, mockUnit2); | ||
when(hangar.getUnits()).thenReturn(units); | ||
|
||
UnmaintainedUnitsNagDialog nagDialog = new UnmaintainedUnitsNagDialog(null, campaign); | ||
assertFalse(nagDialog.checkHanger()); | ||
} | ||
|
||
@Test | ||
public void noUnmaintainedUnitExistsButSalvageUnit2() { | ||
when(mockUnit1.isUnmaintained()).thenReturn(false); | ||
when(mockUnit1.isSalvage()).thenReturn(false); | ||
|
||
when(mockUnit2.isUnmaintained()).thenReturn(false); | ||
when(mockUnit2.isSalvage()).thenReturn(true); | ||
|
||
List<Unit> units = List.of(mockUnit1, mockUnit2); | ||
when(hangar.getUnits()).thenReturn(units); | ||
|
||
UnmaintainedUnitsNagDialog nagDialog = new UnmaintainedUnitsNagDialog(null, campaign); | ||
assertFalse(nagDialog.checkHanger()); | ||
} | ||
|
||
@Test | ||
public void noUnmaintainedUnitExistsButSalvageMixed() { | ||
when(mockUnit1.isUnmaintained()).thenReturn(false); | ||
when(mockUnit1.isSalvage()).thenReturn(true); | ||
|
||
when(mockUnit2.isUnmaintained()).thenReturn(false); | ||
when(mockUnit2.isSalvage()).thenReturn(false); | ||
|
||
List<Unit> units = List.of(mockUnit1, mockUnit2); | ||
when(hangar.getUnits()).thenReturn(units); | ||
|
||
UnmaintainedUnitsNagDialog nagDialog = new UnmaintainedUnitsNagDialog(null, campaign); | ||
assertFalse(nagDialog.checkHanger()); | ||
} | ||
} |