To create a test case for the createSubscribedFieldJson method with a focus on the primsizeindex, secsizeindex, and creationstatus, we will use Mockito to mock dependencies and instantiate the required objects for the test. The test will assert that the method behaves as expected when the relevant keys are present in the PartnerSubscribedResponseMap.
Mock Classes Required
We need to create mock versions of the various classes that may be used in the createSubscribedFieldJson method, such as ProductDataResponse, Color, ProductDescriptions, and any other relevant classes depending on your context.
Sample Test Case
Here's a sample test case using JUnit 4 and Mockito. Make sure to adjust the imports and class names based on your actual code structure.
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.*;
public class YourServiceTest {
@InjectMocks
private YourService yourService; // The service containing the method to test
@Mock
private ProductDataResponse structuredObject;
@Mock
private List<ProductDataResponse> productDataResponseList;
private Map<String, Object> partnerSubscribedResponseMap;
@Before
public void setUp() {
MockitoAnnotations.openMocks(this);
// Example Subscription Map
partnerSubscribedResponseMap = new HashMap<>();
}
@Test
public void testCreateSubscribedFieldJson_withPrimSizeIndex_secSizeIndex_creationStatus() {
// Prepare a structured object with necessary properties
when(structuredObject.getStrokeNo()).thenReturn("12345");
when(structuredObject.getDept()).thenReturn("Dept1");
when(structuredObject.getPrimSizeRange()).thenReturn("100-200");
when(structuredObject.getSecSizeRange()).thenReturn("50-100");
when(structuredObject.getCreationStatus()).thenReturn("CREATED");
// Create a mock variant containing primSizeIndex and secSizeIndex
ProductDataResponse variant = mock(ProductDataResponse.class);
when(variant.getPrimarySizeIndex()).thenReturn(1);
when(variant.getSecondarySizeIndex()).thenReturn(2);
// Add the variant to the productDataResponseList mock
when(productDataResponseList.get(0)).thenReturn(variant);
// Populate partnerSubscribedResponseMap with relevant keys
partnerSubscribedResponseMap.put(FieldSubscriptionKeys.PRIMSIZEINDEX, true);
partnerSubscribedResponseMap.put(FieldSubscriptionKeys.SECSIZEINDEX, true);
partnerSubscribedResponseMap.put(FieldSubscriptionKeys.CREATION_STATUS, true);
// Call the method under test
Map<String, Object> response = yourService.createSubscribedFieldJson(
productDataResponseList, structuredObject, partnerSubscribedResponseMap, true);
// Assertions to validate that the map returned contains expected values
assertNotNull(response);
assertEquals("12345", response.get("strokeNo"));
assertEquals("Dept1", response.get("dept"));
assertEquals("100-200", response.get("primSizeRange"));
assertEquals("50-100", response.get("secSizeRange"));
assertEquals("CREATED", response.get("creationStatus"));
// Checking the index values in the response
List<ProductDataResponse> seasonData = (List<ProductDataResponse>) response.get("seasons");
assertEquals(1, seasonData.size());
assertEquals(1, seasonData.get(0).getPrimarySizeIndex());
assertEquals(2, seasonData.get(0).getSecondarySizeIndex());
}
}
Explanation of the Test Code:
-
InjectMocks: The YourService (replace with the actual name of your service class) instance is created, and all the mocks are injected into it.
-
Set up mocks:
- The
structuredObject is mocked to return specific values when certain methods are called (such as getStrokeNo, getDept, etc.).
- A mock variant is set up for the primary and secondary size indexes.
-
PartnerSubscribedResponseMap keys: Core keys are populated to instruct the method to include the values associated with primsizeindex, secsizeindex, and creationstatus.
-
Method Invocation: The createSubscribedFieldJson is called with the prepared parameters.
-
Assertions: The test checks that:
- The returned map is not null.
- The expected structure is present with the correct values taken from the
structuredObject.
- The primary and secondary size indexes are correctly populated.
Adjustments:
- Adjust the objects in the
import statements and replace FieldSubscriptionKeys with the correct enumerations or constants according to your project's definitions.
- Ensure the assertions match your method's response structure.
This test case will ensure that your method correctly populates the JSON fields based on the subscription keys present in the PartnerSubscribedResponseMap. If you're running in a JUnit 5 environment, ensure to use the appropriate annotations and imports.