1. Moved commmon code to @Before method in ComparatorUnitTest.
2. Changed ArrayList to List for declarations
This commit is contained in:
parent
5b8fb97494
commit
d954258a80
|
@ -2,12 +2,13 @@ package com.baeldung.comparable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class PlayerSorter {
|
public class PlayerSorter {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
ArrayList<Player> footballTeam = new ArrayList<Player>();
|
List<Player> footballTeam = new ArrayList<Player>();
|
||||||
Player player1 = new Player(59, "John", 20);
|
Player player1 = new Player(59, "John", 20);
|
||||||
Player player2 = new Player(67, "Roger", 22);
|
Player player2 = new Player(67, "Roger", 22);
|
||||||
Player player3 = new Player(45, "Steven", 24);
|
Player player3 = new Player(45, "Steven", 24);
|
||||||
|
|
|
@ -2,12 +2,13 @@ package com.baeldung.comparator;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class PlayerAgeSorter {
|
public class PlayerAgeSorter {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
ArrayList<Player> footballTeam = new ArrayList<Player>();
|
List<Player> footballTeam = new ArrayList<Player>();
|
||||||
Player player1 = new Player(59, "John", 22);
|
Player player1 = new Player(59, "John", 22);
|
||||||
Player player2 = new Player(67, "Roger", 20);
|
Player player2 = new Player(67, "Roger", 20);
|
||||||
Player player3 = new Player(45, "Steven", 24);
|
Player player3 = new Player(45, "Steven", 24);
|
||||||
|
|
|
@ -2,12 +2,13 @@ package com.baeldung.comparator;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class PlayerRankingSorter {
|
public class PlayerRankingSorter {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
ArrayList<Player> footballTeam = new ArrayList<Player>();
|
List<Player> footballTeam = new ArrayList<Player>();
|
||||||
Player player1 = new Player(59, "John", 22);
|
Player player1 = new Player(59, "John", 22);
|
||||||
Player player2 = new Player(67, "Roger", 20);
|
Player player2 = new Player(67, "Roger", 20);
|
||||||
Player player3 = new Player(45, "Steven", 40);
|
Player player3 = new Player(45, "Steven", 40);
|
||||||
|
|
|
@ -4,6 +4,7 @@ import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
@ -11,7 +12,7 @@ public class ComparableUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingComparable_thenSortedList() {
|
public void whenUsingComparable_thenSortedList() {
|
||||||
ArrayList<Player> footballTeam = new ArrayList<Player>();
|
List<Player> footballTeam = new ArrayList<Player>();
|
||||||
Player player1 = new Player(59, "John", 20);
|
Player player1 = new Player(59, "John", 20);
|
||||||
Player player2 = new Player(67, "Roger", 22);
|
Player player2 = new Player(67, "Roger", 22);
|
||||||
Player player3 = new Player(45, "Steven", 24);
|
Player player3 = new Player(45, "Steven", 24);
|
||||||
|
|
|
@ -4,20 +4,28 @@ import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class ComparatorUnitTest {
|
public class ComparatorUnitTest {
|
||||||
|
|
||||||
@Test
|
List<Player> footballTeam;
|
||||||
public void whenUsingRankingComparator_thenSortedList() {
|
|
||||||
ArrayList<Player> footballTeam = new ArrayList<Player>();
|
@Before
|
||||||
|
public void setUp(){
|
||||||
|
footballTeam = new ArrayList<Player>();
|
||||||
Player player1 = new Player(59, "John", 20);
|
Player player1 = new Player(59, "John", 20);
|
||||||
Player player2 = new Player(67, "Roger", 22);
|
Player player2 = new Player(67, "Roger", 22);
|
||||||
Player player3 = new Player(45, "Steven", 24);
|
Player player3 = new Player(45, "Steven", 24);
|
||||||
footballTeam.add(player1);
|
footballTeam.add(player1);
|
||||||
footballTeam.add(player2);
|
footballTeam.add(player2);
|
||||||
footballTeam.add(player3);
|
footballTeam.add(player3);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingRankingComparator_thenSortedList() {
|
||||||
PlayerRankingComparator playerComparator = new PlayerRankingComparator();
|
PlayerRankingComparator playerComparator = new PlayerRankingComparator();
|
||||||
Collections.sort(footballTeam, playerComparator);
|
Collections.sort(footballTeam, playerComparator);
|
||||||
assertEquals(footballTeam.get(0).getName(), "Steven");
|
assertEquals(footballTeam.get(0).getName(), "Steven");
|
||||||
|
@ -26,13 +34,6 @@ public class ComparatorUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingAgeComparator_thenSortedList() {
|
public void whenUsingAgeComparator_thenSortedList() {
|
||||||
ArrayList<Player> footballTeam = new ArrayList<Player>();
|
|
||||||
Player player1 = new Player(59, "John", 20);
|
|
||||||
Player player2 = new Player(67, "Roger", 22);
|
|
||||||
Player player3 = new Player(45, "Steven", 24);
|
|
||||||
footballTeam.add(player1);
|
|
||||||
footballTeam.add(player2);
|
|
||||||
footballTeam.add(player3);
|
|
||||||
PlayerAgeComparator playerComparator = new PlayerAgeComparator();
|
PlayerAgeComparator playerComparator = new PlayerAgeComparator();
|
||||||
Collections.sort(footballTeam, playerComparator);
|
Collections.sort(footballTeam, playerComparator);
|
||||||
assertEquals(footballTeam.get(0).getName(), "John");
|
assertEquals(footballTeam.get(0).getName(), "John");
|
||||||
|
|
Loading…
Reference in New Issue