Question: how to JUnit test an ArrayList? I have to code 3 JUnit test for a class My code for the class I need to test

how to JUnit test an ArrayList?

I have to code 3 JUnit test for a class

My code for the class I need to test

package test.lab3.model; /* This class stores data about a song. */ public class Song { private String title; // The song's title private String artist; // The song's artist /** * Constructor * * @param title A reference to a String object containing the song's title. * @param artist A reference to a String object containing the song's artist. */ public Song(String title, String artist) { this.title = title; this.artist = artist; } // Task #2 - add setter and getters for title and artist here public String getArtist() { return artist; } public String getTitle() { return title; } public void setArtist(String a) { artist = a; } public void setTitle(String t) { this.title = t; } /** * The toString method * * @return A String object containing the name of the song and the artist. */ public String toString() { return title + " by " + artist + " "; } } 

My code for the test class:

package test.lab3.test; import static org.junit.Assert.assertEquals; import org.junit.jupiter.api.Test; import test.lab3.model.Song; class TestSong { // Task #5 Write 3 tests here! Song test = new Song(null, null); @Test void test() { } } 

One of the issue I have is for the test class I cannot create an object from the Song class (posted above). I currently have null in the parameters of the code because I get an error without them.

I have the songs with the artist in a .txt file. The main method and loadSongs(); method is in a different class.

I'm struggling with:

  1. How do I create an object from the song class to the test class without errors?

  2. How do I use JUnit testing for an Arraylist?

Thank you!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!