Search code examples
javaswingassertj

Get JSlider value in tests using AssertJ


I use JUnit and AssertJ tests for my Swing app written in Java 8. I'd like to test whether slider value is changed when a button is clicked, but I didn't find any way to get a current value from a slider.

I've initialized the app at the beginning of the test

Vizar frame = GuiActionRunner.execute(() -> new Vizar());
window = new FrameFixture(frame);
window.show();

into window variable. I would like to have something like this

int a = window.slider("zoomSlider").getValue();

Is there any way to do that?


Solution

  • In AssertJ-Swing, all the fixtures that you can retrieve via JFrameFixture have a method target() that retrieves the target component.

    For your problem, this means that you will have to write

    int a = window.slider("zoomSlider").target().getValue();