Layout the same instruction twice

I have a Java method with the following statement:

public void someMethod() {
  .....
  Long firstVal = someService.getSomeObject().getId();
  Long secondVal = someService.getSomeObject().getNextFunc().getOtherObject().getId();
  .....
}

      

Now I am trying to test this method and in the mock setup I am trying to do, for example:

@RunWith(MockitoJUnitRunner.class)
public class SomeServiceTest {
  @Mock SomeService mockSomeService;
  SomeObject someObject = new SomeObject();

  @Before
  public void setup() {
    someObject.setId(123456);
    when(mockSomeService.getSomeObject).thenReturn(someObject);
    //...
  }
  //...
}

      

Now how can I mock secondVal?

+3


source to share


1 answer


When you customize the layout, you provide it with (let's say) a story. You say what kind of action you expect from him. Thus, you can create two instances of SomeObject and set up calls to different methods. This will work if it is the same method.

I am changing my code:



  SomeObject someObject1 = new SomeObject();
  SomeObject someObject2 = new SomeObject();

  @Before
  public void setup() {
    someObject1.setId(123456);
    someObject2.setId(123457);
    when(mockSomeService.getSomeObject).thenReturn(someObject1);
    when(mockSomeService.getSomeObject.getNextFunc.getOtherObject).thenReturn(someObject2);
    //...
  }
  //...
}

      

+1


source







All Articles