JUnit 5 JavaFX Multiple ChangeListeners on IntegerProperty not working as intended recursively?
up vote
-1
down vote
favorite
I'm using JUnit 5 and below I have two ChangeListeners
on an IntegerProperty
which one of them recursively changes the value of the property.
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
public class FakeTest {
@Test
void testChangeListener() {
IntegerProperty test = new SimpleIntegerProperty(0);
test.addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldVal, Number newVal) {
System.out.println("In first change listener: " + oldVal + " and " + newVal);
if (newVal.intValue() < 5) {
test.setValue(test.getValue() + 1);
}
}
});
test.addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldVal, Number newVal) {
// assertThat(newVal, is(oldVal.intValue() + 1));
System.out.println("In second change listener: " + oldVal + " and " + newVal);
}
});
test.setValue(1);
}
}
I want to be able to assertThat(newVal, is(oldVal.intValue() + 1));
but for some reason the output is:
In first change listener: 0 and 1
In first change listener: 1 and 2
In first change listener: 2 and 3
In first change listener: 3 and 4
In first change listener: 4 and 5
In second change listener: 4 and 5
In second change listener: 3 and 5
In second change listener: 2 and 5
In second change listener: 1 and 5
In second change listener: 0 and 5
But I expect it to output:
In first change listener: 0 and 1
In first change listener: 1 and 2
In first change listener: 2 and 3
In first change listener: 3 and 4
In first change listener: 4 and 5
In second change listener: 0 and 1
In second change listener: 1 and 2
In second change listener: 2 and 3
In second change listener: 3 and 4
In second change listener: 4 and 5
I've done some research and I found ReactFX Var
according to http://tomasmikula.github.io/blog/2015/02/10/val-a-better-observablevalue.html which says to fix the behaviour on recursive changes, but I'm not getting the desired output.
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import org.junit.jupiter.api.Test;
import org.reactfx.value.Var;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
public class FakeTest {
@Test
void testChangeListener() {
Var<Integer> test = Var.newSimpleVar(0);
test.addListener(new ChangeListener<Integer>() {
@Override
public void changed(ObservableValue<? extends Integer> observableValue, Integer oldVal, Integer newVal) {
System.out.println("In first change listener: " + oldVal + " and " + newVal);
if (newVal < 5) {
test.setValue(test.getValue() + 1);
}
}
});
test.addListener(new ChangeListener<Integer>() {
@Override
public void changed(ObservableValue<? extends Integer> observableValue, Integer oldVal, Integer newVal) {
// assertThat(newVal, is(oldVal + 1));
System.out.println("In second change listener: " + oldVal + " and " + newVal);
}
});
test.setValue(1);
}
}
Interestingly, I'm using IntelliJ and OpenJDK 11, but when I run the testChangeListener
directly, I get the following output:
In first change listener: 0 and 1
In first change listener: 1 and 2
In first change listener: 2 and 3
In first change listener: 3 and 4
In first change listener: 4 and 5
In second change listener: 0 and 5
But when I click Run FakeTest
(ie the whole test suite) I get:
In first change listener: 0 and 1
In second change listener: 0 and 2
In first change listener: 1 and 2
In second change listener: 2 and 3
In first change listener: 2 and 3
In second change listener: 3 and 4
In first change listener: 3 and 4
In second change listener: 4 and 5
In first change listener: 4 and 5
This is close to what I want, but again, I want something like:
In first change listener: 0 and 1
In second change listener: 0 and 1
In first change listener: 1 and 2
In second change listener: 1 and 2
In first change listener: 2 and 3
In second change listener: 2 and 3
In first change listener: 3 and 4
In second change listener: 3 and 4
In first change listener: 4 and 5
In second change listener: 4 and 5
- How can I get the ChangeListeners to my expected output?
- Why does the output change for
Var
when I choose to run the one test compared to the whole test suite? If I use IntegerProperty, I get the same output regardless of running the one test or the whole test suite.
recursion javafx junit changelistener
add a comment |
up vote
-1
down vote
favorite
I'm using JUnit 5 and below I have two ChangeListeners
on an IntegerProperty
which one of them recursively changes the value of the property.
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
public class FakeTest {
@Test
void testChangeListener() {
IntegerProperty test = new SimpleIntegerProperty(0);
test.addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldVal, Number newVal) {
System.out.println("In first change listener: " + oldVal + " and " + newVal);
if (newVal.intValue() < 5) {
test.setValue(test.getValue() + 1);
}
}
});
test.addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldVal, Number newVal) {
// assertThat(newVal, is(oldVal.intValue() + 1));
System.out.println("In second change listener: " + oldVal + " and " + newVal);
}
});
test.setValue(1);
}
}
I want to be able to assertThat(newVal, is(oldVal.intValue() + 1));
but for some reason the output is:
In first change listener: 0 and 1
In first change listener: 1 and 2
In first change listener: 2 and 3
In first change listener: 3 and 4
In first change listener: 4 and 5
In second change listener: 4 and 5
In second change listener: 3 and 5
In second change listener: 2 and 5
In second change listener: 1 and 5
In second change listener: 0 and 5
But I expect it to output:
In first change listener: 0 and 1
In first change listener: 1 and 2
In first change listener: 2 and 3
In first change listener: 3 and 4
In first change listener: 4 and 5
In second change listener: 0 and 1
In second change listener: 1 and 2
In second change listener: 2 and 3
In second change listener: 3 and 4
In second change listener: 4 and 5
I've done some research and I found ReactFX Var
according to http://tomasmikula.github.io/blog/2015/02/10/val-a-better-observablevalue.html which says to fix the behaviour on recursive changes, but I'm not getting the desired output.
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import org.junit.jupiter.api.Test;
import org.reactfx.value.Var;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
public class FakeTest {
@Test
void testChangeListener() {
Var<Integer> test = Var.newSimpleVar(0);
test.addListener(new ChangeListener<Integer>() {
@Override
public void changed(ObservableValue<? extends Integer> observableValue, Integer oldVal, Integer newVal) {
System.out.println("In first change listener: " + oldVal + " and " + newVal);
if (newVal < 5) {
test.setValue(test.getValue() + 1);
}
}
});
test.addListener(new ChangeListener<Integer>() {
@Override
public void changed(ObservableValue<? extends Integer> observableValue, Integer oldVal, Integer newVal) {
// assertThat(newVal, is(oldVal + 1));
System.out.println("In second change listener: " + oldVal + " and " + newVal);
}
});
test.setValue(1);
}
}
Interestingly, I'm using IntelliJ and OpenJDK 11, but when I run the testChangeListener
directly, I get the following output:
In first change listener: 0 and 1
In first change listener: 1 and 2
In first change listener: 2 and 3
In first change listener: 3 and 4
In first change listener: 4 and 5
In second change listener: 0 and 5
But when I click Run FakeTest
(ie the whole test suite) I get:
In first change listener: 0 and 1
In second change listener: 0 and 2
In first change listener: 1 and 2
In second change listener: 2 and 3
In first change listener: 2 and 3
In second change listener: 3 and 4
In first change listener: 3 and 4
In second change listener: 4 and 5
In first change listener: 4 and 5
This is close to what I want, but again, I want something like:
In first change listener: 0 and 1
In second change listener: 0 and 1
In first change listener: 1 and 2
In second change listener: 1 and 2
In first change listener: 2 and 3
In second change listener: 2 and 3
In first change listener: 3 and 4
In second change listener: 3 and 4
In first change listener: 4 and 5
In second change listener: 4 and 5
- How can I get the ChangeListeners to my expected output?
- Why does the output change for
Var
when I choose to run the one test compared to the whole test suite? If I use IntegerProperty, I get the same output regardless of running the one test or the whole test suite.
recursion javafx junit changelistener
1
Do not do updates fromChangeListener
s. The javadoc forChangeListener.change
warns you: "In general, it is considered bad practice to modify the observed value in this method."
– fabian
Nov 11 at 22:59
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I'm using JUnit 5 and below I have two ChangeListeners
on an IntegerProperty
which one of them recursively changes the value of the property.
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
public class FakeTest {
@Test
void testChangeListener() {
IntegerProperty test = new SimpleIntegerProperty(0);
test.addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldVal, Number newVal) {
System.out.println("In first change listener: " + oldVal + " and " + newVal);
if (newVal.intValue() < 5) {
test.setValue(test.getValue() + 1);
}
}
});
test.addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldVal, Number newVal) {
// assertThat(newVal, is(oldVal.intValue() + 1));
System.out.println("In second change listener: " + oldVal + " and " + newVal);
}
});
test.setValue(1);
}
}
I want to be able to assertThat(newVal, is(oldVal.intValue() + 1));
but for some reason the output is:
In first change listener: 0 and 1
In first change listener: 1 and 2
In first change listener: 2 and 3
In first change listener: 3 and 4
In first change listener: 4 and 5
In second change listener: 4 and 5
In second change listener: 3 and 5
In second change listener: 2 and 5
In second change listener: 1 and 5
In second change listener: 0 and 5
But I expect it to output:
In first change listener: 0 and 1
In first change listener: 1 and 2
In first change listener: 2 and 3
In first change listener: 3 and 4
In first change listener: 4 and 5
In second change listener: 0 and 1
In second change listener: 1 and 2
In second change listener: 2 and 3
In second change listener: 3 and 4
In second change listener: 4 and 5
I've done some research and I found ReactFX Var
according to http://tomasmikula.github.io/blog/2015/02/10/val-a-better-observablevalue.html which says to fix the behaviour on recursive changes, but I'm not getting the desired output.
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import org.junit.jupiter.api.Test;
import org.reactfx.value.Var;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
public class FakeTest {
@Test
void testChangeListener() {
Var<Integer> test = Var.newSimpleVar(0);
test.addListener(new ChangeListener<Integer>() {
@Override
public void changed(ObservableValue<? extends Integer> observableValue, Integer oldVal, Integer newVal) {
System.out.println("In first change listener: " + oldVal + " and " + newVal);
if (newVal < 5) {
test.setValue(test.getValue() + 1);
}
}
});
test.addListener(new ChangeListener<Integer>() {
@Override
public void changed(ObservableValue<? extends Integer> observableValue, Integer oldVal, Integer newVal) {
// assertThat(newVal, is(oldVal + 1));
System.out.println("In second change listener: " + oldVal + " and " + newVal);
}
});
test.setValue(1);
}
}
Interestingly, I'm using IntelliJ and OpenJDK 11, but when I run the testChangeListener
directly, I get the following output:
In first change listener: 0 and 1
In first change listener: 1 and 2
In first change listener: 2 and 3
In first change listener: 3 and 4
In first change listener: 4 and 5
In second change listener: 0 and 5
But when I click Run FakeTest
(ie the whole test suite) I get:
In first change listener: 0 and 1
In second change listener: 0 and 2
In first change listener: 1 and 2
In second change listener: 2 and 3
In first change listener: 2 and 3
In second change listener: 3 and 4
In first change listener: 3 and 4
In second change listener: 4 and 5
In first change listener: 4 and 5
This is close to what I want, but again, I want something like:
In first change listener: 0 and 1
In second change listener: 0 and 1
In first change listener: 1 and 2
In second change listener: 1 and 2
In first change listener: 2 and 3
In second change listener: 2 and 3
In first change listener: 3 and 4
In second change listener: 3 and 4
In first change listener: 4 and 5
In second change listener: 4 and 5
- How can I get the ChangeListeners to my expected output?
- Why does the output change for
Var
when I choose to run the one test compared to the whole test suite? If I use IntegerProperty, I get the same output regardless of running the one test or the whole test suite.
recursion javafx junit changelistener
I'm using JUnit 5 and below I have two ChangeListeners
on an IntegerProperty
which one of them recursively changes the value of the property.
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
public class FakeTest {
@Test
void testChangeListener() {
IntegerProperty test = new SimpleIntegerProperty(0);
test.addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldVal, Number newVal) {
System.out.println("In first change listener: " + oldVal + " and " + newVal);
if (newVal.intValue() < 5) {
test.setValue(test.getValue() + 1);
}
}
});
test.addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldVal, Number newVal) {
// assertThat(newVal, is(oldVal.intValue() + 1));
System.out.println("In second change listener: " + oldVal + " and " + newVal);
}
});
test.setValue(1);
}
}
I want to be able to assertThat(newVal, is(oldVal.intValue() + 1));
but for some reason the output is:
In first change listener: 0 and 1
In first change listener: 1 and 2
In first change listener: 2 and 3
In first change listener: 3 and 4
In first change listener: 4 and 5
In second change listener: 4 and 5
In second change listener: 3 and 5
In second change listener: 2 and 5
In second change listener: 1 and 5
In second change listener: 0 and 5
But I expect it to output:
In first change listener: 0 and 1
In first change listener: 1 and 2
In first change listener: 2 and 3
In first change listener: 3 and 4
In first change listener: 4 and 5
In second change listener: 0 and 1
In second change listener: 1 and 2
In second change listener: 2 and 3
In second change listener: 3 and 4
In second change listener: 4 and 5
I've done some research and I found ReactFX Var
according to http://tomasmikula.github.io/blog/2015/02/10/val-a-better-observablevalue.html which says to fix the behaviour on recursive changes, but I'm not getting the desired output.
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import org.junit.jupiter.api.Test;
import org.reactfx.value.Var;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
public class FakeTest {
@Test
void testChangeListener() {
Var<Integer> test = Var.newSimpleVar(0);
test.addListener(new ChangeListener<Integer>() {
@Override
public void changed(ObservableValue<? extends Integer> observableValue, Integer oldVal, Integer newVal) {
System.out.println("In first change listener: " + oldVal + " and " + newVal);
if (newVal < 5) {
test.setValue(test.getValue() + 1);
}
}
});
test.addListener(new ChangeListener<Integer>() {
@Override
public void changed(ObservableValue<? extends Integer> observableValue, Integer oldVal, Integer newVal) {
// assertThat(newVal, is(oldVal + 1));
System.out.println("In second change listener: " + oldVal + " and " + newVal);
}
});
test.setValue(1);
}
}
Interestingly, I'm using IntelliJ and OpenJDK 11, but when I run the testChangeListener
directly, I get the following output:
In first change listener: 0 and 1
In first change listener: 1 and 2
In first change listener: 2 and 3
In first change listener: 3 and 4
In first change listener: 4 and 5
In second change listener: 0 and 5
But when I click Run FakeTest
(ie the whole test suite) I get:
In first change listener: 0 and 1
In second change listener: 0 and 2
In first change listener: 1 and 2
In second change listener: 2 and 3
In first change listener: 2 and 3
In second change listener: 3 and 4
In first change listener: 3 and 4
In second change listener: 4 and 5
In first change listener: 4 and 5
This is close to what I want, but again, I want something like:
In first change listener: 0 and 1
In second change listener: 0 and 1
In first change listener: 1 and 2
In second change listener: 1 and 2
In first change listener: 2 and 3
In second change listener: 2 and 3
In first change listener: 3 and 4
In second change listener: 3 and 4
In first change listener: 4 and 5
In second change listener: 4 and 5
- How can I get the ChangeListeners to my expected output?
- Why does the output change for
Var
when I choose to run the one test compared to the whole test suite? If I use IntegerProperty, I get the same output regardless of running the one test or the whole test suite.
recursion javafx junit changelistener
recursion javafx junit changelistener
asked Nov 11 at 20:53
mepmerp
3251511
3251511
1
Do not do updates fromChangeListener
s. The javadoc forChangeListener.change
warns you: "In general, it is considered bad practice to modify the observed value in this method."
– fabian
Nov 11 at 22:59
add a comment |
1
Do not do updates fromChangeListener
s. The javadoc forChangeListener.change
warns you: "In general, it is considered bad practice to modify the observed value in this method."
– fabian
Nov 11 at 22:59
1
1
Do not do updates from
ChangeListener
s. The javadoc for ChangeListener.change
warns you: "In general, it is considered bad practice to modify the observed value in this method."– fabian
Nov 11 at 22:59
Do not do updates from
ChangeListener
s. The javadoc for ChangeListener.change
warns you: "In general, it is considered bad practice to modify the observed value in this method."– fabian
Nov 11 at 22:59
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253137%2fjunit-5-javafx-multiple-changelisteners-on-integerproperty-not-working-as-intend%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Do not do updates from
ChangeListener
s. The javadoc forChangeListener.change
warns you: "In general, it is considered bad practice to modify the observed value in this method."– fabian
Nov 11 at 22:59