Authors:
Ankit Mehta, Cille Schliebitz, Anita Gu
Changed on:
27 May 2025

| Annotation | Purpose |
| @RunWith | E.g. @RunWith(PowerMockRunner.class) is used to define a different test runner for PowerMock |
| @PrepareForTest | E.g. @PrepareForTest(Event.class) is used to prepare the runner for the Event class as the Event is the final class |



1
2 package com.training.rule;
3
4 public class MarkOrderHighValueRuleTest
5 {
6
7 private MarkOrderHighValueRule rule = new MarkOrderHighValueRule();
8
9 @Mock
10
11 private Context context;
12
13 @Before
14
15 public void setup()
16 {
17
18 MockitoAnnotations.initMocks(this);
19
20 }
21
22 }1/**
2 * NOTE: This is a snippet from `MarkOrderHighValueRule class from 'Writing the Rules' section
3 **/
4
5// Validation:
6Integer threshold = context.getProp(Constants.PARAM_NAME_HIGH_VALUE_THRESHOLD, Integer.class);
7if (null == threshold)
8{
9 throw new PropertyNotFoundException(400,String.format("Required Parameter not provided: %s", Constants.PARAM_NAME_HIGH_VALUE_THRESHOLD));
10}1<dependency>
2 <groupId>org.powermock</groupId>
3 <artifactId>powermock-module-junit4</artifactId>
4 <version>1.6.6</version>
5 <scope>test</scope>
6 </dependency>
7 <dependency>
8 <groupId>org.powermock</groupId>
9 <artifactId>powermock-api-mockito</artifactId>
10 <version>1.6.6</version>
11 <scope>test</scope>
12 </dependency>1// Required Mocks
2@Mock
3private Event event;
4@Mock
5private ReadOnlyFluentApiClient apiClient;
6@Mock
7private GetOrderByIdQuery.Data data;
8@Mock
9private GetOrderByIdQuery.OrderById orderById;
10@Mock
11private ActionFactory actionFactory;1@Test
2 public void run_withValueGreaterThanThreshold_addsAttributeWithNameIsHighValue() throws Exception {
3
4 //arrange:
5 when(orderById.totalPrice()).thenReturn(1001.00);
6 when(context.action()).thenReturn(actionFactory);
7
8 AttributeInput attributeInput = AttributeInput.builder().name("test-name").type("test-type").value("test-value").build();
9 PowerMockito.whenNew(AttributeInput.Builder.class).withNoArguments().thenReturn(mockedAttributeInputBuilder);
10 when(mockedAttributeInputBuilder.name(anyString())).thenReturn(mockedAttributeInputBuilder);
11 when(mockedAttributeInputBuilder.type(anyString())).thenReturn(mockedAttributeInputBuilder);
12 when(mockedAttributeInputBuilder.value(any())).thenReturn(mockedAttributeInputBuilder);
13 when(mockedAttributeInputBuilder.build()).thenReturn(attributeInput);
14
15 //act:
16 rule.run(context);
17
18 //assert:
19 verify(mockedAttributeInputBuilder, times(1)).name(Constants.IS_HIGH_VALUE_ORDER_ATTRIBUTE_NAME);
20 }1public class MarkOrderHighValueRuleExecutorTest {
2
3 private static final Integer HIGH_VALUE_THRESHOLD = 1000;
4 private static final String ORDER_ID = "123";
5
6 private TestExecutor executor;
7
8 private Event event;
9
10 @Mock
11 private ReadOnlyFluentApiClient apiClient;
12
13 @Mock
14 private GetOrderByIdQuery.Data data;
15
16 @Before
17 public void setup() {
18
19 MockitoAnnotations.initMocks(this);
20
21 when(apiClient.query(any())).thenReturn(data);
22 }
23}1@Before
2 public void setup() {
3
4 MockitoAnnotations.initMocks(this);
5
6 when(apiClient.query(any())).thenReturn(data);
7
8 // Setup up an instance of a Rubix Entity
9 RubixEntity entity = RubixEntity.builder()
10 .status("BOOKED")
11 .entityType("ORDER")
12 .flexType("flexType")
13 .flexVersion(1)
14 .ref(ORDER_ID)
15 .id(UUID.randomUUID().toString())
16 .build();
17
18 }1// Set up a TestExecutor to simulate Orchestration Engine
2 executor = TestExecutor.builder()
3 .rule(MarkOrderHighValueRule.class)
4 .ruleset(ruleSet)
5 .entity(entity)
6 .testApiClient(apiClient)
7 .build();1@Test
2 public void run_withTotalPriceGreaterThanThreshold_callsMutateAction() {
3
4 // arrange:
5 GetOrderByIdQuery.OrderById orderById = GetOrderByIdQuery.OrderById.builder().id(ORDER_ID).totalPrice(1001.00).type("type").__typename("type-name").build();
6
7 when(data.orderById()).thenReturn(orderById);
8
9 executor.validateWorkflow(event);
10
11 // act:
12 TestContext context = executor.execute(event);
13
14 // assert:
15 assertEquals(1, context.countActionsOfType(TestContext.MutateAction.class));
16 }