vendredi 13 décembre 2019

How to use my Application class in Spring Boot Test?

I'm using Spring Boot 2.2.2.RELEASE with Java.

I have some @Service classes, these have the same class name, but different packages. Example: package1/Service.java package2/Service.java

I had a problem "non-compatible bean definition of same name and class". I've fixed it so: I've added this to my main class in Application.java, so that all beans have their full paths as a name.

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(TaxEngineApp.class);
        app.setBeanNameGenerator(new AnnotationBeanNameGenerator() {
            @Override
            protected String buildDefaultBeanName(BeanDefinition definition) {
                String beanClassName = definition.getBeanClassName();
                Assert.state(beanClassName != null, "No bean class name set");
                return beanClassName;
            }
        });
        DefaultProfileUtil.addDefaultProfile(app);
        Environment env = app.run(args).getEnvironment();
        logApplicationStartup(env);
    }

But if I run any test annotated with @SpringBootTest, it uses its own Application. So I have the same problem "conflicts with existing, non-compatible bean definition of same name and class". How can I do so, that it would use my Application.java? I've tried @SpringBootTest(classes = TaxEngineApp.class), but it doesn't help too.

Aucun commentaire:

Enregistrer un commentaire