【问题标题】:Mock a method within another method in mockmvc在 mockmvc 中的另一个方法中模拟一个方法
【发布时间】:2019-05-06 17:00:18
【问题描述】:

我是 MockMVC 和 junit 的新手,所以需要帮助,所以情况是我需要模拟一个方法,同时模拟包含该方法的外部方法。例如方法calculator(int a, int b),我用两个模拟值模拟这个方法,在这个方法中有另一个方法可以做一些其他的验证(比如说一些外部验证)。

我可以模拟主方法计算器,直到调用这个外部方法,我习惯用“Given().willReturn()”模拟主方法(计算器),另一个“Given( ).willReturn()" 语句来模拟包含方法(验证之一),但这给了我 nullPointer 异常。所以我需要一些可以帮助我解决这个问题的东西,以便可以有条不紊地进行模拟。

     public GuestRegistrationResponse registerGuest(SomeObject guestRegistrationRequest) throws Exception {

    SomeObject guestRegistrationResponse = new SomeObject();
    Folio folio = new Folio();

    folio.setForename(guestRegistrationRequest.getForename());
    folio.setEmail1(guestRegistrationRequest.getEmail());
    folio.setCity(guestRegistrationRequest.getCity());


    Result result = null;
    result = (Result) sampleAPICall.executeExternalAPI(result,new Class[] { SomeObject.class, User.class, Another.class, Folio.class, FolioList.class },
            Result.class);
    if (result != null && result.getFolioList() != null ) {
        guestRegistrationResponse.setFolioid(result.getFolioList().getFolio().getFolioId());

    } else {
        throw new ExternalException(result.getResult());
    }
    return guestRegistrationResponse;
}

测试方法

    @RunWith(SpringRunner.class)
 @WebMvcTest(value = ServiceImpl.class, secure = true)
 public class TestServiceImpl {

@Autowired
MockMvc mockmvc;

@InjectMocks
ServiceImpl serviceImpl;

@Before
public void setUp() {
    // We would need this line if we would not use MockitoJUnitRunner
    MockitoAnnotations.initMocks(this);
    // Initializes the JacksonTester
    JacksonTester.initFields(this, new ObjectMapper());
}

@Test
public void testRegisterGuest() throws Exception {

        SomeObject mockInput = new SomeObject();

        mockInput.setCity("CITY");
            mockInput.setEmail("test@test.com");
    mockInput.setForename("FORENAME");
    /* other datat is also collected*/
    ExpectedResponse mockOutput = new ExpectedResponse();
    mockOutput.setResult(true);

    Result Result = new Result();
    Result.setMessage("success");
    Result.setStatus(true);
    processTestThatMethod(mockInput, mockOutput,  Result);
}

private void processTestThatMethod(SomeObject mockInput, ExpectedResponse mockOutput,
         ,Result result) throws Exception {
    System.out.println("Inside processTestThatMethod");
     given(serviceImpl.registerGuest(mockInput)).willReturn(mockOutput);
         // what to do below ..
             given(sampleAPICall.executeExternalAPI(any(Result.class),any(new Class[]{SomeObject.class, User.class, Another.class, Folio.class, FolioList.class}),any(Result.class))).willReturn(result);

}
}

已编辑代码@Sachin rai

【问题讨论】:

  • 在发布代码之前至少要花一些精力来格式化代码
  • 对不起 Deadpool ,它不是一个正确的代码,它只是展示逻辑的一些示例,这就是为什么将它添加为语句的原因。感谢您的投票。
  • 不,我不是反对者@david
  • 开个玩笑@Pool ,请不要投票的人回答我的问题,至少一个小线索或链接可以帮助我解决我的问题。 :)

标签: java spring-boot junit mockmvc


【解决方案1】:
@Mock
    private class object;
    @Test
     public void testCalculator() {

      given(object.calculator(2,3)).willreturn(1);
      // remove the following line as when validate method will be called it will itself return true assuming it has simple check for positive value a and not calling any other method inside it
      given(class.validate(2)).willReturn(true); 
      //asserts here //
      }

【讨论】:

  • 嘿,我尝试了这种方法,但问题是当第一个“给定”语句被执行时,它在“if(big.getResult()!=true”之前覆盖了“calculator()”方法) {" 它给出了 Nullpointer 异常,因为变量 big 实际上是 null !!所以在某种程度上,我的第二个“给定”声明没有机会参与其中。
  • 你能展示你的validate(a)方法吗?如果您的validate 方法返回big 的实例,将其Result 值设置为truefalse ,那么big.getResult() 将不会抛出空指针异常。
  • 嘿sachin,validate(a) 方法返回一个“Result”对象,其中包含“result”和“status”作为getter/setter。
  • 已经编辑了代码,如果你发现任何遗漏,请期待它出现在代码中,因为代码真的很长,并且为了有意义/逻辑而缩短了它。@sachin
  • 注:第二个给定的没有机会被执行,因为第一个调用该方法并卡在服务中的 if 条件。
猜你喜欢
  • 1970-01-01
  • 2019-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-09
  • 1970-01-01
相关资源
最近更新 更多