繁体   English   中英

在 JUnit 中测试类方法?

[英]Testing Class Methods in JUnit?

我目前正在编写一个需要一些示例 JUnit 测试的程序。 我的程序的目标是根据导演按字母顺序对电影进行分类。 我正在使用Comparator类,它将采用MoviesArrayList并按导演的字母顺序对它们进行排序。

问题 1 :无法在我的JUnit测试类中使用“add”方法将Movies添加到ArrayList ,任何想法为什么?

问题 2 :如何将DirectorComparator作为JUnit测试用例进行测试?

电影.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);
        }
    };

电影测试程序

 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??

任何帮助在这里真的很感激!

首先让我们简化一下您的比较器实现:

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);
}

然后让我们为所有可能的情况编写一个测试:

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);
    }
}

顺便说一句,要完成您的原始测试,您应该通过以下方式添加电影:

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);

但是这个集合与DirectorComparator完全没有关系。 因此,在DirectorComparator测试中不需要它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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