简体   繁体   English

如何模拟从测试类的另一种方法获得的局部变量?

[英]How to mock local variable obtained from another method of tested class?

I have following class我有以下课程

class MyClass{
   public void m(InputStream is){
       ...
       Parser eParser = getExcelFileParser();
       eParser.parse(is);
       ...
       eParser.foo();
       eParser.bar();

   }
   public ExcelFileParser getExcelFileParser(){
       ...
   } 
}

How to write unit test for method m at this situation?在这种情况下如何为方法m编写单元测试? I want to mock eParser object only.我只想模拟eParser对象。

Is it possible?是否可以?

I use Mockito and PowerMockito我使用 Mockito 和 PowerMockito

You can do what you want in Mockito (no PowerMock needed) using a spy without changing your code at all.您可以使用spy在 Mockito 中执行您想要的操作(不需要 PowerMock),而根本无需更改您的代码。

In your unit test you need to do something like the following:在您的单元测试中,您需要执行以下操作:

ExcelFileParser parser = mock(ExcelFileParser.class);
MyClass myClass = spy(new MyClass());
doReturn(parser).when(myClass).getExcelFileParser();

您可以将 AnotherObject 作为参数传递给方法 m 而不是在方法本身中调用 getAnotherObject() 吗?

Preface: I use EasyMock not Mockito so this may be a bit off.前言:我使用的是 EasyMock 而不是 Mockito,所以这可能有点偏离。

Can't you create an inner subclass of MyClass in your test that overrides getExcelFileParser and has it return a mock?你不能在你的测试中创建一个 MyClass 的内部子类来覆盖 getExcelFileParser 并让它返回一个模拟吗? Like this:像这样:

public class MyClassMock extends MyClass {

     ExcelFileParser _mock;

     public MyClassMock(ExcelFileParser mock) {
          _mock = mock;
     }

     @Override
     public ExcelFileParser getExcelFileParser() {
         return _mock;
     }
}

I haven't tested this so there could be issues with this, but the basic idea should be right.我还没有测试过这个所以可能会有问题,但基本的想法应该是正确的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用powermock-easymock从正在测试的方法中模拟另一个类方法调用? - How to mock another class method call from the method being tested using powermock-easymock? 如何模拟正在测试的同一个 class 中的另一个方法? - How to mock another method in the same class which is being tested? 如何在要测试的类中模拟Abstract类方法 - How to mock Abstract class method in a class to be tested 如何模拟正在被同一类测试的另一个方法内部调用的类的方法? - How to mock a method of a class being called inside another method that is being tested of the same class? 如何从一个类访问方法的局部变量到另一个类 - How to Access local variable of the method from one class to another 如何在私有 Java 类的静态方法中模拟局部变量? - How to mock local variable in a static method of a private java class? 在正在测试的同一个类中模拟私有方法 - Mock private method in the same class that is being tested 如何通过从查询获得的结果中以字符串形式获取方法名称来从另一个类中调用Java中的方法 - how to invoke a method in java from another class by obtaining the method name as a string from the result obtained from a query 我应该在经过测试的方法中模拟本地方法调用吗? - Should I mock local method invocation inside a tested method? MockK:如何模拟在另一个被测方法中调用的已测试方法(Kotlin) - MockK: How to mock an already tested method called inside another method under test (Kotlin)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM