The Altair Community is migrating to a new platform to provide a better experience for you. In preparation for the migration, the Altair Community is on read-only mode from October 28 - November 6, 2024. Technical support via cases will continue to work as is. For any urgent requests from Students/Faculty members, please submit the form linked here
Creating JUnit tests for new opearotrs
Hello,
I have the following simple TestOperator and MyIOObject.
I am trying to make JUnit tests for this operator.
TestOperator
1) Is there any simple way how to create my operator object (TestOperator)? To use a constructor I am missing the OperatorDescription and its constructor seem to be quite complicated (requires additional classes).
2) How can I set the IO object (io) to the input port: ce.getInputPorts().getPortByIndex(0)
Thank you very much for any advice.
radone
I have the following simple TestOperator and MyIOObject.
I am trying to make JUnit tests for this operator.
TestOperator
public class TestOperator extends Operator {IO object
// input port
protected InputPort inParam = getInputPorts().createPort("MyIOObject");
// output port
protected OutputPort outParam = getOutputPorts().createPort("MyIOObject");
public TestOperator(OperatorDescription description) {
super(description);
}
public void doWork() throws OperatorException {
// Read input
MyIOObject ioObj = inParam.getData();
// Increment value
ioObj.setValue( ioObj.getValue() + 1 );
// Deliver output
outParam.deliver(ioObj);
}
}
public class MyIOObject extends ResultObjectAdapter {My idea about JUnit test is such as follows:
private int value = -1;
public void setValue(int value) {
this.value = value;
}
public int getValue() {
return this.value;
}
}
public class CirclePixelsExtractorTest extends TestCase {Please, could anyone help me with:
@Test
public void testMeanValue() throws OperatorException {
// create operator
// ??????????????????????????????????????????????????????
// ???????????????????????? 1 ???????????????????????????
// ??????????????????????????????????????????????????????
OperatorDescription od = null; // (1) ??? HOW TO GET AN OperatorDescription ??;
TestOperator ce = new TestOperator (od);
// create IO input data
MyIOObject io = new MyIOObject();
io.setValue(5);
// provide IO data as an input
// ??????????????????????????????????????????????????????
// ???????????????????????? 2 ???????????????????????????
// ??????????????????????????????????????????????????????
ce.getInputPorts().getPortByIndex(0). // how to set Data ???;
// execute the operator
ce.doWork();
// read output
MyIOObject io2 = ce.getOutputPorts().getPortByIndex(0).getData();
int res = io2.getValue();
// validate
assertEquals(6, res);
}
}
1) Is there any simple way how to create my operator object (TestOperator)? To use a constructor I am missing the OperatorDescription and its constructor seem to be quite complicated (requires additional classes).
2) How can I set the IO object (io) to the input port: ce.getInputPorts().getPortByIndex(0)
Thank you very much for any advice.
radone
Tagged:
0
Answers
For anyone dealing with the same problem:
Answer 1)
The OperatorDescription can be created by method: and the creating of the operator can be done by: Answer 2)
you don't need the OperatorDescription. If RapidMiner.init() was called, you can use
OperatorService.createOperator(YourOpClass.class).
Best,
Simon
Hello,I am trying to write Junit test case for my operator. Below is my operator code. Could you please help me how to write the test case for it
Hi @vc126m,
Would it be possible to share the java project file or the .jar file of this extension to test it and provide feedback or help here?
Cheers,
Hi @vc126m,
When you want to write JUnitTests for your own operator, you have to initialize RapidMiner itself in the Test Classes. You can do this by calling:
Then you can use the already mentioned OperatorService to create an instance of your operator.
With this instance you can implement any JUnit Test you can think of. If you are not so familiar with JUnit Tests you can find plenty of tutorials in the web (for example I was using this).
What you also can do is installing the Process Testing Extension from the Marketplace to be used in RapidMiner. You can then create UnitTest processes (which you for example can also store in your version control repository), and 'run and store expected results'. After that you can run 'test process' to check if the results of the processes changed.
Hopes this helps
Fabian