<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/testing-your-auto-configuration-against-a-missing-optional-dependency-ohli3thgh" -->

---
title: Testing your auto-configuration against a missing...
description: A deep dive into a subtle bug where Spring Boot auto-configurations with optional dependencies can throw NoClassDefFoundError for users who lack that...
canonical: https://daily.dev/posts/testing-your-auto-configuration-against-a-missing-optional-dependency-ohli3thgh
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Testing your auto-configuration against a missing optional dependency | daily.dev
og:description: A deep dive into a subtle bug where Spring Boot auto-configurations with optional dependencies can throw NoClassDefFoundError for users who lack that...
og:url: https://daily.dev/posts/testing-your-auto-configuration-against-a-missing-optional-dependency-ohli3thgh
og:image: https://api.daily.dev/og/posts/OHLI3thGH.png
og:image:alt: Testing your auto-configuration against a missing optional dependency
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Testing your auto-configuration against a missing optional dependency

**[Wim Deblauwe](https://daily.dev/sources/wim-deblauwe)** · 9 min read · 1 upvotes · 3 comments

## Summary

A deep dive into a subtle bug where Spring Boot auto-configurations with optional dependencies can throw NoClassDefFoundError for users who lack that dependency, even when @ConditionalOnClass guards are used correctly. The root cause is that Java resolves method signatures for every declared method in a class when reflecting over it, before Spring's conditions get a chance to skip anything. The standard testing tool, FilteredClassLoader, cannot reproduce this failure because it never defines classes itself, so the application class loader (which has the dependency) still defines the auto-configuration class. A custom HidingClassLoader is presented that becomes the defining class loader for the classes under test while hiding selected classes, correctly reproducing the failure. The fix is to move optional-dependency-referencing beans into a nested @ConditionalOnClass-guarded configuration class, matching the pattern Spring Boot itself uses internally.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.wimdeblauwe.com/blog/2026/09/06/testing-your-auto-configuration-against-a-missing-optional-dependency>

## Questions this post answers

### Why doesn't FilteredClassLoader catch a NoClassDefFoundError caused by a missing optional dependency in a Spring Boot auto-configuration?

FilteredClassLoader only intercepts class loading by name and delegates everything else to its parent, so it never becomes the defining class loader of the configuration class itself. Since the JVM resolves method signatures using a class's defining class loader, the configuration class is still defined by the application class loader that has the dependency, so no error occurs and the test passes incorrectly.

_When a config test passes but production still breaks, daily.dev surfaces classloader debugging techniques like this one._

### Why does @ConditionalOnMissingBean fail with NoClassDefFoundError even on a bean unrelated to the missing optional dependency?

Evaluating @ConditionalOnMissingBean requires finding the bean method via ReflectionUtils.findMethod, which calls Class.getDeclaredMethods(), and that call resolves the signatures of every declared method in the class at once, not just the target one. If any other method in the same class references a type from a missing optional dependency, the whole reflection call throws NoClassDefFoundError, even though @ConditionalOnClass would have skipped that unrelated method.

_Spotting subtle Spring Boot auto-configuration bugs like this is easier with daily.dev feeding relevant deep dives._

### How do you fix a Spring Boot auto-configuration that crashes when an optional dependency like spring-web is missing?

Move any bean method that references a type from the optional dependency into a nested @Configuration class annotated with @ConditionalOnClass. Since Spring reads conditions from annotation metadata via ASM without loading the nested class, that class is never loaded when the dependency is missing, so its problematic method signatures are never resolved. The rule: no method signature in the top-level auto-configuration class may reference an optional dependency's types.

_Maintainers hardening Spring Boot starters against classpath gaps can track patterns like this via daily.dev._

## Community discussion

Top comments from developers on daily.dev.

**@agustinbarrientos** · 1 upvotes

> At seventy lines, I'd probably just copy the helper. Does `HidingClassLoader` really need to become another dependency?

**@leonidbugaev** · 0 upvotes

> FilteredClassLoader never defines GreeterAutoConfiguration, so getDeclaredMethods still resolves ClientHttpRequestInterceptor against the loader that has spring-web. I wouldn't trust that green test. Nested @ConditionalOnClass is what actually works, because the outer class never names the missing type.

**@nark3d** · 0 upvotes

> You are right that FilteredClassLoader misses this.  It never defines the classes, so it cannot reproduce the failure a real app class loader would hit.  The HidingClassLoader gets closer to what actually happens in production, where the auto-configuration class is defined by a loader that can see the optional dependency.
>
>
> The nested configuration fix mirrors what Spring Boot does internally, which is usually a good sign.  The outer class stays loadable, the inner one only loads when the dependency is present, and reflection never gets a chance to choke on the missing signature.
>
>
> Worth...

## Similar posts on daily.dev

- [The Bug That Made Me Question Reality for a Few Hours](https://daily.dev/posts/the-bug-that-made-me-question-reality-for-a-few-hours-ndxvzfmjj) · DEV · 157 upvotes · 12 comments
- [How Project Leyden brought a new perspective](https://daily.dev/posts/how-project-leyden-brought-a-new-perspective-q0c622lxb) · Quarkus · 22 upvotes · 0 comments
- [Improving First Request Latency in Java Spring Application](https://daily.dev/posts/improving-first-request-latency-in-java-spring-application-ngw8nzoq2) · Awesome Java Newsletter · 26 upvotes · 0 comments
- [No Database Operations Found: What Writing a Spring Boot Analyzer Taught Me About Spring](https://daily.dev/posts/no-database-operations-found-what-writing-a-spring-boot-analyzer-taught-me-about-spring-ly4puhnc1) · Foojay.io · 32 upvotes · 6 comments

---

Tags: [#testing](https://daily.dev/tags/testing), [#java](https://daily.dev/tags/java), [#spring-boot](https://daily.dev/tags/spring-boot)

[View this post on daily.dev](https://daily.dev/posts/testing-your-auto-configuration-against-a-missing-optional-dependency-ohli3thgh)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Testing your auto-configuration against a missing optional dependency","url":"https://daily.dev/posts/testing-your-auto-configuration-against-a-missing-optional-dependency-ohli3thgh","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/testing-your-auto-configuration-against-a-missing-optional-dependency-ohli3thgh"},"datePublished":"2026-09-06T09:08:26.452Z","dateModified":"2026-09-14T07:21:17.854Z","description":"A deep dive into a subtle bug where Spring Boot auto-configurations with optional dependencies can throw NoClassDefFoundError for users who lack that...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/73df68e20e9cd44b326384f5781368bd?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/73df68e20e9cd44b326384f5781368bd?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Wim Deblauwe","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Wim Deblauwe","logo":"https://media.daily.dev/image/upload/s--g1qrglrf--/c_limit,w_256/f_auto,q_auto/v1773648779/logos/wim-deblauwe?_a=BAMAMiiu0","url":"https://daily.dev/sources/wim-deblauwe"},"commentCount":3,"discussionUrl":"https://daily.dev/posts/testing-your-auto-configuration-against-a-missing-optional-dependency-ohli3thgh","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":3}],"keywords":"testing,java,spring-boot","timeRequired":"PT9M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Wim Deblauwe","item":"https://daily.dev/sources/wim-deblauwe"},{"@type":"ListItem","position":3,"name":"Testing your auto-configuration against a missing optional dependency"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/testing-your-auto-configuration-against-a-missing-optional-dependency-ohli3thgh","comment":[{"@type":"Comment","text":"At seventy lines, I’d probably just copy the helper. Does HidingClassLoader really need to become another dependency?","datePublished":"2026-09-09T09:37:40.863Z","url":"https://daily.dev/posts/OHLI3thGH#c-8pz9W9oMi","author":{"@type":"Person","name":"Agustin Barrientos","url":"https://daily.dev/agustinbarrientos","image":"https://media.daily.dev/image/upload/s--5ayxQnqn--/f_auto/v1788281802/avatars/avatar_wQYYVe5Tbj0NJ7C7qPoa8?_a=BAMAMicg0"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1}},{"@type":"Comment","text":"FilteredClassLoader never defines GreeterAutoConfiguration, so getDeclaredMethods still resolves ClientHttpRequestInterceptor against the loader that has spring-web. I wouldn’t trust that green test. Nested @ConditionalOnClass is what actually works, because the outer class never names the missing type.","datePublished":"2026-09-08T15:05:36.146Z","url":"https://daily.dev/posts/OHLI3thGH#c-bVp8egLZR","author":{"@type":"Person","name":"Leonid Bugaev","url":"https://daily.dev/leonidbugaev","image":"https://lh3.googleusercontent.com/a/ACg8ocLhMgurwTmJElWaH9A8Ju8cHvZxgMCDt009jEYkmFCyUGAoaGSw=s96-c"}},{"@type":"Comment","text":"You are right that FilteredClassLoader misses this.  It never defines the classes, so it cannot reproduce the failure a real app class loader would hit.  The HidingClassLoader gets closer to what actually happens in production, where the auto-configuration class is defined by a loader that can see the optional dependency.\nThe nested configuration fix mirrors what Spring Boot does internally, which is usually a good sign.  The outer class stays loadable, the inner one only loads when the dependency is present, and reflection never gets a chance to choke on the missing signature.\nWorth adding a test that asserts the failure mode itself, not just that the fix works.  A test that proves the auto-configuration fails without the dependency, then passes with it, documents why the nesting exists in the first place.","datePublished":"2026-09-15T14:47:08.552Z","url":"https://daily.dev/posts/OHLI3thGH#c-9INhgyZ1c","author":{"@type":"Person","name":"Adam","url":"https://daily.dev/nark3d","image":"https://avatars.githubusercontent.com/u/2162621?v=4"}}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/testing-your-auto-configuration-against-a-missing-optional-dependency-ohli3thgh#faq","mainEntity":[{"@type":"Question","name":"Why doesn't FilteredClassLoader catch a NoClassDefFoundError caused by a missing optional dependency in a Spring Boot auto-configuration?","acceptedAnswer":{"@type":"Answer","text":"FilteredClassLoader only intercepts class loading by name and delegates everything else to its parent, so it never becomes the defining class loader of the configuration class itself. Since the JVM resolves method signatures using a class's defining class loader, the configuration class is still defined by the application class loader that has the dependency, so no error occurs and the test passes incorrectly. When a config test passes but production still breaks, daily.dev surfaces classloader debugging techniques like this one."}},{"@type":"Question","name":"Why does @ConditionalOnMissingBean fail with NoClassDefFoundError even on a bean unrelated to the missing optional dependency?","acceptedAnswer":{"@type":"Answer","text":"Evaluating @ConditionalOnMissingBean requires finding the bean method via ReflectionUtils.findMethod, which calls Class.getDeclaredMethods(), and that call resolves the signatures of every declared method in the class at once, not just the target one. If any other method in the same class references a type from a missing optional dependency, the whole reflection call throws NoClassDefFoundError, even though @ConditionalOnClass would have skipped that unrelated method. Spotting subtle Spring Boot auto-configuration bugs like this is easier with daily.dev feeding relevant deep dives."}},{"@type":"Question","name":"How do you fix a Spring Boot auto-configuration that crashes when an optional dependency like spring-web is missing?","acceptedAnswer":{"@type":"Answer","text":"Move any bean method that references a type from the optional dependency into a nested @Configuration class annotated with @ConditionalOnClass. Since Spring reads conditions from annotation metadata via ASM without loading the nested class, that class is never loaded when the dependency is missing, so its problematic method signatures are never resolved. The rule: no method signature in the top-level auto-configuration class may reference an optional dependency's types. Maintainers hardening Spring Boot starters against classpath gaps can track patterns like this via daily.dev."}}]}
```

