Reputation: 2833
How do you automate a functional test, spanning several Java processes with TestNG?
I got interested in trying TestNG instead of JUnit, because it claims to be designed for more than just unit testing, but I haven't been able to find a concrete example on how to do this.
I have a Java program (p1) that listens to events coming from some special hardware. It parses these commands and sends them to another Java process (p2) which performs actions based on the commands.
To test the entire chain, I managed to come up with the following ad hoc solution:
I do this using a simple shell script - could this be automated with TestNG?
Upvotes: 3
Views: 2045
Reputation: 10675
Yes, you can easily automate multiple scripts using testNG.
TestNG supports data driven testing; i.e. you provide all data in a method which gets passed to the test method to performs checks(assertions). Also all this happens through Annotation so you don't need to write any code for that.
I will suggest you to use IDE like Eclipse ( if you not using yet) and there are plugins available for eclipse. Through Eclipse; writing test cases as well as their automation becomes very easy. Will suggest you to just google; you will find enough examples.
Also Junit 4 is equally good.
Upvotes: 2
Reputation: 7428
What you've got is an execution environment that spans more than one process. That means that your test environment has to be able to automatically create those processes upon demand. The basic flow is standard: setup, multiple tests, teardown. The setup and teardown routines, though, need to start and stop processes, initialize inputs, provide test-specific environment arguments, etc.
From what you've described, it doesn't seem as if it matters that program p1
is in Java. You can use java.lang.Runtime
to execute p1
. You've already anticipated the needed to mock the hardware behavior, so I assume you already know how to get p1
running in an appropriate integration testing mode. If for some reason you need more that Runtime
gives you, look at JVMTI, the interface for debugging and instrumentation.
Depending on how you want to or need to test, you may have performance issues with bringing up a process for each test case. In that case you'd want one process for a single set of tests. In this situation you're reusing an asset, one that might fail, as it is a test environment after all. So you'd want to check that the process is running at the beginning of each test. One way to ensure that is to put the check in a factory method that supplies the main object-under-test to the test case. The class java.lang.Process
doesn't directly allow that to happen, but it does supply standard input/output to the process. If that available, implementing a simple "ping" interface on that channel would suffice. Other kinds of shared resources can also work.
Upvotes: 2