I have a dilemma trying to solve the following test. The class is a toggle created through the Togglz library. I'm catching the feature manager method execution because I'm using a JDBCStateReporsitory to read the toggle value and if something goes wrong with the DB I have to be able to return the default value of the toggle with the @EnabledByDefault
annotation.
@Slf4j
public enum PocToggle {
@EnabledByDefault
USE_MY_FEATURE;
public boolean isActive() {
FeatureManager featureManager = FeatureContext.getFeatureManager();
try {
return featureManager.isActive(this);
} catch (RuntimeException ignored) {
if (log.isWarnEnabled()) {
log.warn(String.format("Failed to retrieve feature '%s' state", this.name()));
}
FeatureMetaData metaData = featureManager.getMetaData(this);
FeatureState featureState = metaData.getDefaultFeatureState();
return featureState.isEnabled();
}
}
}
I have no clue of how to do it because an object created by an inner utility static method doesn't allow me to Stub or Mock it. I just created the true and false paths of the test, but the test trying to cover the exception path is not working throwing me a Expected exception of type 'java.lang.IllegalStateException', but no exception was thrown
message.
class PocToggleSpecification extends Specification {
@Rule
private TogglzRule toggleRule = TogglzRule.allEnabled(PocToggle.class)
def "Should toggle to use my feature when it is enabled"() {
when:
toggleRule.enable(USE_MY_FEATURE)
then:
USE_MY_FEATURE.isActive()
}
def "Should toggle to not to use my feature when it is disabled"() {
when:
toggleRule.disable(USE_MY_FEATURE)
then:
!USE_MY_FEATURE.isActive()
}
def "Should throw an exception when something goes wrong"() {
given:
toggleRule.enable(USE_MY_FEATURE)
FeatureManager featureManager = Stub()
featureManager.isActive() >> { throw new IllegalStateException() }
def featureContext = Spy(FeatureContext)
featureContext.getFeatureManager() >> featureManager
when:
USE_MY_FEATURE.isActive()
then:
thrown IllegalStateException
}
}
Could you please help me to solve this kind of test?
Aucun commentaire:
Enregistrer un commentaire