Can somebody help how to write unit tests for below code using JUnit/Mockito?
@FunctionName("nodeStats")
public void nodeStats(@TimerTrigger(name = "nodeStatsTrigger", schedule = "0 */5 * * * *", dataType = "string") String timerInfo,
final ExecutionContext context) {
getNodeStats();
}
The following docs might help:
https://code.visualstudio.com/docs/java/java-testing
A sample Test case gets automatically created for HTTP Triggered Function when you create a Java Function App:
@Test
public void testHttpTriggerJava() throws Exception {
// Setup
@SuppressWarnings("unchecked")
final HttpRequestMessage<Optional<String>> req = mock(HttpRequestMessage.class);
final Map<String, String> queryParams = new HashMap<>();
queryParams.put("name", "Azure");
doReturn(queryParams).when(req).getQueryParameters();
final Optional<String> queryBody = Optional.empty();
doReturn(queryBody).when(req).getBody();
doAnswer(new Answer<HttpResponseMessage.Builder>() {
@Override
public HttpResponseMessage.Builder answer(InvocationOnMock invocation) {
HttpStatus status = (HttpStatus) invocation.getArguments()[0];
return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status);
}
}).when(req).createResponseBuilder(any(HttpStatus.class));
final ExecutionContext context = mock(ExecutionContext.class);
doReturn(Logger.getGlobal()).when(context).getLogger();
// Invoke
final HttpResponseMessage ret = new Function().run(req, context);
// Verify
assertEquals(ret.getStatus(), HttpStatus.OK);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.