Skip to content

Add unit tests to raise code coverage across all modules - #10

Merged
NoahHsu merged 1 commit into
mainfrom
add-unit-tests-for-coverage
Aug 20, 2026
Merged

NoahHsu merged 1 commit into
mainfrom
add-unit-tests-for-coverage

Conversation

@fukuanru

Copy link
Copy Markdown
Collaborator

Summary

Raises the line coverage of the four published modules from 128/475 to 462/475 (97%) and the test count from 22 to 191. No production code is changed — this PR only adds tests and the build wiring needed to measure them.

Module Before After
flagship4j-javaflagr-core no coverage report, 2 tests 214/223 (96%)
flagship4j-openfeature-provider-javaflagr no tests at all 75/76 (99%)
flagship4j-javaflagr-spring-boot-web-starter 128/156 (82%) 153/156 (98%)
flagship4j-openfeature-spring-boot-starter no tests at all 20/20 (100%)

What is covered

  • DefaultOpenFlagr — every isFeatureFlagOn / getVariantKey / getVariantAttachment / evaluate overload, driven through MockWebServer, including the fallbacks for an unknown flag, an unassigned variant, a non-effective variant, a server error, a malformed body and a context that cannot be serialized.
  • EffectiveVariant — all on/true/1 and off/false/0 aliases, the blank and unknown value rejections, case-insensitive isValid, and the Jackson round trip.
  • OpenFlagrConfig — the hand-written builder overloads (String/URI endpoint, *Ms and *Seconds timeouts), plus the Lombok accessors.
  • OpenFlagrProvider — the boolean, string, integer, double and object evaluations, each with its success, parse-error and flag-not-found path, and the OpenFeature to OpenFlagr context translation.
  • FlagrEvalClientImpl — the non-200, empty-body, refused-connection and truncated-response failure branches.
  • BeanLocator, RandomUtils, SecureRandomUtils, FlagrEvalClientConfiguration, and the auto-configuration of the OpenFeature starter (bean registration, property binding, @ConditionalOnMissingBean back off).

Build changes

  • Apply the jacoco plugin to the three modules that did not have it, so every module reports coverage the same way the web starter already does.
  • Add junit-jupiter:5.10.2 and mockito 4.11.0 to the provider module, and spring-boot-starter-test to the OpenFeature starter. Mockito stays on 4.x because the project targets Java 8.

Notes for reviewers

Three things surfaced while writing these tests. None of them are fixed here, so that this PR stays test-only — happy to open follow-ups.

  1. EvaluationContextExtensionMethods never skips an unset targeting key. It guards with if (source.getTargetingKey() != null), but the OpenFeature SDK reports an unset targeting key as an empty string, so entityId=\"\" is always sent. keepsOnlyTheFlagKeyAndAnEmptyEntityIdWhenTheSourceContextIsEmpty pins the current behaviour with a comment explaining why.
  2. Two dead catch blocks. The OpenFlagrNoFlagKeyException / OpenFlagrNoVariantException / Exception handlers in DefaultOpenFlagr.evaluateByDefaultVariantKey and the Exception handler in FlagrEvalServiceImpl.evaluateByDefaultVariantKey are unreachable, because the inner getEvaluationResult / getFlagrVariantKey already swallow every exception. Those 12 lines are the entire remaining coverage gap; they were left alone rather than covered artificially.
  3. OpenFlagrProvider.getObjectEvaluation NPEs on a null variantAttachment (Structure.mapToStructure(null)). DefaultOpenFlagr's validation makes it unreachable today, but any other OpenFlagr implementation would hit it.

Branch coverage sits at 53% because almost everything missing is Lombok-generated equals/hashCode. Adding a root lombok.config with lombok.addLombokGeneratedAnnotation = true would make JaCoCo skip generated methods and turn that number into something meaningful — left out as a reporting-policy decision for the maintainers.

Test plan

  • ./gradlew clean build passes.
  • ./gradlew test --rerun-tasks run three times, no flakes.
  • Note: the build requires JDK 17 or lower locally. Gradle 7.4.2 rejects class file major 65 and Lombok 1.18.28 fails on JDK 21 with NoSuchFieldError: JCTree$JCImport.qualid. CI already uses JDK 8, so it is unaffected.

🤖 Generated with Claude Code

Line coverage of the four published modules goes from 128/475 to 462/475,
and the test count from 22 to 191.

- flagship4j-javaflagr-core: 96% (was measured by no coverage report at all,
  only 2 tests). Covers every DefaultOpenFlagr overload and its fallback
  behaviour, the EffectiveVariant aliases and Jackson mapping, the
  OpenFlagrConfig builder, the exceptions and the model wire format.
- flagship4j-openfeature-provider-javaflagr: 99% (had no tests). Covers the
  five evaluation types of OpenFlagrProvider, including the parse error and
  flag-not-found paths, and the OpenFeature context translation.
- flagship4j-javaflagr-spring-boot-web-starter: 82% -> 98%. Adds BeanLocator,
  the FlagrEvalClientImpl failure branches, the random utilities, the bean
  factory methods and the configuration properties.
- flagship4j-openfeature-spring-boot-starter: 100% (had no tests). Covers the
  auto-configuration beans, the property binding and the ConditionalOnMissingBean
  back off.

Build changes:
- Apply the jacoco plugin to the three modules that did not have it, so the
  coverage of every module is reported the same way as the web starter.
- Add junit-jupiter and mockito 4.11.0 to the provider module, and
  spring-boot-starter-test to the openfeature starter. Mockito is kept on 4.x
  because the project targets Java 8.

The only lines left uncovered are unreachable: the
OpenFlagrNoFlagKeyException / OpenFlagrNoVariantException / Exception catch
blocks in DefaultOpenFlagr.evaluateByDefaultVariantKey and the Exception catch
block in FlagrEvalServiceImpl.evaluateByDefaultVariantKey are dead code,
because the inner getEvaluationResult / getFlagrVariantKey already swallow
every exception.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

@NoahHsu NoahHsu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@NoahHsu
NoahHsu merged commit e76c0a7 into main Aug 20, 2026
2 checks passed
@NoahHsu
NoahHsu deleted the add-unit-tests-for-coverage branch August 20, 2026 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants