# Unit testing with juce::Value::Listener fails

**URL:** <https://forum.juce.com/t/unit-testing-with-juce-listener-fails/60436>\
**Category:** General JUCE discussion\
**Created:** [March 13, 2024, 10:48am UTC](https://forum.juce.com/t/unit-testing-with-juce-listener-fails/60436 "2024-03-13T10:48:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Audiodroid](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@Audiodroid](https://forum.juce.com/u/Audiodroid)\
**Post date:** [March 13, 2024, 10:48am UTC](https://forum.juce.com/t/unit-testing-with-juce-listener-fails/60436/1 "2024-03-13T10:48:28Z")

</div>

Hello,  
I have the following problem: I am using catch2 as a unit testing framework and I am trying to test my class, that derives from juce::Value::Listener. I’ve used the testing framework with juce a lot and it works fine in general. I have learned to call ScopedJuceInitialiser\_GUI() to get the MessageManager to run, but somehow the code - as outline/simplified below - still does not work. With some investigation I found listeners are called asynchronously, but even if I throw-in a sleep/wait after changing the value, the listener still doesn’t get called. Any help much appreciated.

> #include \<catch2/catch\_all.hpp\>
> 
> class MyListener : public juce::Value::Listener  
> {  
> public:  
> void valueChanged (Value& value)  
> {  
> changed = true;  
> }
> 
> ```
> bool valueHasChanged()
> {
> return changed;
> }
> 
> ```
> 
> private:  
> bool changed {false};  
> };
> 
> TEST\_CASE (“Test”, “[Test]”)  
> {  
> const auto x = juce::ScopedJuceInitialiser\_GUI();
> 
> ```
> auto value = juce::Value (var (0));
> 
> auto listener = MyListener();
> value.addListener (&listener);
> 
> value.setValue (1);
> 
> REQUIRE (listener.valueHasChanged());
> 
> ```
> 
> }

---

<div class="post-metadata">

**Author:** ![Audiodroid](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@Audiodroid](https://forum.juce.com/u/Audiodroid)\
**Post date:** [March 13, 2024, 11:55am UTC](https://forum.juce.com/t/unit-testing-with-juce-listener-fails/60436/2 "2024-03-13T11:55:36Z")

</div>

I found the answer to this problem here:

> [@GUI testing in JUCE](https://forum.juce.com/t/gui-testing-in-juce/59503):
>
> Hi all. I’im the process of testing my JUCE plugin with the integrated juce UnitTest and UnitTestRunner classes. I have so far been successful in testing most of the logic and simple GUI interactions (simple “Fake” simulated clicks or scrolls or drag/drops), but I have been unsuccessful in testing any value change that interacts with the JUCE value tree. My attepts look something like: Retrieve the pointer to the component I need to test a value change on, taking the example of a ToggleButto…

You need to run the test-runner on a new thread, and then run the core of the actual test on the main/message-thread. For a more detailed explanation follow the link above.

---

<div class="post-metadata">

**Author:** ![anthony-nicholls](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/anthony-nicholls/32/1246_2.png) [@anthony-nicholls](https://forum.juce.com/u/anthony-nicholls)\
**Post date:** [March 13, 2024, 12:27pm UTC](https://forum.juce.com/t/unit-testing-with-juce-listener-fails/60436/3 "2024-03-13T12:27:48Z")

</div>

Beat me to it, I couldn’t find that thread when I searched. We’ll try and put a tutorial together some point in the future, post JUCE 8.
