java - 在 mockmvc 中的另一个方法中模拟一个方法
问题描述
我是 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
解决方案
@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 //
}
推荐阅读
- qt - 创建单个 ui.qml 文件的标准(Qt)方法是什么?
- javascript - javascript - 检查函数执行计数并使其执行不超过一次
- php - 如何使用数据提供程序测试一个键在多维数组的不同级别不存在?
- sql - Case When 语句的 Else 部分是否在 PL/SQL 中被忽略?
- html - 从 Firebase 中删除行数据
- spring - 如何在spring webflux中重定向请求?
- css - angular.json css 样式设置和组件 css 设置之间的冲突
- ios - Swift:添加项目时更改 UICollectionView 中单元格布局的宽度
- neo4j - 删除 neo4j 中的 MIN 计数数据
- java - 反应式 - 混合阻塞和异步请求