Aspect测试 - 无法获得aspect bean属性的正确值

时间:2017-03-05 12:26:24

标签: spring aspectj

方面

@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;

    }
}

配置类

在AspectJ运行时lib下,不需要@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();
    }
}

测试类

@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));

    }

}

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

Console log

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

1 个答案:

答案 0 :(得分:0)

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

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

如果我是对的,你的豆子布线有问题。请在评论中提供反馈。