方面测试 - 无法获得方面 bean 的正确值 属性

Aspect testing - can't get right value of aspects bean's property

看点

@Configurable
@Aspect
public class FilmCounter {

    private Map<Integer, Integer> filmCounts = 
            new HashMap<Integer, Integer>();

    @Pointcut(
        "execution(* soundsystem.CompactDisc.play(int)) " +
        "&& args(filmNumber)" )
    public void filmPlayed(int filmNumber) {}

    @Before("filmPlayed(filmNumber)")
    public void countFilm(int filmNumber){      
        int currentCount = getPlayCount(filmNumber);
        filmCounts.put(filmNumber, currentCount + 1);

        System.out.println("filmNumber " + filmNumber + " contains in ? " + filmCounts.containsKey(filmNumber) + " then filmCount = " + filmCounts.get(filmNumber));

    }

    public int getPlayCount(int filmNumber){
        //System.out.println("filmNumber " + filmNumber + " contains in ? " + filmCounts.containsKey(filmNumber) + "");

        return filmCounts.containsKey(filmNumber)
                ? filmCounts.get(filmNumber) : 0;

    }
}

配置class

在 AspectJ 运行库下所以不需要 @EnableAspectJAutoProxy 注释

@Configuration
public class FilmCounterConfig {

    @Bean
    public CompactDisc filmDisk(){
        FilmDisc cd = new FilmDisc();
        cd.setTitle("1981's Melody Films");
        cd.setArtist("Kishor Kumar");
        List<String> films = new ArrayList<String>();
        films.add("Sanam Teri Kasam");
        films.add("Akhari Rasta");
        films.add("Hum Apke Hai Koun");
        films.add("Dil Hain Ke Manata Nahi");
        films.add("Hum Sath Sath Hain");
        cd.setFilms(films);

        return cd;
    }   

    @Bean
    public FilmCounter filmCounter(){
        return new FilmCounter();
    }
}

测试class

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=FilmCounterConfig.class)
public class FilmCounterTest {

    @Rule
    public final SystemOutRule systemOutRule = new SystemOutRule().enableLog(); 

    @Autowired
    private CompactDisc cd;

    @Autowired
    private FilmCounter counter;

    @Test
    public void testFilmCounter(){

        cd.play(1); System.out.println("counter.getPlayCount(1) = " + counter.getPlayCount(1));
        cd.play(2);
        cd.play(3);
        cd.play(3);
        cd.play(3);
        cd.play(3);     

        /*assertEquals(1, counter.getPlayCount(1));
        assertEquals(1, counter.getPlayCount(2));
        assertEquals(4, counter.getPlayCount(3));
        */

        System.out.println("Film 1 = " + counter.getPlayCount(1));
        System.out.println("Film 2 = " + counter.getPlayCount(2));
        System.out.println("Film 3 = " + counter.getPlayCount(3));

    }

}

当运行上面的测试用例时,我得到以下输出:

图像显示第一个方法调用 cd.play(2); 的控制台日志输出看起来正常,但在调用 System.out.println("Film 1 = " + counter.getPlayCount(1)); 后显示错误结果(应该显示“4”但显示“0” ").

我想我知道发生了什么,查看您的控制台日志:有一个活动的方面实例记录您的方法调用,但可能与连接到您的测试中的实例不同。这将解释方面日志输出与测试日志输出之间的差异。

如果您将 System.out.println(this); 添加到 countFilm(int) 建议并将 System.out.println(counter); 添加到测试用例,我猜您可能会看到两个不同的对象 ID。

如果我是对的,那么你的 bean 接线有问题。请在评论中提供反馈。