Archived
0

JUnit4 -> JUnit5

This commit is contained in:
2018-10-07 21:31:00 +03:00
parent 7a8a3e6ad4
commit 7225efced9
13 changed files with 175 additions and 161 deletions

View File

@@ -22,6 +22,7 @@ subprojects {
slf4j_version = '1.7.25'
spring_version = '5.1.0.RELEASE'
lombok_version = '1.18.2'
junit_version = '5.3.1'
}
configurations {
@@ -42,12 +43,17 @@ subprojects {
compileOnly (group: 'org.projectlombok', name: 'lombok', version: lombok_version)
/* Testing */
testCompile (group: 'junit', name: 'junit', version: '4.12')
testImplementation (group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: junit_version)
testRuntimeOnly(group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: junit_version)
testCompile (group: 'org.slf4j', name: 'slf4j-simple', version: slf4j_version)
testCompile (group: 'org.mockito', name: 'mockito-core', version: '1.10.19')
testCompile (group: 'org.springframework', name: 'spring-test', version: spring_version)
}
test {
useJUnitPlatform()
}
task copyDep(type: Copy) {
into 'libs'
from configurations.compile + configurations.runtime - configurations.compile_excludeCopy

View File

@@ -1,28 +1,28 @@
package mc.core;
import mc.core.world.block.BlockLocation;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.concurrent.ThreadLocalRandom;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
public class TestBlockLocation {
class TestBlockLocation {
private static final ThreadLocalRandom rnd = ThreadLocalRandom.current();
private static final int minI = 0, maxI = 10;
private int x, y, z;
@Before
public void before() {
@BeforeEach
void before() {
x = rnd.nextInt(minI, maxI);
y = rnd.nextInt(minI, maxI);
z = rnd.nextInt(minI, maxI);
}
@Test
public void testEquals() {
void testEquals() {
BlockLocation loc1 = new BlockLocation(x, y, z);
BlockLocation loc2 = new BlockLocation(x, y, z);
assertEquals(loc1, loc2);
@@ -32,7 +32,7 @@ public class TestBlockLocation {
}
@Test
public void testClone() {
void testClone() {
BlockLocation locOrig = new BlockLocation(x, y, z);
BlockLocation locClone = locOrig.clone();
assertEquals(locOrig, locClone);

View File

@@ -1,22 +1,22 @@
package mc.core;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.concurrent.ThreadLocalRandom;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
public class TestEntityLocation {
class TestEntityLocation {
private static final ThreadLocalRandom rnd = ThreadLocalRandom.current();
private static final double minD = 0.0d, maxD = 10.0d;
private static final float minF = 0.0f, maxF = 359.9f;
private double x, y, z;
private float yaw, pitch;
@Before
public void before() {
@BeforeEach
void before() {
x = rnd.nextDouble(minD, maxD);
y = rnd.nextDouble(minD, maxD);
z = rnd.nextDouble(minD, maxD);
@@ -25,7 +25,7 @@ public class TestEntityLocation {
}
@Test
public void testEquals() {
void testEquals() {
EntityLocation loc1 = new EntityLocation(x, y, z, yaw, pitch);
EntityLocation loc2 = new EntityLocation(x, y, z, yaw, pitch);
assertEquals(loc1, loc2);
@@ -38,14 +38,14 @@ public class TestEntityLocation {
}
@Test
public void testClone() {
void testClone() {
EntityLocation locOrig = new EntityLocation(x, y, z, yaw, pitch);
EntityLocation locClone = locOrig.clone();
assertEquals(locOrig, locClone);
}
@Test
public void testGetBlockXZ() {
void testGetBlockXZ() {
EntityLocation location;
location = new EntityLocation(0d, 0, 0d, 0f, 0f);

View File

@@ -1,20 +1,17 @@
package mc.core;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class TestImmutableEntityLocation {
@Rule
public ExpectedException thrown = ExpectedException.none();
class TestImmutableEntityLocation {
@Test
public void testSetValue() {
void testSetValue() {
EntityLocation location = new ImmutableEntityLocation(1d, 2d, 3d, 4f, 5f);
thrown.expect(UnsupportedOperationException.class);
assertThrows(UnsupportedOperationException.class, () -> {
location.setX(1);
location.setY(1);
location.setZ(1);
@@ -23,10 +20,11 @@ public class TestImmutableEntityLocation {
location.setXYZ(1, 2, 3);
location.setYawPitch(1, 2);
location.set(EntityLocation.ZERO());
});
}
@Test
public void testClone() {
void testClone() {
EntityLocation locOrig = new ImmutableEntityLocation(1d, 2d, 3d, 4f, 5f);
EntityLocation locClone = locOrig.clone();

View File

@@ -1,12 +1,12 @@
package mc.core.text;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
public class TestText {
class TestText {
@Test
public void testToPlain() {
void testToPlain() {
final String m1 = "mes";
final String m2 = "sage";
final String message = m1 + m2;
@@ -26,7 +26,7 @@ public class TestText {
}
@Test
public void testEquals() {
void testEquals() {
assertEquals(Text.of(), Text.of(""));
assertEquals(Text.of(), Text.builder().build());
assertEquals(Text.of(), Text.builder("").build());
@@ -44,7 +44,7 @@ public class TestText {
}
@Test
public void testEmpty() {
void testEmpty() {
assertTrue(Text.of().isEmpty());
assertTrue(Text.of((String) null).isEmpty());
assertTrue(Text.of((Text) null).isEmpty());

View File

@@ -1,15 +1,14 @@
package mc.core.utils;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.util.concurrent.ThreadLocalRandom;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestCompactedCoords {
class TestCompactedCoords {
@Test
public void testXZ() {
void testXZ() {
ThreadLocalRandom random = ThreadLocalRandom.current();
for (int i = 0; i < 100; i++) {

View File

@@ -1,15 +1,15 @@
package mc.core.h2db;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.util.UUID;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
public class TestH2Player {
class TestH2Player {
@Test
public void testEquals() {
void testEquals() {
UUID uuid = UUID.randomUUID();
H2Player player1 = new H2Player();

View File

@@ -4,21 +4,21 @@ import mc.core.EntityLocation;
import mc.core.h2db.service.H2PlayerService;
import mc.core.player.Player;
import mc.core.world.World;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.List;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {TestSpringConfig.class})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class TestH2PlayerManager {
class TestH2PlayerManager {
@Autowired
private H2PlayerService h2PlayerService;
@Autowired
@@ -27,7 +27,7 @@ public class TestH2PlayerManager {
private H2PlayerManager playerManager;
@Test
public void testCreatePlayer() {
void testCreatePlayer() {
final String playerName = "NEW_PLAYER";
final Player newPlayer = playerManager.createPlayer(playerName, EntityLocation.ZERO(), mockWorld);
@@ -45,7 +45,7 @@ public class TestH2PlayerManager {
}
@Test
public void testJoinServer() {
void testJoinServer() {
assertEquals(0, playerManager.getCountPlayers());
final String playerName = "NEW_PLAYER";
@@ -57,7 +57,7 @@ public class TestH2PlayerManager {
}
@Test
public void testLeftServer() {
void testLeftServer() {
assertEquals(0, playerManager.getCountPlayers());
final String playerName = "NEW_PLAYER";
@@ -87,7 +87,7 @@ public class TestH2PlayerManager {
}
@Test
public void testGetPlayer() {
void testGetPlayer() {
assertEquals(0, playerManager.getCountPlayers());
final String playerName = "NEW_PLAYER";
@@ -104,7 +104,7 @@ public class TestH2PlayerManager {
}
@Test
public void testGetPlayers() {
void testGetPlayers() {
assertEquals(0, playerManager.getCountPlayers());
final String playerName = "NEW_PLAYER";
@@ -128,7 +128,7 @@ public class TestH2PlayerManager {
}
@Test
public void testGetCountPlayers() {
void testGetCountPlayers() {
assertEquals(0, playerManager.getCountPlayers());
final String playerName = "NEW_PLAYER";
@@ -145,7 +145,7 @@ public class TestH2PlayerManager {
}
@Test
public void testGetOfflinePlayer() {
void testGetOfflinePlayer() {
final String playerName = "NEW_PLAYER";
final Player player = playerManager.createPlayer(playerName, EntityLocation.ZERO(), mockWorld);
playerManager.joinServer(player);

View File

@@ -3,21 +3,22 @@ package mc.core.h2db.service;
import mc.core.EntityLocation;
import mc.core.h2db.H2Player;
import mc.core.h2db.TestSpringConfig;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
@RunWith(SpringJUnit4ClassRunner.class)
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {TestSpringConfig.class})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class H2PlayerServiceTest {
class H2PlayerServiceTest {
@Autowired
private H2PlayerService h2PlayerService;
@@ -43,91 +44,101 @@ public class H2PlayerServiceTest {
}
@Test
public void save() {
void save() {
H2Player player = buildPlayer();
H2Player savedPlayer = h2PlayerService.save(player);
player.setId(savedPlayer.getId()); //FIXME костыль, однако
Assert.assertEquals(player, savedPlayer);
assertEquals(player, savedPlayer);
}
@Test(expected = Exception.class)
public void save_NameEmpty() {
@Test
void save_NameEmpty() {
assertThrows(Exception.class, () -> {
H2Player player = buildPlayer();
player.setName("");
h2PlayerService.save(player);
});
}
@Test(expected = Exception.class)
public void save_NameNull() {
@Test
void save_NameNull() {
assertThrows(Exception.class, () -> {
H2Player player = buildPlayer();
player.setName(null);
h2PlayerService.save(player);
});
}
@Test(expected = Exception.class)
public void save_UuidNull() {
@Test
void save_UuidNull() {
assertThrows(Exception.class, () -> {
H2Player player = buildPlayer();
player.setUuid(null);
h2PlayerService.save(player);
});
}
@Test(expected = Exception.class)
public void save_LocationNull() {
@Test
void save_LocationNull() {
assertThrows(Exception.class, () -> {
H2Player player = buildPlayer();
player.setLocation(null);
h2PlayerService.save(player);
});
}
@Test
public void remove() {
void remove() {
H2Player player = h2PlayerService.save(buildPlayer());
h2PlayerService.remove(player);
H2Player player2 = h2PlayerService.getById(player.getId());
Assert.assertNull(player2);
}
@Test(expected = Exception.class)
public void remove_NotExists() {
H2Player player = h2PlayerService.save(buildPlayer());
h2PlayerService.remove(player);
h2PlayerService.remove(player);
assertNull(player2);
}
@Test
public void getByName() {
void remove_NotExists() {
assertThrows(Exception.class, () -> {
H2Player player = h2PlayerService.save(buildPlayer());
h2PlayerService.remove(player);
h2PlayerService.remove(player);
});
}
@Test
void getByName() {
H2Player player = h2PlayerService.save(buildPlayer());
H2Player player2 = h2PlayerService.getByName(player.getName());
Assert.assertEquals(player, player2);
assertEquals(player, player2);
}
@Test
public void getByName_NotExists() {
Assert.assertNull(h2PlayerService.getByName("UNKNOW_PLAYER"));
void getByName_NotExists() {
assertNull(h2PlayerService.getByName("UNKNOW_PLAYER"));
}
@Test
public void getByName_Empty() {
Assert.assertNull(h2PlayerService.getByName(""));
void getByName_Empty() {
assertNull(h2PlayerService.getByName(""));
}
@Test
public void getByName_Null() {
Assert.assertNull(h2PlayerService.getByName(null));
void getByName_Null() {
assertNull(h2PlayerService.getByName(null));
}
@Test
public void getById() {
void getById() {
H2Player player = h2PlayerService.save(buildPlayer());
H2Player player2 = h2PlayerService.getById(player.getId());
Assert.assertEquals(player, player2);
assertEquals(player, player2);
}
@Test
public void getById_NotExists() {
Assert.assertNull(h2PlayerService.getById(9999));
void getById_NotExists() {
assertNull(h2PlayerService.getById(9999));
}
}

View File

@@ -1,17 +1,17 @@
package mc.core.network.proto_1_12_2.packets;
import mc.core.network.proto_1_12_2.ByteArrayOutputNetStream;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.util.Random;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestByteArrayInputNetStream {
class TestByteArrayInputNetStream {
private Random rnd = new Random();
@Test
public void testReadByte() {
void testReadByte() {
final byte b0 = (byte) rnd.nextInt();
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
netStream.writeByte(b0);
@@ -23,7 +23,7 @@ public class TestByteArrayInputNetStream {
}
@Test
public void testReadInt() {
void testReadInt() {
final int i0 = rnd.nextInt();
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
netStream.writeInt(i0);
@@ -35,7 +35,7 @@ public class TestByteArrayInputNetStream {
}
@Test
public void testReadFloat() {
void testReadFloat() {
final float f0 = rnd.nextFloat();
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
netStream.writeFloat(f0);
@@ -43,6 +43,6 @@ public class TestByteArrayInputNetStream {
ByteArrayInputNetStream netInputStream = new ByteArrayInputNetStream(buffer);
float f1 = netInputStream.readFloat();
assertEquals(f0, f1, 0.0f);
assertEquals(f0, f1, 0.00001f);
}
}

View File

@@ -9,31 +9,31 @@ import mc.core.world.block.BlockFactory;
import mc.core.world.block.BlockType;
import mc.core.world.chunk.Chunk;
import mc.core.world.chunk.ChunkSection;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.InputStream;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class TestChunkdataPacket {
class TestChunkdataPacket {
private static byte[] expectedPacketData;
private World world;
@BeforeClass
public static void beforeClassTest() throws IOException {
@BeforeAll
static void beforeClassTest() throws IOException {
InputStream inputStream = TestChunkdataPacket.class.getResourceAsStream("ChunkDataPacket.bin");
expectedPacketData = ByteStreams.toByteArray(inputStream);
}
@Before
public void prepareWorld() {
@BeforeEach
void prepareWorld() {
final ChunkSection chunkSection = mock(ChunkSection.class);
when(chunkSection.getSkyLight(anyInt(), anyInt(), anyInt())).thenAnswer(invocation -> {
int y = (int)invocation.getArguments()[1];
@@ -77,7 +77,7 @@ public class TestChunkdataPacket {
}
@Test
public void test() {
void test() {
ChunkDataPacket packet = new ChunkDataPacket();
packet.setX(0);
packet.setZ(0);

View File

@@ -1,19 +1,19 @@
package mc.core.network.proto_1_12_2.packets;
import mc.core.network.proto_1_12_2.ByteArrayOutputNetStream;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Random;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestPlayerAbilitiesPacket {
class TestPlayerAbilitiesPacket {
private Random rnd = new Random();
private PlayerAbilitiesPacket packet;
@Before
public void before() {
@BeforeEach
void before() {
packet = new PlayerAbilitiesPacket();
packet.setGodMode(rnd.nextBoolean());
packet.setFlying(rnd.nextBoolean());
@@ -23,7 +23,7 @@ public class TestPlayerAbilitiesPacket {
}
@Test
public void test() {
void test() {
ByteArrayOutputNetStream netOutputStream = new ByteArrayOutputNetStream();
packet.writeSelf(netOutputStream);
@@ -31,10 +31,10 @@ public class TestPlayerAbilitiesPacket {
PlayerAbilitiesPacket outPkt = new PlayerAbilitiesPacket();
outPkt.readSelf(netInputStream);
assertEquals("god mode", packet.isGodMode(), outPkt.isGodMode());
assertEquals("flying", packet.isFlying(), outPkt.isFlying());
assertEquals("can fly", packet.isCanFly(), outPkt.isCanFly());
assertEquals("instant destroy block", packet.isInstantDestroyBlocks(), outPkt.isInstantDestroyBlocks());
assertEquals("flying speed", packet.getFlyingSpeed(), outPkt.getFlyingSpeed(), 0.0f);
assertEquals(packet.isGodMode(), outPkt.isGodMode(), "god mode");
assertEquals(packet.isFlying(), outPkt.isFlying(), "flying");
assertEquals(packet.isCanFly(), outPkt.isCanFly(), "can fly");
assertEquals(packet.isInstantDestroyBlocks(), outPkt.isInstantDestroyBlocks(), "instant destroy block");
assertEquals(packet.getFlyingSpeed(), outPkt.getFlyingSpeed(), 0.00001f, "flying speed");
}
}

View File

@@ -1,27 +1,27 @@
package mc.core.network.proto_1_12_2.serializers;
import mc.core.world.block.BlockLocation;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.concurrent.ThreadLocalRandom;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestBlockLocationSerializer {
class TestBlockLocationSerializer {
private static final ThreadLocalRandom rnd = ThreadLocalRandom.current();
private static final int minI = 0, maxI = 10;
private int x, y, z;
@Before
public void before() {
@BeforeEach
void before() {
x = rnd.nextInt(minI, maxI);
y = rnd.nextInt(minI, maxI);
z = rnd.nextInt(minI, maxI);
}
@Test
public void test() {
void test() {
BlockLocation location = new BlockLocation(x, y, z);
final long serializedCoords = BlockLocationSerializer.toLong(location);