如何模拟在使用 JMockit 测试的方法中创建的本地对象

How to mock a local object being created in method being tested using JMockit

我无法模拟 Class 的实例,该实例正在我尝试测试的方法中创建。下面是一个例子来说明这个问题。

Class 正在测试的方法:

// 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
  }
}

测试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(); 

    // ... 
 }
}

我模拟了 SomeOtherClass 及其方法,但运气不好,不确定使用 JMockit 模拟它的正确方法。

SomeOtherClass & its method addObject

尽管我在测试中模拟了它class,但它在要测试的方法中被清除了。我发现有人问过类似的问题 HERE,但该解决方案使用了其他一些单元测试框架 Mockito。我正在努力寻找使用 JMokcit 的类似解决方案。任何人都可以帮助我找到解决方案吗?

下面是我更新的代码示例

Class 正在测试的方法:

// 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;
}
}

测试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");
}
}

在尝试和分析问题后,我发现了问题所在。我将在答案部分更新解决方案,以便对其他人也有帮助。

谢谢。

您只需使用 @Mocked 即可。查看 JMockit tutorial 或 API 文档,有很多示例。

再次尝试调试后发现问题所在。我做错的是模拟接口(IService),因此它不起作用。当我将其更改为模拟已实现的 class (OtherService) 时,它可以正常工作。

所以在测试中 class 我替换了

@Mocked IService otherService;

@Mocked OtherService otherService;

此更改后我的问题得到解决。

谢谢