Okay, so I wrote a method that goes through the arraylist checks if the items has the maximum value, which is ten, and returns it in a new arraylist. I got that method to work, but I am having trouble writing the test class for it. It keeps returning false.
Here is my main method,

public ArrayList allTracksWithMaxRating() {
        ArrayList<Track> maxRatingTracks = new ArrayList();
        int i = 0;
        while (i < tracks.size()) {
            if (tracks.get(i).getRating() == Track.MAXIMUM_RATING)
        return maxRatingTracks;

and here is my test class.

public void testShouldMakeArrayListWithOnlySongOne() {
        theTracks = new Playlist("mo");

		theTracks.add(new Track("song 1", 10));
		theTracks.add(new Track("song 2", 4));
		theTracks.add(new Track("song 3", 6));
		theTracks.add(new Track("song 4", 8));
		assertEquals(true, theTracks.allTracksWithMaxRating().contains("song 1"));

I have never had to write a test class that checks if an item has gone to another arraylist like this so I might just be writing it wrong.
Here is my contains method if it helps

public boolean contains(String targetTracksName) {
        if (targetTracksName == null) {
            throw new IllegalArgumentException("Must not be null");

        return (indexOf(targetTracksName) >= 0);    

Thanks in advance

6 Years
Discussion Span
Last Post by cale.macdonald

Just from looking at the code posted, I think I understand why its returning false. You are adding Tracks to the list, but you are checking if the list contains a string.

Without seeing the whole code it may be difficult to provide more insight.


This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.