Spring in Action第4章testTrackCounter未通过测试

时间:2019-07-24 10:28:17

标签: java spring spring-aop

我在练习4中学习了Spring4,我无法通过考试! 测试错误是

    java.lang.AssertionError: 
    Expected :1
    Actual   :0

我调试代码,发现计数器大小为零
debug img1
但类TrackCounter的大小不为零
debug img2
我不明白为什么测试中的计数器是{HashMap @ 2278}另一个是{HashMap @ 2294} 这是我的代码

CompactDisc:

package soundsystem;

public interface CompactDisc {
    void playTrack(int number);
}

空白光盘:

package soundsystem;


import java.util.List;

public class BlankDisc implements CompactDisc {
    private String title;
    private String artist;
    private List<String> tracks;

    public void setTitle(String title) {
        this.title = title;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

    public void setTracks(List<String> tracks) {
        this.tracks = tracks;
    }


    @Override
    public void playTrack(int trackNumber) {
        System.out.println("-Track: " + tracks.get(trackNumber));
    }
}

package soundsystem;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import java.util.HashMap;
import java.util.Map;

@Aspect
public class TrackCounter {
    private Map<Integer, Integer> trackCounts = new HashMap<Integer, Integer>();

    @Pointcut("execution(* soundsystem.CompactDisc.playTrack(int)) " +
            "&& args(trackNumber)")
    public void trackPlayed(int trackNumber) {
    }

    @Before(value = "trackPlayed(trackNumber)", argNames = "trackNumber")
    public void countTrack(int trackNumber) {
        int currentCount = getPlayCount(trackNumber);
        trackCounts.put(trackNumber, currentCount + 1);
    }

    public int getPlayCount(int trackNumber) {
        return trackCounts.getOrDefault(trackNumber, 0);
    }
}

TrackCountConfig:

package soundsystem;

import org.springframework.context.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableAspectJAutoProxy
public class TrackCountConfig {
    @Bean
    @Primary
    public CompactDisc sgtPeppers() {
        BlankDisc cd = new BlankDisc();
        cd.setTitle("Sgt. Pepper's Lonely Hearts Club Band");
        cd.setArtist("The Beatles");
        List<String> tracks = new ArrayList<String>();
        tracks.add("Sgt. Pepper's Lonely Hearts Club Band");
        tracks.add("With A Little Help From My Friends");
        tracks.add("Lucy In The Sky With Diamonds");
        tracks.add("Getting Better");
        tracks.add("Fixing A Hole");
        tracks.add("She's Leaving Home");
        tracks.add("Being For The Benefit Of Mr. Kite!");
        tracks.add("Within You Without You");
        tracks.add("When I'm Sixty Four");
        tracks.add("Lovely Rita");
        tracks.add("Good Morning Good Morning");
        cd.setTracks(tracks);
        return cd;
    }

    @Bean
    public TrackCounter trackCounter() {
        return new TrackCounter();
    }
}
package soundsystem;


import org.junit.Test;

import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.assertEquals;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TrackCountConfig.class)
public class TrackCounterTest {
    @Autowired
    private CompactDisc cd;
    @Autowired
    private TrackCounter counter;

    @Test
    public void testTrackCounter() {
        cd.playTrack(1);
        cd.playTrack(2);
        cd.playTrack(2);
        cd.playTrack(2);
        cd.playTrack(3);
        cd.playTrack(3);
        cd.playTrack(3);
        cd.playTrack(7);
        cd.playTrack(7);
        assertEquals(1, counter.getPlayCount(1));
    }
}

1 个答案:

答案 0 :(得分:0)

我在WWW上搜索了该书的源代码,因为您的问题缺少两个类。我找到了他们:

package soundsystem;

public interface CompactDisc {
  void play();
  void playTrack(int track);
}
package soundsystem;

import java.util.List;

public class BlankDisc implements CompactDisc {
  private String title;
  private String artist;
  private List<String> tracks;

  public void setTitle(String title) {
    this.title = title;
  }

  public void setArtist(String artist) {
    this.artist = artist;
  }

  public void setTracks(List<String> tracks) {
    this.tracks = tracks;
  }

  public void play() {
    System.out.println("Playing " + title + " by " + artist);
    for (int i = 0; i < tracks.size(); i++) {
      playTrack(i);
    }
  }

  public void playTrack(int track) {
    System.out.println("-Track: " + tracks.get(track));
  }
}

使用这两个类以及您提供的测试合格。如果您没有通过,则BlankDisc的实现可能会有所不同。