JUnit4执行过程中,org.junit.runner.notification. RunListener和RunNotifier运用了观察者模式。
1.观察者
观察者Observer/Listener主要作用是分析各种事件并定义相应的回调接口。例如JDK中MouseListener处理鼠标键相关的5个动作:鼠标键被按下/pressed、释放/released、单击/clicked、光标进入或离开某组件/enters or exits。java.awt.event .MouseListener的源代码:
public interface MouseListener extendsEventListener {
publicvoid mouseClicked(MouseEvent e);
publicvoid mousePressed(MouseEvent e);
publicvoid mouseReleased(MouseEvent e);
publicvoid mouseEntered(MouseEvent e);
publicvoid mouseExited(MouseEvent e);
}
那么,RunListener处理测试运行的7个动作:
1. publicvoid testRunStarted(Description description)
在所有测试将要运行前的动作。如同运动会比赛前召开开幕式一样。
2. public void testStarted(Description