user2800089
user2800089

Reputation: 2381

Unable to find a @SpringBootConfiguration when doing spring cloud contract test

I have a spring boot project as producer which have API endpoints, created via RouterFunction.

That API endpoint call handler which call a service and then make a call to JPA repository.

We have used liquibase to create db tables and for writing testcases we have used postgressql testcontainer.

I have written base class for producer as below:

@SpringBootTest
public abstract class RestBase extends TestBase {

    @Autowired
    private AccountHandler accountHandler;

    @BeforeEach
    public void setup() {
        RestAssuredWebTestClient.webTestClient(WebTestClient.bindToRouterFunction(
                new RouterConfig(accountHandler).routes()).build());

    }
}

However, when I am trying to run mvn clean install, below stack trace is coming.

[INFO] Running com.example.RestTest
2021-09-02 13:46:26.207  INFO 25420 --- [           main] .b.t.c.SpringBootTestContextBootstrapper : Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.example.RestTest], using SpringBootContextLoader
2021-09-02 13:46:26.208  INFO 25420 --- [           main] o.s.t.c.support.AbstractContextLoader    : Could not detect default resource locations for test class [com.example.RestTest]: no resource found for suffixes {-context.xml, Context.groovy}.
2021-09-02 13:46:26.208  INFO 25420 --- [           main] t.c.s.AnnotationConfigContextLoaderUtils : Could not detect default configuration classes for test class [com.example.RestTest]: RestTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.127 s <<< FAILURE! - in com.example.RestTest
[ERROR] com.example.RestTest  Time elapsed: 0.127 s  <<< ERROR!
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
2021-09-02 13:46:26.346  INFO 25420 --- [           main] c.e.e.c.ContainerEnvironmentResource     : Stopping all test containers.
2021-09-02 13:46:26.778  INFO 25420 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2021-09-02 13:46:26.781  INFO 25420 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2021-09-02 13:46:26.795  INFO 25420 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Errors: 
[ERROR]   RestTest » IllegalState Unable to find a @SpringBootConfiguration, you need to...

How to solve this issue? Is there any issue with my base test class?

Router Config class looks like below:

@Configuration
@RequiredArgsConstructor
public class RouterConfig {

    private final AccountHandler accountHandler;

    @Bean
    public RouterFunction<ServerResponse> routes (){
        return route()
                .GET("/accounts",accountHandler::retrieveAccountByFilter)
                .build();
    }
}

Upvotes: 1

Views: 6458

Answers (2)

BStateham
BStateham

Reputation: 1629

Make sure that the classes you are testing are in the same namespace as your @SpringBootApplication class, or are in namespaces UNDER the namespace that contains your @SpringBootApplication class.

For example, if your app class is in com.example.demo.MyApplication but the classes you are testing is com.example.MyClass S

Or if they need to be in separate namespace trees, you may try adding the list of app classes to your @SpringBootTest annotation, e.g.

@SpringBootTest(classes=YouAppClassName.class)
public abstract class RestBase extends TestBase {
 ...
}

Upvotes: 1

Marcin Grzejszczak
Marcin Grzejszczak

Reputation: 11149

I mean literally it is said right there in the error message what you should do. Just provide the configuration class in @SpringBootTest annotation. Also for contract tests you should not start any databases or repositories. You should mock them out.

Upvotes: 2

Related Questions