Snicolas
Snicolas

Reputation: 38168

Unused / Used Constants for Tests

I got a bunch of constants that I use for my tests. They look like this :

//Canada 
private final static String CA_QUEBEC = "CA/Québec/Québec";
private final static String CA_VANCOUVER = "CA/British Columbia/Vancouver";

//USA
private final static String US_LOS_ANGELES = "US/California/Los Angeles";
private final static String US_NEW_YORK = "US/District of Columbia/New York";

//FRANCE
private final static String FR_PARIS = "FR/Paris/Paris";

etc. The goal of my test is to run my application quickly while using one of these constants : For instance :

    String cityPath = FR_PARIS;

Nevertheless, as I use only one of them, I must surround every constant with a suppress warning annotation (except the one I use, see below):

    //Canada 
    @SuppressWarnings("unused")
    private final static String CA_QUEBEC = "CA/Québec/Québec";
    @SuppressWarnings("unused")
    private final static String CA_VANCOUVER = "CA/British Columbia/Vancouver";

So that the compiler doesn't complain about those constants not beeing use although they are declared. Moreover, when I choose one of these constants, I then got another warning telling me that I used the suppresswarning annotation but my constant is actually beeing used.

If I want to get rid of all warnings, I should then spend my time putting and removing those annotations, and that's quite unconfortable to my mind.

So, how can I get a clean, warning free code in such a case. I am looking for a simple yet elegant solution (i.e. not introspection for instance).

Thanks to all, Stack Over Flow is really a cool place. :)

Stéphane

Upvotes: 0

Views: 201

Answers (2)

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 299108

The simplest solution: Mark your constants as public.

Upvotes: 1

Jeremy
Jeremy

Reputation: 22435

Make them public or protected, and your warnings will go away.

It also seems like a code smell that you are manually changing them to run tests. Wouldn't it make more sense to have separate tests for each constant? If there's a lot of common code, then you can have a base class to extend from.

Upvotes: 4

Related Questions