# Unit Testing - Integration Testing of Multiple Components (not Dummy)

**URL:** <https://community.openems.io/t/unit-testing-integration-testing-of-multiple-components-not-dummy/2005>\
**Category:** Software-Development\
**Created:** [October 11, 2023, 11:47am UTC](https://community.openems.io/t/unit-testing-integration-testing-of-multiple-components-not-dummy/2005 "2023-10-11T11:47:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomas](https://community.openems.io/user_avatar/community.openems.io/thomas/32/670_2.png) [@thomas](https://community.openems.io/u/thomas)\
**Post date:** [October 11, 2023, 11:47am UTC](https://community.openems.io/t/unit-testing-integration-testing-of-multiple-components-not-dummy/2005/1 "2023-10-11T11:47:30Z")

</div>

I am interested in testing the integration of the component I have written (a battery) with the Generic Symmetric ESS component as I have been seeing some issues around starting the battery in this setup.

I have previously written some unit tests for my battery using the `ComponentTest` class which allows one to instantiate a component and add references to dummy components where needed such as ModbusBridge. Like this:

```auto
new ComponentTest(new MyBatteryImpl()) //
				.addReference("cm", new DummyConfigurationAdmin()) //
				.addReference("componentManager", new DummyComponentManager(clock)) //
				.addReference("setModbus", new DummyModbusBridge(MODBUS_ID)) //
				.activate(MyConfig.create() //
						.setId(BATTERY_ID) //
						.setModbusId(MODBUS_ID) //
						.setModbusUnitId(1) //
						.setStartStop(StartStopConfig.START) //
						.build())
				.next(new TestCase("Check State Machine = UNDEFINED to begin")
						.input(STATUS_CHANNEL, MyBattery.Status.SLEEP) // Ensure that it is in a sleep mode to begin with, so it will need to be woken up
						.output(STATE_MACHINE, StateMachine.State.UNDEFINED))

```

This works fine, but I have a couple of issues:

1. I would like to call a method on the MyBatteryImpl class as part of a test case. Specifically I would like to call the `setStartStop` method to simulate the Generic ESS calling this method to start the battery. I know that I can use the `input` method of the `TestCase` to set specific channel value, but how can I call an actual method on the unit under test?
2. I would like to write a test involving an ’ EssGenericManagedSymmetric’ and ‘MyBattery’ (with a dummy battery inverter). Is this possible, and are there any examples available where this is done? I can’t find any examples of tests where there are two ‘real’ components under test - as opposed to a single real component under test and references to only dummy components.

Hope my questions make sense - any help greatly appreciated.

Thanks,  
Thomas

---

<div class="post-metadata">

**Author:** ![thomas](https://community.openems.io/user_avatar/community.openems.io/thomas/32/670_2.png) [@thomas](https://community.openems.io/u/thomas)\
**Post date:** [October 11, 2023, 2:36pm UTC](https://community.openems.io/t/unit-testing-integration-testing-of-multiple-components-not-dummy/2005/2 "2023-10-11T14:36:58Z")

</div>

**Update**

I have an answer to question 1, which is quite simple. It is just to declare the battery (or SUT) as a variable which is passed to the test case. Code looks like the below:

```auto
// Define the battery as a variable
MyBattery sut = new MyBattery();

// Set up the test	
ComponentTest test = new ComponentTest(sut) //
			.addReference("cm", new DummyConfigurationAdmin()) //
			.addReference("componentManager", new DummyComponentManager(clock)) //
			.addReference("setModbus", new DummyModbusBridge(MODBUS_ID)) //
			.activate(MyConfig.create() //
					.setId(BATTERY_ID) //
					.setModbusId(MODBUS_ID) //
					.setModbusUnitId(1) //
					.setStartStop(StartStopConfig.AUTO) //
					.build())
			.next(new TestCase("Start in UNDEFINED state. The StartStop is UNDEFINED to begin with")
					.input(STATUS_CHANNEL, Status.IDLE)
					.output(STATE_MACHINE, StateMachine.State.UNDEFINED)
					.output(START_STOP, StartStop.UNDEFINED))

// Call method on the battery
sut.setStartStop(StartStop.START);

// Continue the test
test.next(new TestCase("Check that nothing has changed (yet)")
					.output(STATE_MACHINE, StateMachine.State.UNDEFINED)
					.output(START_STOP, StartStop.UNDEFINED))

```

I still am not sure how it is possible to define a test involving a Generic ESS and MyBattery.

---

<div class="post-metadata">

**Author:** ![EEMChula](https://community.openems.io/user_avatar/community.openems.io/eemchula/32/786_2.png) [@EEMChula](https://community.openems.io/u/EEMChula)\
**Post date:** [October 14, 2023, 1:05am UTC](https://community.openems.io/t/unit-testing-integration-testing-of-multiple-components-not-dummy/2005/3 "2023-10-14T01:05:50Z")

</div>

Hi Thomas,

I am begineer of OpenEMS. I am also interested in implementing of new Battery. Could you tell me where you modified the code and how did you implement it?

Thank you very much in advance.

Best regards,  
EEM

---

<div class="post-metadata">

**Author:** ![thomas](https://community.openems.io/user_avatar/community.openems.io/thomas/32/670_2.png) [@thomas](https://community.openems.io/u/thomas)\
**Post date:** [October 15, 2023, 6:45pm UTC](https://community.openems.io/t/unit-testing-integration-testing-of-multiple-components-not-dummy/2005/4 "2023-10-15T18:45:30Z")

</div>

> [@EEMChula](#):
>
> ch in advance.
> 
> Best rega

Hi EEMChula,

Sure I can provide some information. Can you let me know a little more about the battery you want to implement in OpenEMS? What connectivity does the battery provide (e.g modbus)? Do you have a Modbus specification for the battery?

In general it is necessary to create a new device in OpenEMS This guide ( [Implementing a device :: Open Energy Management System (openems.github.io)](https://openems.github.io/openems.io/openems/latest/edge/implement.html)) is a good starting point. The main programming task is to implement the communication with the device, which for me involved defining all the Modbus registers used by the battery and mapping these to OpenEMS channels.

I found it very useful to thoroughly read the code for existing battery implementations in OpenEMS - e.g for the Fenecon Home and Commercial batteries - and I used this as the basis for the device that I wrote.

Happy to share more information and my code if you can provide a little more info on your situation 🙂

---

<div class="post-metadata">

**Author:** ![EEMChula](https://community.openems.io/user_avatar/community.openems.io/eemchula/32/786_2.png) [@EEMChula](https://community.openems.io/u/EEMChula)\
**Post date:** [October 16, 2023, 3:34am UTC](https://community.openems.io/t/unit-testing-integration-testing-of-multiple-components-not-dummy/2005/5 "2023-10-16T03:34:36Z")

</div>

Hi thomas,

Thank you very much for your response. I followed this guide([Implementing a device :: Open Energy Management System](https://openems.github.io/openems.io/openems/latest/edge/implement.html)) to implement new Meter. I haven’t tried yet for a new battery. I will try, and follow your above suggestions and update you.

Thanks again.

---

<div class="post-metadata">

**Author:** ![SvenAarts](https://community.openems.io/user_avatar/community.openems.io/svenaarts/32/943_2.png) [@SvenAarts](https://community.openems.io/u/SvenAarts)\
**Post date:** [December 11, 2023, 2:59pm UTC](https://community.openems.io/t/unit-testing-integration-testing-of-multiple-components-not-dummy/2005/6 "2023-12-11T14:59:36Z")

</div>

hello Thomas,

I also am having troubles defining a battery.  
My biggest problem is that the battery communicates with an RS485 cable to the inverter (Sinexcel PWS2-30P-EX). how did you resolve this problem? i have a working modbus-tcp connection with the inverter but this doesn’t give me an ESS.ID so i cant implement any controller. Do you have an idea how to specify this in code?

Thanks in advance!

With kind regards,

Sven

---

<div class="post-metadata">

**Author:** ![thomas](https://community.openems.io/user_avatar/community.openems.io/thomas/32/670_2.png) [@thomas](https://community.openems.io/u/thomas)\
**Post date:** [December 11, 2023, 6:15pm UTC](https://community.openems.io/t/unit-testing-integration-testing-of-multiple-components-not-dummy/2005/7 "2023-12-11T18:15:30Z")

</div>

Hi Sven,

I have some ideas, but I have a couple of questions before I can say which approach might be best…

1. Is your inverter already supported by OpenEMS? Or is it necessary to define a new device for this?
2. Does the modbus TCP connection with your inverter report values such as State of Charge, Battery voltage, temperature etc? (I.e the battery parameters which are defined in the Battery Nature within OpenEMS).

Thomas

---

<div class="post-metadata">

**Author:** ![SvenAarts](https://community.openems.io/user_avatar/community.openems.io/svenaarts/32/943_2.png) [@SvenAarts](https://community.openems.io/u/SvenAarts)\
**Post date:** [December 12, 2023, 10:26am UTC](https://community.openems.io/t/unit-testing-integration-testing-of-multiple-components-not-dummy/2005/8 "2023-12-12T10:26:19Z")

</div>

Hello Thomas,

Thankyou for your quick response.

1. The Sinexcel inverter seems supported (i see other people with this inverter also on the forum but they all dont have a actual battery connected). The state of the inverter is also correctly reported in openems UI (so when it has an error you see a red exclamation mark in the top and when there is no error it turns green). I However get no dashboard data shown or any data visible in UI or Edge.
2. Yes the Battery gives all BMS information to the Sinexcell inverter and sinexcell inverter listens to this (so max charge current is only 9 amps the sinexcell will not go past that). These values i dont get visible in Edge or UI.

Sven

---

<div class="post-metadata">

**Author:** ![stefan.feilmeier](https://community.openems.io/user_avatar/community.openems.io/stefan.feilmeier/32/2585_2.png) [@stefan.feilmeier](https://community.openems.io/u/stefan.feilmeier)\
**Post date:** [January 2, 2024, 10:03pm UTC](https://community.openems.io/t/unit-testing-integration-testing-of-multiple-components-not-dummy/2005/9 "2024-01-02T22:03:10Z")

</div>

Just for reference, there is also a separate thread about the Sinexcel + Battery problem → [Sinexcel inverter with battery - #2 by stefan.feilmeier](https://community.openems.io/t/sinexcel-inverter-with-battery/2229/2)

* * *

Back to the original topic:

Hi @thomas.

> [@thomas](#):
>
> 1. I would like to write a test involving an ’ EssGenericManagedSymmetric’ and ‘MyBattery’ (with a dummy battery inverter). Is this possible, and are there any examples available where this is done? I can’t find any examples of tests where there are two ‘real’ components under test - as opposed to a single real component under test and references to only dummy components.

Generally we avoid hard-wired connection between multiple OSGi bundles to avoid spaghetti code. But of course you can to that for your specific use-case. Here is a (old but simple) example of a JUnit test that activates two Components → [https://github.com/OpenEMS/openems/blob/develop/io.openems.edge.bosch.bpts5hybrid/test/io/openems/edge/bosch/bpts5hybrid/pv/BoschBpts5HybridPvTest.java](https://github.com/OpenEMS/openems/blob/develop/io.openems.edge.bosch.bpts5hybrid/test/io/openems/edge/bosch/bpts5hybrid/pv/BoschBpts5HybridPvTest.java)

If anyway you hard-wire your Battery with Generic-ESS, you can simply rely on Generic-Ess to call the `setStartStop` method. Just like as it is done in the Generic-ESS Junit test → [https://github.com/OpenEMS/openems/blob/develop/io.openems.edge.ess.generic/test/io/openems/edge/ess/generic/symmetric/EssGenericManagedSymmetricImplTest.java#L47-L73](https://github.com/OpenEMS/openems/blob/develop/io.openems.edge.ess.generic/test/io/openems/edge/ess/generic/symmetric/EssGenericManagedSymmetricImplTest.java#L47-L73)

By the way, just a small note: if you want to keep the ‘fluent’ coding style in JUnit tests, there are fluent-helpers to call a method in a cycle-event, e.g.

> <https://github.com/OpenEMS/openems/blob/develop/io.openems.edge.battery.soltaro/test/io/openems/edge/battery/soltaro/cluster/versionb/BatterySoltaroClusterVersionBImplTest.java#L54-L55>

Regards,  
Stefan
