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



  1. How can I get the ChangeListeners to my expected output?

  2. 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.










share|improve this question


















  • 1




    Do not do updates from ChangeListeners. 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















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



  1. How can I get the ChangeListeners to my expected output?

  2. 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.










share|improve this question


















  • 1




    Do not do updates from ChangeListeners. 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













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



  1. How can I get the ChangeListeners to my expected output?

  2. 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.










share|improve this question













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



  1. How can I get the ChangeListeners to my expected output?

  2. 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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 20:53









mepmerp

3251511




3251511








  • 1




    Do not do updates from ChangeListeners. 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














  • 1




    Do not do updates from ChangeListeners. 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








1




1




Do not do updates from ChangeListeners. 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 ChangeListeners. 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Bressuire

Vorschmack

Quarantine