Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9185253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:14:19+00:00 2026-06-17T19:14:19+00:00

I have been working on a project that has numerous UI components. Since all

  • 0

I have been working on a project that has numerous UI components. Since all the components are based on the MVC pattern, they are structured as a component – public interface and factory, package protected model/view/controller.

Testing them “by hand” – using mocking tecniques is too difficult and time consuming.

So i popped in the Fest framework – http://fest.easytesting.org/.
It’s simple, good and does the job.

The problem appears when I try to use both JMockit – http://code.google.com/p/jmockit/ and Fest together. I noticed that Fest uses some libraries that could collide with JMockit – reflection and assert.

When i run the test, JMockit doesn’t mock the required class. I used JMockit before so I’m pretty sure it’s some sort of collision between the libraries. There is no $Proxy$ generated on the mocked class, and of course, the class misbehaves.

Mocking IS required, since I have to test the full component interaction!

Versions:

JMockit:
0.999.8

Fest:

Fest-swing 1.2.1
Fest-assert 1.4
Fest-util 1.1.6
Fest-reflect 1.2

I have no intention to go conflict hunting by looking both libraries, so any help would be appretiated.

Thanks.

UPDATE:

The test/sample code is here:

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
    }
}

It seems that I blamed the framework too early. But I still don’t understand the details why it doesn’t work. The class Service IS mocked, and it should still be usable, even after delaying the expectations. I understand the time problem(initialization of the component), but have no idea how to “fix” this.

Thanks.

UPDATE2:

Thanks, Rogerio.
You can test the component with FEST, but it doesn’t really take advantage of JMockit, and there are classes that have quite a lot of methods(yes, I know – SRP, but let’s try stay on this path) and would benefit greatly from a mocking framework such as JMockit. I used this before posting the question here, so you can use it yourself, and understand that this is not the way I want to go:

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");
    }
}

The question remains – does anybody know some way I could test this with JMockit, don’t forget the EDT violation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:14:20+00:00Added an answer on June 17, 2026 at 7:14 pm

    The problem is in the test code, which happens to call SimpleServable#getValue() during window construction, before the expectation for this method call is recorded in the test.

    To fix this, simply move the new Expectation() {{ ... }} block to the onSetUp() method, putting it before the call to GuiActionRunner.execute(GuiQuery).

    The following simplified version of the code demonstrates it:

    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");
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Android library project that has been working for me pretty well,
We have a database project in Visual Studio 2008 that has been working great.
I have been working on a project that has common bits of functionality, specifically
I have been working on a project that has 2 interfaces - windows forms
Hey so I have been working on a project that I want to be
I have a django project that I have been working on as a solo
I have a console project that I have been working on. I added log4net
I have a project on github that I have been working on before. However,
I have been working through projects involving packages that do all the web design
I have been working on a project for some time, and it has branched

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.