Skip to content

Commit

Permalink
Merge pull request #33 from BME-MIT-IET/java_unit_test
Browse files Browse the repository at this point in the history
java unit tests
  • Loading branch information
darpleon authored May 22, 2024
2 parents 23043a3 + f2d8f54 commit 3471405
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 18 deletions.
10 changes: 8 additions & 2 deletions test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,19 @@ allprojects {

dependencies {
implementation 'com.google.guava:guava:31.1-jre'
testImplementation 'junit:junit:4.13.2'
implementation 'junit:junit:4.13.2'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.2'
testImplementation 'org.mockito:mockito-core:5.11.0'
testImplementation 'org.mockito:mockito-junit-jupiter:5.11.0'
implementation "com.badlogicgames.gdx:gdx-backend-headless:$gdxVersion"
testImplementation "com.badlogicgames.gdx:gdx-backend-headless:$gdxVersion"
testImplementation 'io.cucumber:cucumber-java:7.18.0'
testImplementation 'io.cucumber:cucumber-junit:7.18.0'
}

test {
useJUnitPlatform()
}
}

project(":desktop") {
Expand Down
11 changes: 6 additions & 5 deletions test/core/test/ControlPointTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import temalab.model.ControlPoint;
import temalab.model.Position;

import static org.junit.jupiter.api.Assertions.assertNotEquals;

public class ControlPointTest {
@Test
public void controlPointIdIsIncremental() {
ControlPoint controlPoint1 = new ControlPoint(new Position(0, 0), 0, 0, null);
ControlPoint controlPoint2 = new ControlPoint(new Position(0, 1), 0, 0, null);
ControlPoint controlPoint3 = new ControlPoint(new Position(0, 2), 0, 0, null);
Assert.assertTrue(controlPoint1.getId() != controlPoint3.getId());
Assert.assertTrue(controlPoint1.getId() != controlPoint2.getId());
Assert.assertTrue(controlPoint2.getId() != controlPoint3.getId());
assertNotEquals(controlPoint1.getId(), controlPoint3.getId());
assertNotEquals(controlPoint1.getId(), controlPoint2.getId());
assertNotEquals(controlPoint2.getId(), controlPoint3.getId());
}
}
11 changes: 0 additions & 11 deletions test/core/test/ExampleTest.java

This file was deleted.

32 changes: 32 additions & 0 deletions test/core/test/PositionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import org.junit.jupiter.api.Test;
import temalab.model.Position;

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


class PositionTest {
private final Position p1 = new Position(-1, 2);
private final Position p2 = new Position(3, 5);
private final Position p3 = new Position(0, 2);

@Test
void test_inDistance_true() {
assertTrue(p1.inDistance(p3, 2f));
}

@Test
void test_inDistance_false() {
assertFalse(p1.inDistance(p2, 2f));
}

@Test
void test_isNeighbouring_true() {
assertTrue(p1.isNeighbouring(p3));
}

@Test
void test_isNeighbouring_false() {
assertFalse(p1.isNeighbouring(p2));
}
}
107 changes: 107 additions & 0 deletions test/core/test/TeamTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import temalab.common.MainModel;
import temalab.model.Field;
import temalab.model.Position;
import temalab.model.Team;
import temalab.model.Unit;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class TeamTest {
private Team team;

@Mock
private MainModel mockMainModel;
@Mock
private Unit mockUnit;
@Mock
private Position mockPos;
@Mock
private Field mockField;

@BeforeEach
public void init() {
team = new Team("red", "", 100, mockMainModel);
}

@Test
void test_addUnit_canAfford() {
when(mockUnit.price()).thenReturn(40);
team.addUnit(mockUnit);
assertFalse(team.units().isEmpty());
}

@Test
void test_addUnit_canNotAfford() {
when(mockUnit.price()).thenReturn(120);
team.addUnit(mockUnit);
assertTrue(team.units().isEmpty());
}

void addMockUnitWithId(int id) {
when(mockUnit.getUUID()).thenReturn(id);
when(mockUnit.price()).thenReturn(40);
team.addUnit((mockUnit));
}

@Test
void test_moveUnit_OK() {
addMockUnitWithId(1);

when(mockMainModel.getField(mockPos)).thenReturn(mockField);
team.moveUnit(1, mockPos);
verify(mockUnit, times(1)).move(mockField);
}

@Test
void test_moveUnit_fieldMissing() {
addMockUnitWithId(1);

when(mockMainModel.getField(mockPos)).thenReturn(null);
team.moveUnit(1, mockPos);
verify(mockUnit, times(0)).move(mockField);
}

@Test
void test_moveUnit_unitMissing() {
addMockUnitWithId(1);

when(mockMainModel.getField(mockPos)).thenReturn(mockField);
team.moveUnit(2, mockPos);
verify(mockUnit, times(0)).move(mockField);
}

@Test
void test_fireUnit_OK() {
addMockUnitWithId(1);

when(mockMainModel.getField(any(Position.class))).thenReturn(mockField);
team.fireUnit(1, mockPos);
verify(mockUnit, times(1)).shoot(mockField);
}

@Test
void test_fireUnit_fieldMissing() {
addMockUnitWithId(1);

when(mockMainModel.getField(any(Position.class))).thenReturn(null);
team.fireUnit(1, mockPos);
verify(mockUnit, times(0)).shoot(mockField);
}

@Test
void test_fireUnit_unitMissing() {
addMockUnitWithId(1);

when(mockMainModel.getField(any(Position.class))).thenReturn(mockField);
team.fireUnit(2, mockPos);
verify(mockUnit, times(0)).shoot(mockField);
}
}

0 comments on commit 3471405

Please sign in to comment.