diff --git a/.gitattributes b/.gitattributes
index dd04030ae3ce2c291a317be4355891da4aaa1445..ec69a4810c72e109c28daf9d20d8b442c1e6e2ad 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -4641,6 +4641,7 @@ SAS/OTDB_Services/test/t_TreeService.in.unittest_db.dump.gz -text svneol=unset#a
 SAS/OTDB_Services/test/t_TreeService.py -text
 SAS/OTDB_Services/test/t_TreeService.run -text svneol=unset#application/x-shellscript
 SAS/OTDB_Services/test/t_TreeService.sh -text svneol=unset#application/x-shellscript
+SAS/OTDB_Services/test/t_TreeStatusEvents.in.unittest_db.dump.gz -text
 SAS/OTDB_Services/test/t_TreeStatusEvents.py -text
 SAS/OTDB_Services/test/t_TreeStatusEvents.run -text svneol=unset#application/x-shellscript
 SAS/OTDB_Services/test/t_TreeStatusEvents.sh -text svneol=unset#application/x-shellscript
diff --git a/SAS/OTDB_Services/test/t_TreeService.py b/SAS/OTDB_Services/test/t_TreeService.py
index 6b35c2ac4a5e62405c5813a8940f42686a2b9afd..6080285faf5a8eccaf5714ce9054c17d3573ce1d 100644
--- a/SAS/OTDB_Services/test/t_TreeService.py
+++ b/SAS/OTDB_Services/test/t_TreeService.py
@@ -28,11 +28,9 @@ KeyUpdateCommand        : function to update the value of multiple (existing) ke
 StatusUpdateCommand     : finction to update the status of a tree.
 """
 
-import sys
 import logging
 import testing.postgresql
 import psycopg2
-import gzip
 import subprocess
 from lofar.messaging.messagebus import *
 from lofar.messaging.RPC import *
diff --git a/SAS/OTDB_Services/test/t_TreeStatusEvents.in.unittest_db.dump.gz b/SAS/OTDB_Services/test/t_TreeStatusEvents.in.unittest_db.dump.gz
new file mode 100644
index 0000000000000000000000000000000000000000..961fb48516052918b0eeb2ee562216f24662d2de
Binary files /dev/null and b/SAS/OTDB_Services/test/t_TreeStatusEvents.in.unittest_db.dump.gz differ
diff --git a/SAS/OTDB_Services/test/t_TreeStatusEvents.py b/SAS/OTDB_Services/test/t_TreeStatusEvents.py
index dddda9a9b0d8d33ea462bc37d42c55bf424b9516..d45524664dadb49307c6e842bc7e61bfb4ca1b5d 100644
--- a/SAS/OTDB_Services/test/t_TreeStatusEvents.py
+++ b/SAS/OTDB_Services/test/t_TreeStatusEvents.py
@@ -30,62 +30,68 @@ StatusUpdateCommand     : finction to update the status of a tree.
 
 import sys, pg
 import logging
-from optparse import OptionParser
-from lofar.messaging import FromBus
+import testing.postgresql
+import psycopg2
+import subprocess
+from lofar.messaging.messagebus import *
+from lofar.sas.otdb.config import DEFAULT_OTDB_SERVICENAME
+from lofar.sas.otdb.TreeStatusEvents import create_service
+from lofar.common.dbcredentials import Credentials
+import threading
 
-logging.basicConfig(stream=sys.stdout, level=logging.INFO)
+logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.INFO)
 logger = logging.getLogger(__name__)
 
-if __name__ == "__main__":
-    # Check the invocation arguments
-    parser = OptionParser("%prog [options]")
-    parser.add_option("-D", "--database", dest="dbName", type="string", default="",
-                      help="Name of the database")
-    parser.add_option("-H", "--hostname", dest="dbHost", type="string", default="sasdb",
-                      help="Hostname of database server")
-    parser.add_option("-B", "--busname", dest="busname", type="string", default="",
-                      help="Busname or queue-name the status changes are published on")
-    (options, args) = parser.parse_args()
-
-    if not options.dbName:
-        print("Missing database name")
-        parser.print_help()
-        sys.exit(1)
-
-    if not options.dbHost:
-        print("Missing database server name")
-        parser.print_help()
-        sys.exit(1)
-
-    if not options.busname:
-        print("Missing busname")
-        parser.print_help()
-        sys.exit(1)
-
-    try:
-        print("user=postgres, host=", options.dbHost, "dbname=", options.dbName)
-        otdb_connection = pg.connect(user="postgres", host=options.dbHost, dbname=options.dbName)
-    except (TypeError, SyntaxError, pg.InternalError):
-        print("DatabaseError: Connection to database could not be made")
-        sys.exit(77)
-
-    with FromBus(options.busname) as frombus:
-        # First drain the queue
-        no_exception = True
-        while no_exception:
-            try:
-                msg = frombus.receive(timeout=1)
-                frombus.ack(msg)
-            except Exception:
-                no_exception = False
-
-        otdb_connection.query("select setTreeState(1, %d, %d::INT2,'%s')" % (1099266, 500, False))
-        msg = frombus.receive(timeout=5)	  # TreeStateEVent are send every 2 seconds
-        frombus.ack(msg)
-        msg.show()
-        try:
-            ok = (msg.body['treeID'] == 1099266 and msg.body['state'] == 'queued')
-        except IndexError:
-            ok = False
+try:
+    postgresql = testing.postgresql.PostgresqlFactory()()
+
+    database_credentials = Credentials()
+    database_credentials.host = postgresql.dsn()['host']
+    database_credentials.database = postgresql.dsn()['database']
+    database_credentials.port = postgresql.dsn()['port']
+
+    # connect to test-db as root
+    conn = psycopg2.connect(**postgresql.dsn())
+    cursor = conn.cursor()
+
+    # set credentials to be used during tests
+    database_credentials.user = 'otdb_test_user'
+    database_credentials.password = 'otdb_test_password'  # cannot be empty...
+
+    # create user role
+    query = "CREATE USER %s WITH SUPERUSER PASSWORD '%s'" % (database_credentials.user, database_credentials.password)
+    cursor.execute(query)
+    conn.commit()
+    conn.close()
+
+    cmd1 = ['gzip', '-dc', 't_TreeStatusEvents.in.unittest_db.dump.gz']
+
+    cmd2 = ['psql', '-U', database_credentials.user, '-h', database_credentials.host,
+            '-p', str(database_credentials.port), database_credentials.database]
 
+    proc1 = subprocess.Popen(cmd1, stdout=subprocess.PIPE)
+    proc2 = subprocess.Popen(cmd2, stdin=proc1.stdout)
+    proc1.wait(timeout=60)
+    proc2.wait(timeout=60)
+
+    otdb_connection = pg.connect(**database_credentials.pg_connect_options())
+
+    with TemporaryQueue(__name__) as tmp_queue:
+        with tmp_queue.create_frombus() as frombus:
+
+            t = threading.Thread(target=create_service, args=(tmp_queue.address, database_credentials))
+            t.daemon = True
+            t.start()
+
+            otdb_connection.query("select setTreeState(1, %d, %d::INT2,'%s')" % (1099266, 500, False))
+            msg = frombus.receive(timeout=5)	  # TreeStateEVent are send every 2 seconds
+            frombus.ack(msg)
+            msg.show()
+            try:
+                ok = (msg.body['treeID'] == 1099266 and msg.body['state'] == 'queued')
+            except IndexError:
+                ok = False
     sys.exit(not ok)   # 0 = success
+
+finally:
+    postgresql.stop()
diff --git a/SAS/OTDB_Services/test/t_TreeStatusEvents.run b/SAS/OTDB_Services/test/t_TreeStatusEvents.run
index 999e85705f8cbb6f6e84c72e98627074e0ffab09..eeb623a8a83d620fe967b2f30c540079e34bd907 100755
--- a/SAS/OTDB_Services/test/t_TreeStatusEvents.run
+++ b/SAS/OTDB_Services/test/t_TreeStatusEvents.run
@@ -1,25 +1,6 @@
 #!/bin/bash -x
-# constants
-DBHOST=sasdbtest.control.lofar
 
-#cleanup on normal exit and on SIGHUP, SIGINT, SIGQUIT, and SIGTERM
-trap 'qpid-config del exchange --force $queue ; kill ${SERVICE_PID} ; dropdb -U postgres -h ${DBHOST} ${DBNAME}' 0 1 2 3 15
-
-# Generate randome queue name
-queue=$(< /dev/urandom tr -dc [:alnum:] | head -c10)
-DBNAME=unittest_${queue}
-
-# Create the queue
-qpid-config add exchange topic $queue
-
-# Setup a clean database with predefined content
-createdb -U postgres -h ${DBHOST} ${DBNAME}
-gzip -dc $srcdir/unittest_db.dump.gz | psql -U postgres -h ${DBHOST} ${DBNAME} -f -
-TreeStatusEvents.py -B $queue -D ${DBNAME} -H ${DBHOST} -U postgres &
-SERVICE_PID=$!
-# Starting up takes a while
-sleep 3
 
 # Run the unit test
 source python-coverage.sh
-python_coverage_test "Messaging/python" t_TreeStatusEvents.py -D ${DBNAME} -H ${DBHOST} -B $queue
+python_coverage_test "Messaging/python" t_TreeStatusEvents.py