Java.lang.illegalstateexception: configuration error: found multiple declarations of @bootstrapwith for test class

java.lang.IllegalStateException: Configuration Error

This exception occurs when there are multiple declarations of the “@BootstrapWith” annotation for a test class in Java.
The “@BootstrapWith” annotation is used to specify a bootstrap class for test suites.

To understand this issue, let’s consider an example.
Assume we have a test class named “TestClass” with the following code:

        
            @RunWith(SpringRunner.class)
            @SpringBootTest(classes = {Application.class})
            @BootstrapWith(CustomBootstrap.class)
            public class TestClass {
                @Test
                public void testMethod() {
                    // test logic
                }
            }
        
    

In the above code, you can see that the test class is annotated with the “@BootstrapWith” annotation,
specifying a custom bootstrap class called “CustomBootstrap”.
This allows us to customize the test suite bootstrap process.

However, if there are multiple instances of the “@BootstrapWith” annotation in the same test class,
the “java.lang.IllegalStateException” will be thrown.
For example, if we modify the code as follows:

        
            @RunWith(SpringRunner.class)
            @SpringBootTest(classes = {Application.class})
            @BootstrapWith(DefaultBootstrap.class)
            @BootstrapWith(CustomBootstrap.class)
            public class TestClass {
                @Test
                public void testMethod() {
                    // test logic
                }
            }
        
    

There are multiple “@BootstrapWith” annotations declared in the “TestClass” now,
specifying different bootstrap classes “DefaultBootstrap” and “CustomBootstrap”.
This causes a conflict and leads to the “IllegalStateException”.

To resolve this issue, you need to make sure there is only one “@BootstrapWith” annotation in the test class.
Choose the appropriate bootstrap class based on your requirements and remove any redundant or conflicting annotations.

In the above example, you can either keep the “DefaultBootstrap” or the “CustomBootstrap” annotation and remove the other.
For instance, if you want to use the “CustomBootstrap” class, modify the code as follows:

        
            @RunWith(SpringRunner.class)
            @SpringBootTest(classes = {Application.class})
            @BootstrapWith(CustomBootstrap.class)
            public class TestClass {
                @Test
                public void testMethod() {
                    // test logic
                }
            }
        
    

Read more interesting post

Leave a comment