8

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?

dmfrey
  • 1,230
  • 1
  • 17
  • 33

3 Answers3

6

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

spencergibb
  • 24,471
  • 6
  • 69
  • 75
  • Thanks @spencergibb That is what I was thinking as well. However, I would think sonar would see that that class contains a public static main method and use that as an exception to that rule. – dmfrey Sep 24 '15 at 20:50
  • P.S. Good to meet you at SpringOne2gx. Enjoyed your talks. – dmfrey Sep 24 '15 at 20:51
  • Thanks! nice to meed you too. – spencergibb Sep 24 '15 at 20:56
  • @dmfrey you can actually report that usecase at sonarqube@googlegroups.com so SonarQube team can actually add the exception to this rule ! it's open source, only price is reporting issues :) – benzonico Sep 25 '15 at 11:14
  • 1
    @benzonico I have posted a message to the list. Thanks for the info. – dmfrey Sep 25 '15 at 13:35
  • I saw that, I'll try to reply to it soon ;) – benzonico Sep 25 '15 at 15:55
  • 2
    Hi, can you add an link to the post or to an issue in an issue-tracking-system? – hilbert Apr 26 '16 at 11:00
  • 1
    Any recommendation on how to disable this rule for only the SpringBoot Application and not all public constructors violations? – ken Dec 28 '16 at 22:22
  • It is testable, have a look for my answer: https://stackoverflow.com/a/45374995/863403 – Joergi Jul 28 '17 at 13:38
0

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.

Community
  • 1
  • 1
Gergely Bacso
  • 14,243
  • 2
  • 44
  • 64
  • This is really hacking around, please report issue to improve the rule and mark it as false positive in your sonarqube instance. – benzonico Sep 25 '15 at 11:15
  • @benzonico I wasn't going to implement it, but it was just funny that it came up multiple times. – dmfrey Sep 25 '15 at 13:35
  • I assume it was a bit personal for him as he is working on Sonar. If you would happen to report it, would you mind leaving a link to the Jira ticket here? I wonder how long will it take to get it fixed. – Gergely Bacso Sep 26 '15 at 17:15
-1

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)

Joergi
  • 1,527
  • 3
  • 39
  • 82
  • 1
    Do not write tests to satisfy tools or target numbers. That is where the purpose of TDD and unit testing is lost. This answer is a bad pointer. – ring bearer May 13 '20 at 12:11