Mock private static method in final class using PowerMockito - java

I have a final class with private static method which is invoked inside another static method
public final class GenerateResponse{
private static Map<String, String> getErrorDetails(JSONObject jsonObject) {
// implementation
}
public static String method1(params...){
Map<String, String> map = getErrorDetails(new JsonObject());
// implementation
}
}
I need to mock the private static method call getErrorDetails(), but my test is calling the actual method. Here is my code:
#RunWith(PowerMockRunner.class)
#PrepareForTest(GenerateResponse.class)
public class GenerateResponseTest{
#Test
public void testFrameQtcErrorResponse() throws Exception {
Map<String, String> errorDtls = new HashMap<String, String>();
PowerMockito.spy(GenerateResponse.class);
PowerMockito.doReturn(errorDtls).when(GenerateResponse.class, "getErrorDetails", JSONObject.class);
String response = GenerateResponse.method1(params...);
}

You should use an argument matcher in the whenmethod. I've modified your code a little bit to run the test case.
Actual method
public final class GenerateResponse{
private static Map<String, String> getErrorDetails(JSONObject jsonObject) {
return null;
}
public static String method1() {
Map<String, String> map = getErrorDetails(new JSONObject());
return map.get("abc");
}
}
Test method
#RunWith(PowerMockRunner.class)
#PrepareForTest(GenerateResponse.class)
public class GenerateResponseTest {
#Test
public void testFrameQtcErrorResponse() throws Exception {
Map<String, String> errorDtls = new HashMap<String, String>();
errorDtls.put("abc", "alphabets");
PowerMockito.mockStatic(GenerateResponse.class, Mockito.CALLS_REAL_METHODS);
PowerMockito.doReturn(errorDtls).when(GenerateResponse.class,
"getErrorDetails", Matchers.any(JSONObject.class));
String response = GenerateResponse.method1();
System.out.println("response =" + response);
}
}
Output
response =alphabets

Related

How to Mock HttpHeaders inside MultivaluedMap

In my test class, I have framed it as
public class TestDummy {
private AClass a;
private final HttpHeaders mockHeader = Mockito.mock(HttpHeaders.class);
private final MultivaluedMap<String, String> mockMultiHeaderMap =
Mockito.mock(MultivaluedMap.class);
#BeforeEach
void beforeMethod(){
Mockito.when(mockMultiHeaderMap.getFirst("abc")).thenReturn("def);
}
void actualTestClass(){
Assertions.assertDoesNotThrow(() -> {
return a.method(mockHeader);
});
}
}
This is my actual dummy class
public AClass dummy{
public void method(HttpHeaders headers){
MultivaluedMap<String, String> multiHeaderMap = headers.getRequestHeaders();
String val = multiHeaderMap.getFirst("abc");
}
}
I know that I am setting value to mockMultiHeaderMap but I am not getting that how to update my testcase since, in actual AClass we have to pass HttpHeaders object itself on method().

Java: How to Mock a protected method inside a static child class

I am having a protected method inside a static child class. I am running a testcase , its getting successful but code coverage is not increasing.
public class A{
private static final String var1 = "key1";
protected static class B extends OsCmd{
private String abc1;
private String abc2;
protected B(String xyz, String xyz2) {
this.abc1 = xyz;
this.abc2 = xyz2;
}
#Override
protected void updateEnv(Map<String, String> env) {
env.put(VAR1, "FALSE");
env.put(VAR2, "TRUE");
env.put("key3", abc1);
env.put("key4", abc2);
}
}
}
Below is my test case
#ExtendWith(MockitoExtension.class)
public class ATest {
private A mockA;
#BeforeEach
public void setup() {
mockA = Mockito.spy(A.class);
}
#Test
public void test2() {
try (MockedConstruction mockedConstruction =
mockConstruction(A.B.class)) {
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
A.B mockB =
new A.B("a", "b");
//doNothing().when(mockB).updateEnv(map);
mockB.updateEnv(map);
}
}
}
Can someone please help here, what mistake i am doing?
When you mock the constructor, then all internal method calls are also mocked and do not go through the actual code.
If you remove the following try-with-resources:
try (MockedConstruction mockedConstruction =
mockConstruction(A.B.class))
The real code will be executed and the coverage will increase.

Composition or Inheritance for classes with almost similar implementations but different input and outputs for methods?

I have the following classes, which have quite similar method implementations. Only the classes' method inputs and outputs seem to be of different types. When I put it like this, it sounds like a case for inheritance, however, the fact that the inputs and outputs are different and are related to two lambdas, make me wonder if they should remain without any relationship, as one lambda cannot be thought of in place of another (To be a case for inheritance).
My first class looks like the following.
public class JobPersistenceManager {
private String jobIndexName;
private JobLambda JobLambda;
private MyDataPersistence myDataPersistence;
private DataProcessorUtils dataProcessorUtils;
private static final String JOB_ID = "jobId";
private static final String JOB_NAME = "jobName";
#Inject
public JobPersistenceManager(#Named("jobIndexName") String jobIndexName,
JobLambda JobLambda,
MyDataPersistence myDataPersistence) {
this.jobIndexName = jobIndexName;
this.JobLambda = JobLambda;
this.myDataPersistence = myDataPersistence;
createIndexIfNotExists(this.jobIndexName);
}
public SearchDocumentResult searchJob(MyJobInput myJobInput) throws IOException {
return myDataPersistence
.searchDocument(this.jobIndexName,
dataProcessorUtils.transformObjectDataPayloadToMap(myJobInput));
}
public MyJobOutput invokeCreateJobLambdaAndIndexData(final MyJobInput myJobInput)
throws IOException {
String personRequestPayload = dataProcessorUtils.transformObjectDataInputJson(myJobInput);
Map<String, String> createdJobOutput = this.JobLambda.invokeLambda(personRequestPayload);
this.indexCreatedJob(myJobInput, createdPersonOutput);
return MyJobOutput.builder().withJobID(createdJobOutput.get(JOB_ID))
.withJobName(createdJobOutput.get(JOB_NAME)).build();
}
public int indexCreatedJob(final MyJobInput myJobInput,
final Map<String, String> createdJobOutput) throws IOException {
myJobInput = modifyJobInput(myJobInput);
String documentToIndex = dataProcessorUtils.transformObjectDataInputJson(myJobInput);
return myDataPersistence.indexDocument(this.jobIndexName, documentToIndex);
}
private void createIndexIfNotExists(final String indexName) {
if (!myDataPersistence.doesIndexExist(indexName)) {
myDataPersistence.createIndex(CreateIndexInput.builder().indexName(indexName).build());
}
}
}
My second class looks like the following.
public class EmployeePersistenceManager {
private EmployeeLambda employeeLambda;
private MyTestDataPersistence myTestDataPersistence;
private DataProcessorUtils dataProcessorUtils;
private String employeeIndexName;
private static final String PERSON_ID_KEY = "personId";
private static final String PERSON_NAME_KEY = "personName";
#Inject
public EmployeePersistenceManager(#Named("employeeIndexName") String employeeIndexName,
EmployeeLambda employeeLambda,
MyTestDataPersistence myTestDataPersistence,
DataProcessorUtils dataProcessorUtils) {
this.employeeIndexName = employeeIndexName;
this.employeeLambda = employeeLambda;
this.myTestDataPersistence = myTestDataPersistence;
this.dataProcessorUtils = dataProcessorUtils;
createIndexIfNotExists(employeeIndexName);
}
public SearchDocumentResult searchPerson(EmployeeInput employeeInput) throws IOException {
return myTestDataPersistence
.searchDocument(employeeIndexName,
dataProcessorUtils.transformObjectDataPayloadToMap(employeeInput));
}
public EmployeeOutput invokeCreatePersonLambdaAndIndexData(final EmployeeInput employeeInput)
throws IOException {
String personRequestPayload = dataProcessorUtils.transformObjectDataInputJson(employeeInput);
Map<String, String> createdPersonOutput = this.employeeLambda.invokeLambda(personRequestPayload);
this.indexCreatedEmployee(employeeInput, createdPersonOutput);
return EmployeeOutput.builder().withPersonId(createdPersonOutput.get(PERSON_ID_KEY))
.withPersonName(createdPersonOutput.get(PERSON_NAME_KEY)).build();
}
public int indexCreatedEmployee(final EmployeeInput employeeInput,
final Map<String, String> createdPersonOutput) throws IOException {
employeeInput = modifyEmployeeInput(employeeInput);
String documentToIndex = dataProcessorUtils.transformObjectDataInputJson(employeeInput);
return myTestDataPersistence.indexDocument(this.employeeIndexName, documentToIndex);
}
public Map.Entry<String, Map<String, String>> invokeLambda(final String payload) {
return new AbstractMap.SimpleEntry<>(payload, this.employeeLambda.invokeLambda(payload));
}
private void createIndexIfNotExists(final String indexName) {
if (!myTestDataPersistence.doesIndexExist(indexName)) {
myTestDataPersistence.createIndex(CreateIndexInput.builder().indexName(indexName).build());
}
}
}
As you can see, the methods perform almost the same actions. Only the indexCreatedEmployee and indexCreatedJob methods from the classes have an extra step of processing the input.
Should I keep these classes as they are now without any relationships between them, or should I create an abstract persistence manager class and perform the following.
Move createIndexIfNotExists to the abstract class
Create abstract methods search(), invokeLambda() and indexCreatedData() methods and implement them in each child class. The data types MyJobInput and MyEmployeeInput are POJO classes that don't have any relationship. So I guess these methods I mentioned would then take "Object" parameters?
EmployeeLambda and JobLambda are again classes with no relationship between them. Another concern I had towards creating some sort of inheritance was that, Employee Lambda and JobLambda cannot be used inter-changeably. So was wondering if they should inherit the same parent class just because they're both lambda classes.
OR is there another way to go about this? Any advice would be much appreciated. Thank you very much in advance.
As promised yesterday, here is what I would do.
Create a Lambda interface and make JobLambda and EmployeeLambda implement it
public interface Lambda {
Map<String, String> invokeLambda(String payload);
}
public class JobLambda implements Lambda {
//... your implementation
}
public class EmployeeLambda implements Lambda {
//... your implementation
}
Do the same for DataPersistence
public interface DataPersistence {
boolean doesIndexExist(String indexName);
void createIndex(CreateIndexInput createIndexInput);
int indexDocument(String indexName, String documentToIndex);
SearchDocumentResult searchDocument(String indexName, Map<String, String> payloadMap);
}
public class MyDataPersistence implements DataPersistence {
//... your implementation
}
public class MyTestDataPersistence implements DataPersistence {
//... your implementation
}
Then create a parent class PersistenceManager which contains all the duplicated methods, parametrized for the type of input/output:
(Note: I didn't complete everything, but I did something just to make you understand the concept)
public class PersistenceManager<I, O> {
protected static final String ID = "Id";
protected static final String NAME = "Name";
private String indexName;
private Lambda lambda;
private DataPersistence dataPersistence;
private DataProcessorUtils dataProcessorUtils;
public PersistenceManager(String indexName, Lambda lambda, DataPersistence dataPersistence, DataProcessorUtils dataProcessorUtils) {
this.indexName = indexName;
this.lambda = lambda;
this.dataPersistence = dataPersistence;
this.dataProcessorUtils = dataProcessorUtils;
createIndexIfNotExists(indexName);
}
public SearchDocumentResult search(I input) {
return dataPersistence.searchDocument(indexName, dataProcessorUtils.transformObjectDataPayloadToMap(input));
}
public O invokeCreateLambdaAndIndexData(final I input) {
String requestPayload = dataProcessorUtils.transformObjectDataInputJson(input);
Map<String, String> createdOutput = this.lambda.invokeLambda(requestPayload);
//continue generalizing following the same logic
}
public int indexCreated(I input, Map<String, String> createdOutput) {
//continue generalizing following the same logic
}
private void createIndexIfNotExists(final String indexName) {
if (!dataPersistence.doesIndexExist(indexName)) {
dataPersistence.createIndex(CreateIndexInput.builder().indexName(indexName).build());
}
}
}
At this point, you can specialize your classes by simply choosing the parameters
... all the rest of the code will be shared in the parent class.
public class JobPersistenceManager extends PersistenceManager<MyJobInput, MyJobOutput> {
private static final String JOB_ID = "Job" + ID;
private static final String JOB_NAME = "Job" + NAME;
public JobPersistenceManager(String indexName, Lambda lambda, DataPersistence dataPersistence, DataProcessorUtils dataProcessorUtils) {
super(indexName, lambda, dataPersistence, dataProcessorUtils);
}
}
public class EmployeePersistenceManager extends PersistenceManager<MyEmployeeInput, MyEmployeeOutput> {
private static final String EMPLOYEE_ID = "Employee" + ID;
private static final String EMPLOYEE_NAME = "Employee" + NAME;
public EmployeePersistenceManager(String indexName, Lambda lambda, DataPersistence dataPersistence, DataProcessorUtils dataProcessorUtils) {
super(indexName, lambda, dataPersistence, dataProcessorUtils);
}
}
... and use them like this:
PersistenceManager employeePersistenceManager = new EmployeePersistenceManager(...);
employeePersistenceManager.search(employeeInput); //<-- the code is in the base class

Mock returns Wrong Collection

I want to return a filled Map with my mocked Object, but the size of the Map is always Null. The mocked Object "CommandLineValues options" is not Null and also the Boolean variable "doCleanFirst" I can mock successfully.
Here is my Testclass:
#RunWith(MockitoJUnitRunner.class)
public class IndexBMECatTest {
#InjectMocks
private IndexBMECat classUnderTest;
#Mock
private CommandLineValues options;
#Test
public void testAccessoryItemHasNoDublicates() {
Map<String, String> testMap = new HashMap<>();
testMap.put("key", "value");
when(options.getCleanFirst()).thenReturn(false);
when(options.readWhitlist()).thenReturn(testMap);
classUnderTest.run();
}
}
Here is the constructor of my class where the code start, the tested Method is not relevant:
private boolean doCleanFirst;
private Map<String, String> whiteList;
public IndexBMECat(TransportClient client, CommandLineValues options, BMECatReader reader) throws Exception {
this.doCleanFirst = options.getCleanFirst();
this.whiteList = options.readWhitlist();
if (whiteList.isEmpty()) {
throw new Exception("Missing whiteList");
}
}
I also tried other variants:
Mock the Map and the return value of the method "isEmpty"
Initialize the Testclass and give the mocked Object to the constructor
But the whiteList has always the size = 0
Thx, this works now:
private IndexBMECat classUnderTest;
#Mock
private CommandLineValues options;
#Mock
private BMECatReader reader;
#Mock
TransportClient client;
#Before
public void setUp() throws Exception {
Map<String, String> testMap = new HashMap<>();
testMap.put("key", "value");
when(options.getCleanFirst()).thenReturn(false);
when(options.readWhitlist()).thenReturn(testMap);
classUnderTest = new IndexBMECat(client, options, reader);
}
#Test
public void testAccessoryItemHasNoDublicates() {
classUnderTest.run();
}
First I mock the methods which will be executed in the contructor and then I create the instance of my testclass.

How to pass hashmap as payload in RES and how to test it through POSTMAN

I have a rest API with the following URL
#PostMapping(path = "/Employees/employees")
private ResponseEntity<Map<String, BigDecimal>> availabilityCalculator(#RequestBody ReqOb req, Map<String, BigDecimal> testMap) {}
what annotation should I use for the map(like RequestBody for Object). Can I use RequestBody itself considering the map is also a type of object? 2. How should I pass - a hashmap and an object as payload for testing it through POSTMAN
Create a class that will contain that map and pass it like that. For example:
public class CalculationStatsDto {
private Map<String, BigDecimal> testMap;
public CalculationStatsDto () {
}
public Map<String, BigDecimal> getTestMap() {
return testMap;
}
public void setTestMap(Map<String, BigDecimal> testMap) {
this.testMap = testMap;
}
}
And your rest method should be:
#PostMapping(path = "/Employees/employees")
private ResponseEntity<CalculationStatsDto> availabilityCalculator(#RequestBody
CalculationStatsDto calculationStatsDto) {}
And if you need also that 'ReqOb req' in the request body then you can put it in your entity class:
public class CalculationStatsDto {
private Map<String, BigDecimal> testMap;
private ReqOb req;
public CalculationStatsDto() {
}
public Map<String, BigDecimal> getTestMap() {
return testMap;
}
public void setTestMap(Map<String, BigDecimal> testMap) {
this.testMap = testMap;
}
public ReqOb getReq() {
return req;
}
public void setReq(ReqOb req) {
this.req = req;
}
}
With the last one you will wrap both of it in one request body.

Categories