🎉Community Raffle - Win $25

An exclusive raffle opportunity for active members like you! Complete your profile, answer questions and get your first accepted badge to enter the raffle.
Join and Win

Creating JUnit tests for new opearotrs

User: "radone"
New Altair Community Member
Updated by Jocelyn
Hello,
I have the following simple TestOperator and MyIOObject.
I am trying to make JUnit tests for this operator.

TestOperator
public class TestOperator extends Operator {
       // 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);
}
}

IO object
public class MyIOObject  extends ResultObjectAdapter {
private int value = -1;
       public void setValue(int value) {
           this.value = value;
       }

       public int getValue() {
           return this.value;
       }
}
My idea about JUnit test is such as follows:
public class CirclePixelsExtractorTest extends TestCase {

@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);
}
}
Please, could anyone help me with:
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

Find more posts tagged with