无法在单一单元测试-Spring Boot中使用Maven的spring-boot-starter-test方法

时间:2018-10-07 21:34:13

标签: java spring maven spring-boot

我是Spring Boot的初学者,我正在尝试运行单元测试,我使用maven进行程序包管理,但是它无法导入相应的程序包,这会导致一些错误,例如,我无法使用来自org的注释.springframework.boot.test。谁能告诉我如何解决这个问题?谢谢!

enter image description here

spring-boot-starter-test的颜色为灰色。 enter image description here

下面是我的pom.xml

public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> {

private static final String TAG = MovieAdapter.class.getSimpleName();
private List<Movie> movies;
private Context context;

public MovieAdapter(Context context) {
    this.movies = new ArrayList<>();
    this.context = context;
}

public MovieAdapter(List<Movie> movies,  Context context) {
    this.movies = movies;
    this.context = context;
}

public List<Movie> getMovies() {
    return movies;
}

public void setMovies(List<Movie> movies) {
    this.movies.addAll(movies);
    Log.d(TAG, this.movies.size() + "");
}

public void addMovies(List<Movie> movies) {
    this.movies.addAll(movies);
    Log.d(TAG, this.movies.size() + "");
}

@NonNull
@Override
public MovieAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.movie_list_item, viewGroup, false);
    ViewHolder holder = new ViewHolder(view);
    return holder;
    /*
    MovieListItemBinding binding = DataBindingUtil
            .inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.movie_list_item, viewGroup, false);
    return new ViewHolder(binding);
    */
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
    final Movie movie = movies.get(i);
    // viewHolder.movieListItemBinding.setMovie(movie);
    viewHolder.textView.setText(movie.getTitle());
    viewHolder.textView2.setText(movie.getGenre());
    viewHolder.textView3.setText(movie.getReleaseDate());
    viewHolder.textView4.setText(movie.getRating());
    Picasso.get().load(movie.getImagePath()).into(viewHolder.imageView);
    viewHolder.parentLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle(movie.getTitle());
            builder.setMessage(movie.getOverview());
            builder.setPositiveButton("OK", null);
            builder.show();
        }
    });
}

@Override
public int getItemCount() {
    return movies.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    // Binding variables
    public MovieListItemBinding movieListItemBinding;
    TextView textView;
    TextView textView2;
    TextView textView3;
    TextView textView4;
    ImageView imageView;
    RelativeLayout parentLayout;

    // Constructor to do view lookups for each subview
    /*
    public ViewHolder(MovieListItemBinding movieLayoutBinding) {
        super(movieLayoutBinding.getRoot());
        movieListItemBinding = movieLayoutBinding;
    }
    */

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        textView = itemView.findViewById(R.id.textView);
        textView2 = itemView.findViewById(R.id.textView2);
        textView3 = itemView.findViewById(R.id.textView3);
        textView4 = itemView.findViewById(R.id.textView4);
        imageView = itemView.findViewById(R.id.imageView);
        parentLayout = itemView.findViewById(R.id.movieListView);
    }
}

2 个答案:

答案 0 :(得分:2)

如果您正在使用eclipse通过maven项目开发spring boot应用程序。 运行单元测试时,应在src / test / java中创建测试文件 enter image description here

答案 1 :(得分:0)

您可以尝试清除本地Maven存储库,然后运行maven命令。