Skip to content

Commit

Permalink
Repairman action test and Jacoco setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Duhi95 committed May 19, 2024
1 parent f973609 commit 83632b6
Show file tree
Hide file tree
Showing 3 changed files with 282 additions and 14 deletions.
29 changes: 28 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@
<version>5.8.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.4.0</version>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -54,6 +61,26 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
13 changes: 0 additions & 13 deletions src/test/java/CharacterTest.java

This file was deleted.

254 changes: 254 additions & 0 deletions src/test/java/RepairmanActionsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Stream;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.api.Nested;
import org.mockito.MockedStatic;

public class RepairmanActionsTest {
Repairman rm;
ArrayList<Element> elements;
Pipe pipe;
Pump pump;



@BeforeEach
void CreatesAPipeAndAPumpWithARepairmanOnAPipe(){
rm = new Repairman();
pump = new Pump();
pipe = new Pipe();
elements = new ArrayList<Element>();
elements.add(pipe);
elements.add(pump);
rm.setPosition(pipe);
}

static NonPipe[] nonPipes(){
return new NonPipe[]{new Pump(),new Cistern(),new WaterSource()};
}

@ParameterizedTest(name = "Testing the {index}.NonPipe type to move onto from a Pipe")
@MethodSource(value = "nonPipes")
void givenRepairmanWhenMovesToAPermittedFieldThenMoved(NonPipe nonPipe){
elements.add(nonPipe);
try (MockedStatic<Game> mockedStaticGame = mockStatic(Game.class)) {
Game mockedGame = mock(Game.class);
mockedStaticGame.when(Game::getInstance).thenReturn(mockedGame);
when(mockedGame.getGameElements()).thenReturn(elements);

AddEachotherToNeighbors(pipe, nonPipe);
rm.move(0);
assertNotEquals(pipe, rm.getPosition());
assertEquals(nonPipe, rm.getPosition());
}
}


@Test
void givenRepairmanWhenMovesToANotPermittedFieldThenStayed(){
try (MockedStatic<Game> mockedStaticGame = mockStatic(Game.class)) {
Game mockedGame = mock(Game.class);
mockedStaticGame.when(Game::getInstance).thenReturn(mockedGame);
when(mockedGame.getGameElements()).thenReturn(elements);

rm.move(0);
assertEquals(pipe, rm.getPosition());
}
}


@Test
void givenRepairmanWhenRepairsABrokenPipeThenGetsFixed(){
pipe = new Pipe(true,false,0,0,0);
rm.setPosition(pipe);
assertTrue(pipe.getHoleOnPipe());

rm.RepairElement();
assertFalse(pipe.getHoleOnPipe());

}


@Test
void givenRepairmanWhenRepairsABrokenPumpThenGetsFixed(){
pump = new Pump(true,false,0);
rm.setPosition(pump);
assertTrue(pump.getBroken());

rm.RepairElement();
assertFalse(pump.getBroken());
}


@TestFactory
Stream<DynamicTest> givenRepairmanWithoutPumpWhenAsksForPumpAtElementsThenGetsOrNot(){
Object[][] data = new Object[][]{{new Pipe(),"pipe",false},
{new Pump(),"pump",false},
{new Cistern(),"cistern",true},
{new WaterSource(),"watersource",false}};
return Arrays.stream(data).map(entry ->{
Element element = (Element)entry[0];
String name = (String)entry[1];
Boolean expected = (Boolean)entry[2];
return dynamicTest("Repairman on a "+name+ " gets a pump => "+expected,() -> {
rm = new Repairman();
rm.setPosition(element);
rm.LiftPump();
if(expected)
assertNotEquals(null, rm.getHoldingPump());
else
assertEquals(null, rm.getHoldingPump());
});
});
}


@Test
void givenRepairmanWithPumpWhenTriesToPlaceAPumpAtPipeThenItsPlaced(){
assertEquals(null,rm.getHoldingPump());

Pump pump2 = new Pump();
AddEachotherToNeighbors(pipe, pump2);
AddEachotherToNeighbors(pipe, pump);

rm.setHoldingPump(new Pump());
assertNotEquals(null,rm.getHoldingPump());

ElementButton elementButton =new ElementButton(pump);
try (MockedStatic<Gui> mockedStaticGui = mockStatic(Gui.class)) {
Gui mockedGui = mock(Gui.class);
mockedStaticGui.when(Gui::getInstance).thenReturn(mockedGui);
when(mockedGui.findElementButton(pump)).thenReturn(elementButton);
when(mockedGui.findElementButton(pump2)).thenReturn(elementButton);
when(mockedGui.findElementButton((Element)pipe)).thenReturn(elementButton);

rm.PlacePump();
assertEquals(null,rm.getHoldingPump());
}

}

@Test
void givenRepairmanWithPumpWhenTriesToPlaceAPumpAtNonPipeThenNotPlaced(){
assertEquals(null,rm.getHoldingPump());

Pipe pipe2 = new Pipe();
AddEachotherToNeighbors(pipe, pump);
AddEachotherToNeighbors(pipe2, pump);

rm.setHoldingPump(new Pump());
assertNotEquals(null,rm.getHoldingPump());

rm.setPosition(pump);

rm.PlacePump();
assertNotEquals(null,rm.getHoldingPump());

}


@Test
void givenRepairmanWithoutPipeWhenTriesToPickAPipeAtCisternThenItsPickedUp(){
Cistern cistern = new Cistern();
AddEachotherToNeighbors(pipe, cistern);
rm.setPosition(cistern);
assertEquals(null,rm.getHoldingPipe());

rm.LiftPipe(0);
assertAll("End asserts",
() ->assertNotEquals(null,rm.getHoldingPipe()),
() ->assertEquals(pipe, rm.getHoldingPipe())
);
}

@Test
void givenRepairmanWithoutPipeWhenTriesToPickAPipeDoesntExistsAtCisternThenNotPickedUp(){
Cistern cistern = new Cistern();
AddEachotherToNeighbors(pipe, cistern);
rm.setPosition(cistern);
assertEquals(null,rm.getHoldingPipe());

rm.LiftPipe(3);
assertEquals(null,rm.getHoldingPipe());
}


@Test
void givenRepairmanWithoutPipeWhenTriesToPickThePipeAtPipeThenItsPickedUp(){
Cistern cistern = new Cistern();
AddEachotherToNeighbors(pipe, cistern);
assertEquals(null,rm.getHoldingPipe());

rm.LiftPipe(0);
assertAll("End asserts",
() -> assertNotEquals(null,rm.getHoldingPipe()),
() -> assertEquals(pipe, rm.getHoldingPipe())
);
}


@DisplayName("Grouped test for placing pipe")
@Nested
class PipePlacing{

@BeforeEach
void setNeighborsAndPosition(){
AddEachotherToNeighbors(pipe, pump);
rm.setPosition(pump);
}

@Disabled("The pump gets 2 more neighbors instead of 1 after connecting a pipe to it.")
@Test
void givenRepairmanWithPipeWhenTriesToPlaceThePipeAtNonPipeThenItsPlaced(){
rm.setHoldingPipe(new Pipe());
assertNotEquals(null,rm.getHoldingPipe());

assertEquals(1, pump.getNeighbors().size());

rm.PlacePipe();
assertAll("End asserts",
() -> assertEquals(null,rm.getHoldingPipe()),
() -> assertEquals(2, pump.getNeighbors().size())
);
}

@Test
void givenRepairmanWithoutPipeWhenTriesToPlaceThePipeAtNonPipeThenNothing(){
assertEquals(null,rm.getHoldingPipe());

rm.PlacePipe();
assertAll("End asserts",
() -> assertEquals(null,rm.getHoldingPipe()),
() -> assertEquals(1, pump.getNeighbors().size())
);
}

}




void AddEachotherToNeighbors(Pipe pipe,NonPipe nonPipe){
pipe.addNeighbor(nonPipe);
nonPipe.addNeighbor(pipe);
}



}

0 comments on commit 83632b6

Please sign in to comment.