I unable to mock an instance of a Class which is being created inside the method which I am trying to test. Below is an example to illustrate the issue.
Class and Method Being tested:
// class being used in the class to be tested public class SomeOtherClass{ public ResponseObject addObject(Request dtoObject){ /// Some business logic goes here return reponseObject; } } // Class to be tested public class ClassToBeTested { public ClassToBeTested() {} public void myMethodToBeTested() { SomeOtherClass otherClassObject = new SomeOtherClass(); // Here I want mock the otherClassObject and also addObject method // Eventhoug I mocked it seems to me that as otherClassObject is being created here locally // I am unable to mock it. ReponseObject reponseObject = otherClassObject.addObject(dtoObject); // do some other stuff using the reponseObject } } Test Class:
public class TestClassToBeTested { @Tested private ClassToBeTested classBeingTested; @Injectable SomeOtherClass innerSomeOtherClass; RequestObject myRequestObject = new RequestObject(); myRequestObject.setSomevalue1("1"); myRequestObject.setSomevalue2("2"); ResponseObject myMockResponseObject = new ResponseObject(); myMockResponseObject.setResultCode(SUCCESS); @Test public void shouldTestSomething() { new NonStrictExpectations(){{ // Here I am returning the mocked response object. SomeOtherClass.addObject((SomeOtherClass)any); result =myMockResponseObject; }}; classBeingTested.myMethodToBeTested(); // ... } } I mocked the SomeOtherClass and its method but no luck, not sure the proper way to mock it using JMockit.
SomeOtherClass & its method addObject
Even though I mocked it in the Test class, but it gets cleared in the method to be tested. I found similar question being asked HERE, but the solution uses some other unit test framework Mockito. I am struggling to find similar solution using JMokcit. Could any one help me to find the solution for this?
Below is my updated code sample
Class and Method Being tested:
// This is the class to be tested public class MyMainClass { public ResponseObject addItem(ProductList products) { ResponseObject responseObject = new ResponseObject(); OtherService otherService = new OtherService(); List<Product> productList = new ArrayList<Product>(); productList = products.getProducts(); for (int i = 0; i < productList.size(); i++) { Product product = otherService.addProduct(productList.get(i)); System.out.println("model.Product " + product.getName() + " added successfully"); } responseObject.setResponseCode("1"); responseObject.setResponseMessage("Success"); return responseObject; } } // Some other service internally used by MyMainClass public class OtherService implements IService{ public Product addProduct(Product product){ // Some business logic to process the product System.out.println("Adding product :"+product.getName()); return product; } } // Interface implemented by OtherService public interface IService { Product addProduct(Product product); } // Sample ResponseObject class public class ResponseObject { String responseCode; String responseMessage; public String getResponseMessage() { return responseMessage; } public void setResponseMessage(String responseMessage) { this.responseMessage = responseMessage; } public String getResponseCode() { return responseCode; } public void setResponseCode(String responseCode) { this.responseCode = responseCode; } } Test Class:
public class MyMainClassTest { @Tested MyMainClass myMainClass; @Mocked IService otherService; private List<Product> myProducts = new ArrayList<Product>(); @Before public void init(){ Product product1 = new Product(); product1.setName("Test1"); product1.setPid(1); product1.setPrice(100.00); myProducts.add(product1); Product product2 = new Product(); product2.setName("Test2"); product2.setPid(2); product2.setPrice(200.00); myProducts.add(product2); } @Test public void addItem_Test() throws Exception{ myMainClass = new MyMainClass(); new NonStrictExpectations(){{ otherService.addProduct((Product)any); returns(myProducts.get(0)); }}; ProductList productList = new ProductList(); productList.setProducts(myProducts); ResponseObject responseObject = myMainClass.addItem(productList); Assert.assertEquals(responseObject.getResponseMessage(),"Success"); } } After trying and analyzing the problem I found the what went wrong. I will update the solution in the answer section so that it would be helpful others also.
Thanks.
SomeOtherClassinsidemyMethodToBeTested(), but create it at level above and send as parametermyMethodToBeTested(otherClassObject). In this case you'll be able to mock it in tests.