dmfrey
dmfrey

Reputation: 1240

Sonar complains about Spring Boot configuration

I have this class to start up the spring-cloud config server. It is a spring-boot application.

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {

    public static void main( String[] args ) {

        SpringApplication.run( ConfigServerApplication.class, args );

    }

}

The application runs fine and all my unit tests are fine. However, in our bamboo pipeline, it will initial a sonar process to analyze the code. We keep getting these minor warnings indicating the following:

Utility classes should not have a public constructor

I know that this is a minor issue, but I have been tasked with removing these from our code.

Ideally, you would mark the class final and provide a private constructor, or so all searches provide as a solution. However, a Spring Configuration class cannot be made final and cannot have a private constructor.

Any ideas how to resolve this?

Upvotes: 8

Views: 8113

Answers (3)

Joergi
Joergi

Reputation: 1593

It's easy to test:

@RunWith(SpringRunner.class)
@SpringBootTest
public class YourApplicationTest {

    @Test
    public void shouldLoadApplicationContext() {
    }

    @Test
    public void applicationTest() {
        YourApplication.main(new String[] {});
    }

}

Now Sonar is saying, this is tested!
(Kudos goes out to: Robert @ https://stackoverflow.com/a/41775613/863403)

Upvotes: -1

Gergely Bacso
Gergely Bacso

Reputation: 14651

Adjusting your sonar settings would be a nicer approach of course, but if you want to please the machine spirits, you can simply add a non-static dummy function to your class, making it "non-utility" in the eyes of the Sonar checker.

Upvotes: 0

spencergibb
spencergibb

Reputation: 25157

I'm afraid this isn't a problem spring-boot or spring-cloud can solve. You need to add exceptions to your sonar configuration.

Upvotes: 6

Related Questions