gpt4 book ai didi

java - Junit集成测试

转载 作者:行者123 更新时间:2023-12-02 12:02:33 26 4
gpt4 key购买 nike

我需要帮助为 NotificationHandler 类编写单元测试。所以我做了 NotificationHandlerTest (使用 junit4),但我不知道如何确定我应该期望的结果与实际结果是什么,所以一个或多个简单的测试(对于它的一些方法)会对我有很大帮助!

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.annotation.Poller;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
class NotificationHandler {

private static Logger LOG = LoggerFactory.getLogger(NotificationHandler.class);

@Autowired
private NotificationRoutingRepository routingRepository;

@Autowired
private SendNotificationGateway gateway;

@Autowired
private AccessService accessService;

@Autowired
private EndpointService endpointService;

@ServiceActivator(inputChannel = Channels.ASSET_MODIFIED_CHANNEL, poller = @Poller("assetModifiedPoller"), outputChannel = Channels.NULL_CHANNEL)
public Message<?> handle(Message<EventMessage> message) {
final EventMessage event = message.getPayload();

LOG.debug("Generate notification messages: {}, {}", event.getOriginType(), event.getType());

routingRepository.findByOriginTypeAndEventType(event.getOriginType(), event.getType()).stream()
.filter(routing -> routing.getOriginId() == null || routing.getOriginId() == event.getOriginId())
.map(routing -> getNotificationMessages(event, routing))
.flatMap(List::stream)
.forEach(notificationMessage -> {
LOG.debug("Sending message {}", notificationMessage);
gateway.send(notificationMessage);
});

return message;
}enter code here
enter code here`enter code here`
private List<NotificationMessage> getNotificationMessages(EventMessage event, NotificationRouting routing) {
switch (routing.getDestinationType()) {
case "USERS":
LOG.trace("Getting endpoints for users");
return getEndpointsByUsers(routing, event.getOrigin(), event.getOriginType()).stream()
.map(endpoint -> new NotificationMessage(event.getOriginType(), event.getOrigin(), endpoint))
.collect(Collectors.toList());
default:
LOG.trace("Getting default endpoints");
return getEndpoints(routing, event.getOrigin(), event.getOriginType()).stream()
.map(endpoint -> new NotificationMessage(event.getOriginType(), event.getOrigin(), endpoint))
.collect(Collectors.toList());
}
}

private List<Endpoint> getEndpoints(NotificationRouting routing, Object origin, String originType) {
final Asset asset = getAssetForObject(origin, originType);

final List<Long> userIds = accessService.list(asset).stream()
.map(ResourceAccess::getUser)
.map(AbstractEntity::getId)
.collect(Collectors.toList());

userIds.add(asset.getCreatorId());

LOG.trace("getEndpoints usersIds {}", userIds);

final List<Endpoint> endpoints = endpointService.getEndpoints(userIds, routing.getEndpointType());
LOG.trace("Endpoints {}", endpoints.stream().map(Endpoint::getId).collect(Collectors.toList()));
return endpoints;
}

private List<Endpoint> getEndpointsByUsers(NotificationRouting routing, Object origin, String originType) {
final Asset asset = getAssetForObject(origin, originType);

final List<Long> userIds = accessService.list(asset).stream()
.map(ResourceAccess::getUser)
.map(AbstractEntity::getId)
.filter(routing.getDestinations()::contains)
.collect(Collectors.toList());

routing.setDestinations(userIds);
routingRepository.save(routing);

LOG.trace("getEndpointsByUsers usersIds {}", userIds);

final List<Endpoint> endpoints = endpointService.getEndpoints(userIds, routing.getEndpointType());
LOG.trace("Endpoints {}", endpoints.stream().map(Endpoint::getId).collect(Collectors.toList()));
return endpoints;
}

private Asset getAssetForObject(Object origin, String originType) {
switch (originType) {
case EventMessage.POINT:
return (Point) origin;
case EventMessage.FEED:
return ((Feed) origin).getPoint();
case EventMessage.ACTUATOR:
return ((Actuator)origin).getPoint();
case EventMessage.DEVICE:
return (Device) origin;
case EventMessage.ALARM:
return ((Alarm) origin).getPoint();
default:
throw new IllegalArgumentException("Unsupported type: " + originType);
}
}

}

最佳答案

如果您不确定要测试什么,我建议您从一个简单的测试开始。一项测试可验证如果您发送 null 作为参数,则不会出现任何异常。

例如

@Test
public void shouldNotThrowAnyExceptionIfArgumentIsNull() {
// given
NotificationHandler handler = new NotificationHandler();
// when
handler.handle(null);
// then no exception is thrown.
}

之后,您可以逐行分析方法handle正在做什么,并编写测试来验证其行为。

例如,您可以根据您在参数中发送的内容来验证方法 gateway.send(...); 是否已执行。

对于依赖模拟和行为验证,我建议您使用 mockito 或类似的工具。您可以关注this教程以了解如何操作。

关于java - Junit集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47138065/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com