| 1 | package fr.sii.ogham.testing.assertion.internal.matcher; | |
| 2 | ||
| 3 | import org.hamcrest.BaseMatcher; | |
| 4 | import org.hamcrest.Description; | |
| 5 | import org.springframework.beans.BeansException; | |
| 6 | import org.springframework.context.ApplicationContext; | |
| 7 | ||
| 8 | /** | |
| 9 | * Matcher that checks if the tested object is the same instance as the bean | |
| 10 | * registered in Spring context. | |
| 11 | * | |
| 12 | * <p> | |
| 13 | * If no bean is declared, then it will fail. | |
| 14 | * | |
| 15 | * <p> | |
| 16 | * If a bean is declared but not same instance, then it will fail. | |
| 17 | * | |
| 18 | * | |
| 19 | * @author Aurélien Baudet | |
| 20 | * | |
| 21 | * @param <T> | |
| 22 | * Type of the tested object | |
| 23 | */ | |
| 24 | public class IsSpringBeanInstance<T> extends BaseMatcher<T> { | |
| 25 | private final ApplicationContext context; | |
| 26 | private final Class<?> beanClass; | |
| 27 | private boolean beanExists; | |
| 28 | ||
| 29 | /** | |
| 30 | * This matcher requires the Spring context and which bean to retrieve. It | |
| 31 | * is helpful to avoid handling {@link BeansException} when bean is not | |
| 32 | * registered at all. | |
| 33 | * | |
| 34 | * @param context | |
| 35 | * the Spring application context | |
| 36 | * @param beanClass | |
| 37 | * the bean to retrieve | |
| 38 | */ | |
| 39 | public IsSpringBeanInstance(ApplicationContext context, Class<?> beanClass) { | |
| 40 | super(); | |
| 41 | this.context = context; | |
| 42 | this.beanClass = beanClass; | |
| 43 | } | |
| 44 | ||
| 45 | @Override | |
| 46 | public boolean matches(Object item) { | |
| 47 | try { | |
| 48 | Object bean = context.getBean(beanClass); | |
| 49 | beanExists = true; | |
| 50 |
5
1. matches : replaced boolean return with true for fr/sii/ogham/testing/assertion/internal/matcher/IsSpringBeanInstance::matches → NO_COVERAGE 2. matches : replaced boolean return with true for fr/sii/ogham/testing/assertion/internal/matcher/IsSpringBeanInstance::matches → SURVIVED 3. matches : negated conditional → NO_COVERAGE 4. matches : negated conditional → KILLED 5. matches : negated conditional → KILLED |
return bean == item; |
| 51 | } catch (BeansException e) { // NOSONAR | |
| 52 | beanExists = false; | |
| 53 |
3
1. matches : replaced boolean return with true for fr/sii/ogham/testing/assertion/internal/matcher/IsSpringBeanInstance::matches → NO_COVERAGE 2. matches : replaced boolean return with true for fr/sii/ogham/testing/assertion/internal/matcher/IsSpringBeanInstance::matches → TIMED_OUT 3. matches : replaced boolean return with true for fr/sii/ogham/testing/assertion/internal/matcher/IsSpringBeanInstance::matches → KILLED |
return false; |
| 54 | } | |
| 55 | } | |
| 56 | ||
| 57 | @Override | |
| 58 | public void describeTo(Description description) { | |
| 59 |
1
1. describeTo : negated conditional → NO_COVERAGE |
description.appendText(beanExists ? "bean not registered" : "bean registered but not same instance"); |
| 60 | } | |
| 61 | ||
| 62 | } | |
Mutations | ||
| 50 |
1.1 2.2 3.3 4.4 5.5 |
|
| 53 |
1.1 2.2 3.3 |
|
| 59 |
1.1 |