Skip to content

Commit e6c808d

Browse files
author
Rupert Westenthaler
committed
MORE-Platform#326: Fixed unit test after refactoring the applicationContext.getBean() does never return NULL but throws exceptions instead. This needed to be reflected in the mocks of unit tests!
1 parent 0a33bd6 commit e6c808d

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

studymanager-services/src/test/java/io/redlink/more/studymanager/service/ObservationServiceTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.mockito.InjectMocks;
3535
import org.mockito.Mock;
3636
import org.mockito.junit.jupiter.MockitoExtension;
37+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
3738
import org.springframework.context.ApplicationContext;
3839

3940
import static org.assertj.core.api.Assertions.assertThat;
@@ -61,10 +62,11 @@ class ObservationServiceTest {
6162

6263
@Test
6364
void testValidation() {
65+
when(applicationContext.getBean("not-existing-observation", ObservationFactory.class)).thenThrow(NoSuchBeanDefinitionException.class);
6466
NotFoundException notFoundException = Assertions.assertThrows(NotFoundException.class, () ->
65-
observationService.addObservation(new Observation().setStudyId(1L).setObservationId(1).setType("my-observation"))
67+
observationService.addObservation(new Observation().setStudyId(1L).setObservationId(1).setType("not-existing-observation"))
6668
);
67-
Assertions.assertEquals("ObservationFactory for Observation[study: 1, id:1, type: my-observation] cannot be found", notFoundException.getMessage());
69+
Assertions.assertEquals("ObservationFactory for Observation[study: 1, id:1, type: not-existing-observation] cannot be found", notFoundException.getMessage());
6870

6971
ObservationFactory factory = mock(ObservationFactory.class);
7072
when(factory.validate(any())).thenThrow(new ConfigurationValidationException(ConfigurationValidationReport.init().error("My error")));
@@ -152,7 +154,7 @@ void testGetObservationFactory_optional() {
152154
java.util.Optional<ObservationFactory> present = observationService.getObservationFactory(obs);
153155
org.assertj.core.api.Assertions.assertThat(present).containsSame(factory);
154156

155-
org.mockito.Mockito.when(applicationContext.getBean("x", ObservationFactory.class)).thenReturn(null);
157+
org.mockito.Mockito.when(applicationContext.getBean("x", ObservationFactory.class)).thenThrow(NoSuchBeanDefinitionException.class);
156158
java.util.Optional<ObservationFactory> empty = observationService.getObservationFactory(obs);
157159
org.assertj.core.api.Assertions.assertThat(empty).isEmpty();
158160
}

0 commit comments

Comments
 (0)