EasyMock en environnement multithread
Par Masclet le dimanche 28 novembre 2010, 13:39 - Tests / XUnit - Lien permanent
Lorsque vous voulez 'mocker' un objet et que ce dernier sera appelé par plusieurs threads, vous aurez le message suivant "Un-thread-safe mock called from multiple threads".
Par exemple :
@test
public void MyTest(){
List mockList = EasyMock.createMock(List.class);
EasyMock.expect(mockList.add(EasyMock.anyObject())).times(NUMBER_OF_THREAD);
EasyMock.replay(mockList);
MyThread[] rateThread= new MyThread[NUMBER_OF_THREAD];
for(int i=0;i<NUMBER_OF_THREAD;i++){
rateThread[i]= new MyThread(){
@Override
doit(){
mockList.add("foo");
}
};
rateThread[i].start();
}
}
Il vous faut alors ajouter EasyMock.makeThreadSafe(mockList,
true);. avant le replay(mockList)
Commentaires
I will not abandon you*_*
It's not at all simplistic just to continually be handing out solutions that many the rest might have been selling.