Model { @VisibleForTesting static <T> Optional<BoundedContextName> findContext(Class<? extends T> rawClass) { ClassName clazzName = ClassName.of(rawClass); try { return knownContexts.get(clazzName, () -> { Optional<BoundedContextName> result; PackageInfo pkg = PackageInfo.of(rawClass); Optional<BoundedContext> annotation = pkg.findAnnotation(BoundedContext.class); if (!annotation.isPresent()) { result = Optional.empty(); } else { String nameAsString = annotation.get() .value(); BoundedContextName contextName = BoundedContextNames.newName(nameAsString); result = Optional.of(contextName); } return result; }); } catch (ExecutionException e) { throw illegalStateWithCauseOf(e); } } private  Model(BoundedContextName context); static synchronized Model inContextOf(Class<? extends T> rawClass); BoundedContextName contextName(); @VisibleForTesting static synchronized void dropAllModels();  }
@Test @DisplayName("find BoundedContext package annotation") void findBoundedContextAnnotation() { Optional<BoundedContextName> ctx1 = Model.findContext(Counter.class); Optional<BoundedContextName> ctx2 = Model.findContext(Encounter.class); assertTrue(ctx1.isPresent()); assertEquals("Counting", ctx1.get() .getValue()); assertEquals(ctx1, ctx2); }