jMockit: How to expect constructor calls to Mocked objects?

后端 未结 1 418
无人共我
无人共我 2021-01-19 01:30

I am unit-testing a method performing some serialization operations. I intend to mock the serialization logic. The code is as below:

ObjectInputStream ois =          


        
相关标签:
1条回答
  • 2021-01-19 01:49

    You can specify a complete set of Expectations for a given set of interactions. From Behavior-based testing with JMockit:

    A possible test for the doSomething() method could exercise the case where SomeCheckedException gets thrown, after an arbitrary number of successful iterations. Assuming that we want (for whatever reasons) to record a complete set of expectations for the interaction between these two classes, we might write the test below:

    @Test
    public void doSomethingHandlesSomeCheckedException() throws Exception
    {
      new Expectations() {
         DependencyAbc abc;
    
         {
            new DependencyAbc(); // expect constructor
    
            abc.intReturningMethod(); result = 3;
    
            abc.stringReturningMethod();
            returns("str1", "str2");
            result = new SomeCheckedException();
         }
      };
    
      new UnitUnderTest().doSomething();
    }
    
    0 讨论(0)
提交回复
热议问题