简体   繁体   中英

Testing Class Methods in JUnit?

I am currently writing a program that requires some sample JUnit tests. The objective of my program is to classify Movies alphabetically according to director. I am using the Comparator class, which is going to be taking an ArrayList of Movies and sorting them alphabetically by director.

Issue 1 : cannot use the "add" method in my JUnit testing class for adding the Movies to an ArrayList , any ideas why?

Issue 2 : how can I test the DirectorComparator as a JUnit test case?

Movie.java:

import java.util.*;

public class Movie {
  String title;

  String director;

  public Movie(String title, String director) {
    this.title = title;
    this.director = director;
  }

  public String getAuthor() {
        return author;
    }

  public static Comparator<Movie> DirectorComparator = new Comparator<Movie>() {
        public int compare(Movie movie1, Movie movie2) {
            String director1 = movie1.getDirector();
            String director2 = movie2.getDirector();
            return director1.compareTo(director2);
        }
    };

MovieTest.java

 import org.junit.Test;
 import java.util.*;
 import static org.junit.Assert.*;

 public class MovieTest {

    Movie movie1("Star Wars", "Lucas, George");
    Movie movie2("ET", "Spielberg, Steven");
    Movie movie3("The Godfather", "Coppola, Francis Ford");

    ArrayList<Movie> Movies = new ArrayList<Movie>();

    Movies.add(movie1); <=== FIRST ISSUE, CANNOT USE "ADD"??

    @test
    <===== SECOND ISSUE, WHAT TYPE OF STRUCTURE TO USE FOR THE DIRECTORCOMPARATOR TEST CASE??

Any help is really appreciated here!

Firstly lets simplify a bit your comparator implementation:

public class Movie {
    private String title;
    private String director;

    public Movie(String title, String director) {
        this.title = title;
        this.director = director;
    }

    public static final Comparator<Movie> DirectorComparator =
        Comparator.comparing(movie -> movie.director);
}

Then lets write a tests for all possible cases:

public class MovieTest {
    @Test
    public void directorComparatorReturnsZeroWhenSameDirectors() {
        // given
        Movie movie1 = new Movie("Star Wars", "Lucas, George");
        Movie movie2 = new Movie("Star Wars", "Lucas, George");

        // when
        final int actualResult = Movie.DirectorComparator.compare(movie1, movie2);

        // then
        Assert.assertEquals(0, actualResult);
    }

    @Test
    public void directorComparatorReturnsNegativeWhenSecondDirectorGreater() {
        // given
        Movie movie1 = new Movie("Star Wars", "Lucas, George");
        Movie movie2 = new Movie("Star Wars", "Mucas, George");

        // when
        final int actualResult = Movie.DirectorComparator.compare(movie1, movie2);

        // then
        Assert.assertEquals(-1, actualResult);
    }

    @Test
    public void directorComparatorReturnsPositiveWhenFirstDirectorGreater() {
        // given
        Movie movie1 = new Movie("Star Wars", "Mucas, George");
        Movie movie2 = new Movie("Star Wars", "Lucas, George");

        // when
        final int actualResult = Movie.DirectorComparator.compare(movie1, movie2);

        // then
        Assert.assertEquals(1, actualResult);
    }
}

BTW, to have worked your original test you should add movies by this way:

Movie movie1 = new Movie("Star Wars", "Lucas, George");
Movie movie2 = new Movie("ET", "Spielberg, Steven");
Movie movie3 = new Movie("The Godfather", "Coppola, Francis Ford");

List<Movie> movies = new ArrayList<>();
movies.add(movie1);
movies.add(movie2);
movies.add(movie3);

But this collection is not related to DirectorComparator at all. So, it is not needed in tests of DirectorComparator .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM