首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JMockit和Fest测试

JMockit和Fest测试
EN

Stack Overflow用户
提问于 2013-01-17 09:28:40
回答 1查看 699关注 0票数 2

我一直在做一个有很多UI组件的项目。因为所有的组件都是基于MVC模式的,所以它们被构造成一个组件--公共接口和工厂、包保护模型/视图/控制器。

“用手”测试它们--使用嘲弄技术太困难了,也太费时。

所以我跳进了Fest框架-- http://fest.easytesting.org/。很简单,很好,做好了这份工作。

当我尝试同时使用JMockit - http://code.google.com/p/jmockit/和Fest时,问题就出现了。我注意到Fest使用了一些可能与JMockit冲突的库-反射和断言。

当我运行测试时,JMockit不模拟所需的类。我以前使用过JMockit,所以我很确定这是库之间的某种冲突。在模拟的类上没有生成$Proxy$,当然,类的行为也不正常。

模拟是必需的,因为我必须测试整个组件交互!

版本:

JMockit: 0.999.8

节日:

节-秋千1.2.1节-断言1.4节-实用1.1.6节-反映1.2

我不打算通过查找两个库来寻找冲突,所以任何帮助都会被认可。

谢谢。

更新:

测试/示例代码如下:

代码语言:javascript
复制
public interface SimpleControllable {
    void init();

    JPanel getPanel();
}

public class SimpleController implements SimpleControllable {

    private final SimpleForm simpleForm;
    private final SimpleModel simpleModel;

    public SimpleController(
            final SimpleForm simpleForm,
            final SimpleModel simpleModel
    ) {
        this.simpleForm = simpleForm;
        this.simpleModel = simpleModel;
    }

    @Override
    public void init() {
        simpleForm.init();

        //This works!
        /*simpleForm.getTestButton().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                simpleForm.getTestButton().setText(simpleModel.getSimpleValue());
            }
        });*/

        //This doesn't!
        simpleForm.getTestButton().setText(simpleModel.getSimpleValue());
    }

    @Override
    public JPanel getPanel() {
        return simpleForm.getTestPanel();
    }
}


public class SimpleModel {

    private final SimpleServable simpleServable;

    public SimpleModel(final SimpleServable simpleServable) {
        this.simpleServable = simpleServable;
    }

    public String getSimpleValue() {
        return simpleServable.getValue();
    }
}

public interface SimpleServable {
    String getValue();
}

public class SimpleService implements SimpleServable {

    private String value;

    @Override
    public String getValue() {
        return value;
    }
}

public class SimpleForm {
    private JButton testButton;
    private JPanel testPanel;

    public void init() {
        testPanel.setName("testPanel");
        testButton.setName("testButton");
    }

    public JButton getTestButton() {
        return testButton;
    }

    public JPanel getTestPanel() {
        return testPanel;
    }
}

public class SimpleTest extends FestSwingJUnitTestCase {

    @Mocked
    private SimpleService simpleServable;

    private FrameFixture window;

    @Override
    protected void onSetUp() {
        FailOnThreadViolationRepaintManager.install();

        JFrame frameUi = GuiActionRunner.execute(new GuiQuery<JFrame>() {
            protected JFrame executeInEDT() {

                SimpleControllable simpleControllable = getSimpleControllable();

                JFrame frame = new JFrame("TEST");

                frame.add(simpleControllable.getPanel());
                frame.setVisible(true);
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.pack();

                return frame;
            }
        });


        robot().settings().delayBetweenEvents(1000);

        // IMPORTANT: note the call to 'robot()'
        // we must use the Robot from FestSwingTestngTestCase

        window = new FrameFixture(robot(), frameUi);
        window.show(); // shows the frameU
    }

    //Should use factory, but not neccesary for test purposes...
    private SimpleControllable getSimpleControllable() {
        SimpleForm simpleForm = new SimpleForm();

        //SimpleServable simpleServable = new SimpleService();
        SimpleModel simpleModel = new SimpleModel(simpleServable);

        SimpleControllable simpleControllable = new SimpleController(
                simpleForm,
                simpleModel
        );

        //Initialize the component, therein lies the problem...
        simpleControllable.init();

        return simpleControllable;
    }

    @Test
    public void test() throws Exception {
        //Before
        new Expectations() {
            {
                simpleServable.getValue();
                result = "TEST";
            }
        };

        //When

        //This works!
//        window.panel("testPanel").button("testButton").click().requireText("TEST");

        //This doesn't!
        window.panel("testPanel").button("testButton").requireText("TEST");

        //Then
    }
}

看来我把责任归咎于框架太早了。但我还是不明白为什么它不起作用。类服务是被嘲弄的,即使在延迟了预期之后,它仍然应该是可用的。我理解时间问题(组件的初始化),但不知道如何“修复”这个问题。

谢谢。

UPDATE2:

谢谢罗格里奥。您可以使用FEST测试组件,但它并没有真正利用JMockit,而且有些类拥有相当多的方法(是的,我知道-- SRP,但让我们继续使用这个路径),并将从JMockit这样的模拟框架中获益良多。在这里发布问题之前,我使用了这个方法,这样您就可以自己使用它,并且理解这不是我想要的方式:

代码语言:javascript
复制
public class SimpleTest extends FestSwingJUnitTestCase {

    //This works, and I used this mechanism before, but this is without the help of JMockit.
    //If you have a lot of methods you want to mock this test turns into chaos.
    public static class MockSimpleServable implements SimpleServable{

        @Override
        public String getValue() {
            return "TEST";
        }
    }

//    @Mocked
//    private SimpleServable simpleServable;

    private FrameFixture window;

    @Override
    protected void onSetUp() {
        FailOnThreadViolationRepaintManager.install();

        JFrame frameUi = GuiActionRunner.execute(new GuiQuery<JFrame>() {
            protected JFrame executeInEDT() {

                SimpleControllable simpleControllable = getSimpleControllable();

                JFrame frame = new JFrame("TEST");

                frame.add(simpleControllable.getPanel());
                frame.setVisible(true);
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.pack();

                return frame;
            }
        });


        robot().settings().delayBetweenEvents(1000);

        // IMPORTANT: note the call to 'robot()'
        // we must use the Robot from FestSwingTestngTestCase

        window = new FrameFixture(robot(), frameUi);
        window.show(); // shows the frameU
    }

    //Should use factory, but not neccesary for test purposes...
    private SimpleControllable getSimpleControllable() {
        SimpleForm simpleForm = new SimpleForm();

        //SimpleServable simpleServable = new SimpleService();
        SimpleServable simpleServable = new MockSimpleServable();
        SimpleModel simpleModel = new SimpleModel(simpleServable);

        SimpleControllable simpleControllable = new SimpleController(
                simpleForm,
                simpleModel
        );

        //Initialize the component, therein lies the problem...
        simpleControllable.init();

        return simpleControllable;
    }

    @Test
    public void test() throws Exception {
        //This works!
//        window.panel("testPanel").button("testButton").click().requireText("TEST");

        //This doesn't!
        window.panel("testPanel").button("testButton").requireText("TEST");
    }
}

问题仍然存在--有人知道我可以用JMockit测试这个问题吗,别忘了违反了EDT。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-24 10:59:11

问题是在测试代码中,在测试中记录对该方法调用的期望之前,在窗口构造过程中调用SimpleServable#getValue()

要解决这个问题,只需将new Expectation() {{ ... }}块移动到onSetUp()方法,将其放在调用GuiActionRunner.execute(GuiQuery)之前。

代码的以下简化版本说明了这一点:

代码语言:javascript
复制
public final class SimpleForm
{
    final JPanel testPanel;
    final JButton testButton;

    public SimpleForm()
    {
        testPanel = new JPanel();
        testPanel.setName("testPanel");
        testButton = new JButton();
        testButton.setName("testButton");
        testPanel.add(testButton);
    }
}

public final class SimpleService {
    public String getValue() { return null; }
}

public final class SimpleController
{
   private final SimpleForm form;

   public SimpleController()
   {
      form = new SimpleForm();
      SimpleService service = new SimpleService();
      form.testButton.setText(service.getValue());
   }

   public JPanel getPanel() { return form.testPanel; }
}

public final class SimpleTest extends FestSwingJUnitTestCase
{
   FrameFixture window;
   @NonStrict SimpleService service;

   @Override
   protected void onSetUp()
   {
      FailOnThreadViolationRepaintManager.install();

      // Record expectations *before* they are replayed:
      new Expectations() {{ service.getValue(); result = "TEST"; }};

      JFrame frameUi = GuiActionRunner.execute(new GuiQuery<JFrame>() {
         @Override
         protected JFrame executeInEDT()
         {
            SimpleController controller = new SimpleController();

            JFrame frame = new JFrame("TEST");
            frame.add(controller.getPanel());
            frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            frame.pack();
            return frame;
         }
      });

      robot().settings().delayBetweenEvents(1000);

      window = new FrameFixture(robot(), frameUi);
      window.show();
   }

   @Test
   public void test()
   {
      // This works provided "service.getValue()" gets called *after* 
      // expectations are recorded. With the call happening during the
      // creation of the window, it must be recorded at the beginning
      // of the "onSetUp" method.
      window.panel("testPanel").button("testButton").requireText("TEST");
   }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14375854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档