특정 코드를 조롱하는 방법을 결정하는 데 문제가 있습니다.Jock을 조롱 된 메서드 내에서 조롱 된 메서드를 테스트하는 방법
여기 내 방법이다 : 나는 findNotifications 호출하는지 테스트하기 위해 JMock을 사용할 수 있도록하고 싶습니다
public void sendNotifications(NotificationType type, Person person)
{
List<Notification> notifications = findNotifications(type);
for(Notification notification : notifications)
{
notification.send(person);
}
}
그와 호출되는 기대 값과 그 전송()를 반환합니다.
findNotifications는 조롱 된 나의 Dao를 호출합니다. 알림은 추상 클래스입니다.
이런 단위 테스트가 있지만 분명히 작동하지 않습니다. 처음 두 가지 기대는 만족하지만 더 이상은 만족하지 않습니다.
@Test
public void testSendNotifications2()
{
final Person person = new Person();
notificationEntries.add(createEntry1);
Mockery context = new JUnit4Mockery() {{ setImposteriser(ClassImposteriser.INSTANCE); }};
final NotificationDao dao = context.mock(NotificationDao.class, "notificationDao");
final Notification mockedNotification = context.mock(V2ADTNotification.class, "mockNotification");
notifications.add(mockedNotification);
final NotifierServiceImpl mockedService = context.mock(NotifierServiceImpl.class, "mockedService");
//NotifierService service = new NotifierServiceImpl(dao);
context.checking(new Expectations() {
{
one(mockedService).setDao(dao);
one(mockedService).sendNotifications(NotificationType.CREATE, person);
one(mockedService).findNotifications(NotificationType.CREATE);
one(dao).getByNotificationType(NotificationType.CREATE);
will(returnValue(notificationEntries));
will(returnValue(notifications));
one(mockedNotification).send(person);
}
});
mockedService.setDao(dao);
mockedService.sendNotifications(NotificationType.CREATE, person);
context.assertIsSatisfied();
}
mockedService.sendNotifications(NotificationType.CREATE, person);
context.assertIsSatisfied();
}
내가 원하는대로 작동하도록하려면 어떻게해야합니까?
다른 방법으로 시도했습니다. 첫 번째 두 가지 기대는 만족하지만, 두 번째 기대는 만족하지 않습니다.
@Test
public void testSendNotifications()
{
final Person person = new Person();
notificationEntries.add(createEntry1);
Mockery context = new JUnit4Mockery() {{ setImposteriser(ClassImposteriser.INSTANCE); }};
final NotificationDao dao = context.mock(NotificationDao.class, "notificationDao");
final Notification mockedNotification = context.mock(V2ADTNotification.class, "mockNotification");
NotifierService service = new NotifierServiceImpl(dao);
context.checking(new Expectations() {
{
one(dao).getByNotificationType(NotificationType.CREATE);
will(returnValue(notificationEntries));
one(mockedNotification).send(person);
}
});
service.sendNotifications(NotificationType.CREATE, person);
context.assertIsSatisfied();
}
나는 내가 뭘하는지에 대한 단서의 대부분을 가지고있는 것처럼 보이게하지 않는 경우 그래서 사과 Jmock을 사용하는 아주 새로운 해요 (난 몰라).
무언가를 놓친다면 나는 많이 알았지 만 확실히 알지 못했습니다. 나는 다른 길을 택했다. findNotifications 메소드 내에서 정적 팩터 리를 사용하고있었습니다. 나는 factory를 정적 인 것이 아니라 주입 가능한 클래스로 만들기로 결정했다. 그래서 지금 나는 그것을 조롱하고있다. 그리고 나는 내가 원하는 행동을 테스트 할 수있다. (그렇지 않으면 내가 조롱 한 Notification 객체를 얻을 수 없었을 것이다. 단지 조롱 된 NotificationEntry 객체 - 아마 더 많은 코드를 포함해야합니다.) 내 문제를 해결하기 때문에 답을 올바르게 표시 할 것이고이 논평에서 내 해결책을 설명했습니다. – AHungerArtist