Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #37291 > unrolled thread
| Started by | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| First post | 2017-08-19 19:37 +0200 |
| Last post | 2017-08-20 13:12 -0400 |
| Articles | 9 — 4 participants |
Back to article view | Back to comp.lang.java.programmer
What is the ‘standard’ when using a constant in several unit tests Cecil Westerhof <Cecil@decebal.nl> - 2017-08-19 19:37 +0200
Re: What is the ‘standard’ when using a constant in several unit tests Cecil Westerhof <Cecil@decebal.nl> - 2017-08-19 21:36 +0200
Re: What is the ‘standard’ when using a constant in several unit tests Arne Vajhøj <arne@vajhoej.dk> - 2017-08-19 20:07 -0400
Re: What is the ‘standard’ when using a constant in several unit tests Cecil Westerhof <Cecil@decebal.nl> - 2017-08-20 04:43 +0200
Re: What is the ‘standard’ when using a constant in several unit tests Arne Vajhøj <arne@vajhoej.dk> - 2017-08-19 20:03 -0400
Re: What is the ???standard??? when using a constant in several unit tests leif.roar@dimnakorr.com - 2017-08-20 04:36 -0500
Re: What is the ???standard??? when using a constant in several unit tests Cecil Westerhof <Cecil@decebal.nl> - 2017-08-20 12:21 +0200
What is the ‘standard’ when using a constant in several unit tests lewbloch@gmail.com - 2017-08-20 09:46 -0700
Re: What is the ‘standard’ when using a constant in several unit tests Arne Vajhøj <arne@vajhoej.dk> - 2017-08-20 13:12 -0400
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Date | 2017-08-19 19:37 +0200 |
| Subject | What is the ‘standard’ when using a constant in several unit tests |
| Message-ID | <87k21zh319.fsf@munus.decebal.nl> |
I have several test classes that have the following constant:
private static final String TEST_DB = "testDB.sqlite";
Is this the way to do it, or should it be put centrally?
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
[toc] | [next] | [standalone]
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Date | 2017-08-19 21:36 +0200 |
| Message-ID | <8760djgxj6.fsf@munus.decebal.nl> |
| In reply to | #37291 |
ram@zedat.fu-berlin.de (Stefan Ram) writes:
> Cecil Westerhof <Cecil@decebal.nl> writes:
>>I have several test classes that have the following constant:
>> private static final String TEST_DB = "testDB.sqlite";
>>Is this the way to do it, or should it be put centrally?
>
> You can put it into an extra class:
>
> public final class TestConstants
> { public static final java.lang.String TEST_DB = "testDB.sqlite"; }
That is not frowned upon?
I implemented it.
> . Just remember: When the constant is changed, the change
> might not have the desired effect, because the value of the
> constant might have been compiled ("burned") into other
> classes. It might be necessary to force a recompilation
> of all the test classes in this case.
I am using maven. That takes care of it I think.
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2017-08-19 20:07 -0400 |
| Message-ID | <onajs2$1pd2$1@gioia.aioe.org> |
| In reply to | #37296 |
On 8/19/2017 3:36 PM, Cecil Westerhof wrote:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>> Cecil Westerhof <Cecil@decebal.nl> writes:
>>> I have several test classes that have the following constant:
>>> private static final String TEST_DB = "testDB.sqlite";
>>> Is this the way to do it, or should it be put centrally?
>>
>> You can put it into an extra class:
>>
>> public final class TestConstants
>> { public static final java.lang.String TEST_DB = "testDB.sqlite"; }
>
> That is not frowned upon?
No. Well maybe. But Joshua Bloch recommends it in "Effective Java". So
you can certainly defend the approach.
Arne
PS: He recommends a private constructor to prevent instantiation.
[toc] | [prev] | [next] | [standalone]
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Date | 2017-08-20 04:43 +0200 |
| Message-ID | <87efs7ez6t.fsf@munus.decebal.nl> |
| In reply to | #37308 |
Arne Vajhøj <arne@vajhoej.dk> writes:
> On 8/19/2017 3:36 PM, Cecil Westerhof wrote:
>> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>> Cecil Westerhof <Cecil@decebal.nl> writes:
>>>> I have several test classes that have the following constant:
>>>> private static final String TEST_DB = "testDB.sqlite";
>>>> Is this the way to do it, or should it be put centrally?
>>>
>>> You can put it into an extra class:
>>>
>>> public final class TestConstants
>>> { public static final java.lang.String TEST_DB = "testDB.sqlite"; }
>>
>> That is not frowned upon?
>
> No. Well maybe. But Joshua Bloch recommends it in "Effective Java". So
> you can certainly defend the approach.
OK, thanks.
> PS: He recommends a private constructor to prevent instantiation.
That I always do with a class that should not be initiated. ;-)
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2017-08-19 20:03 -0400 |
| Message-ID | <onajkn$1ou9$2@gioia.aioe.org> |
| In reply to | #37291 |
On 8/19/2017 1:37 PM, Cecil Westerhof wrote: > I have several test classes that have the following constant: > private static final String TEST_DB = "testDB.sqlite"; > > Is this the way to do it, or should it be put centrally? Several test classes with common functionality seems like a good case for an abstract base class. If you already got a base class, then just put it there. Arne
[toc] | [prev] | [next] | [standalone]
| From | leif.roar@dimnakorr.com |
|---|---|
| Date | 2017-08-20 04:36 -0500 |
| Subject | Re: What is the ???standard??? when using a constant in several unit tests |
| Message-ID | <dJ6dnR1ZOa-RxQTEnZ2dnUU78d2dnZ2d@giganews.com> |
| In reply to | #37291 |
Cecil Westerhof <Cecil@decebal.nl> wrote: > I have several test classes that have the following constant: > private static final String TEST_DB = "testDB.sqlite"; > > Is this the way to do it, or should it be put centrally? > Well, that depends. Are the test classes written specifically for this database, or are they written for a generic database? If it's the latter, it makes more sense to put it centrally, and if it's the latter it makes more sense to leave it in place. -- Leif Roar Moldskred
[toc] | [prev] | [next] | [standalone]
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Date | 2017-08-20 12:21 +0200 |
| Subject | Re: What is the ???standard??? when using a constant in several unit tests |
| Message-ID | <87a82ufskm.fsf@munus.decebal.nl> |
| In reply to | #37310 |
leif.roar@dimnakorr.com writes: > Cecil Westerhof <Cecil@decebal.nl> wrote: >> I have several test classes that have the following constant: >> private static final String TEST_DB = "testDB.sqlite"; >> >> Is this the way to do it, or should it be put centrally? >> > > Well, that depends. Are the test classes written specifically > for this database, or are they written for a generic database? > > If it's the latter, it makes more sense to put it centrally, > and if it's the latter it makes more sense to leave it in > place. It was the latter, but I am probably going to rewrite my tests. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof
[toc] | [prev] | [next] | [standalone]
| From | lewbloch@gmail.com |
|---|---|
| Date | 2017-08-20 09:46 -0700 |
| Message-ID | <d20ce0fe-07d6-425a-bdc0-3a3487e37f4c@googlegroups.com> |
| In reply to | #37291 |
Many choices. - A constants class, considered by many an antipattern. - Injected, a very "in" pattern. - Resource manager call, classic and reliable approach and even compatible with injection. -- Lew
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2017-08-20 13:12 -0400 |
| Message-ID | <oncft0$bjh$1@gioia.aioe.org> |
| In reply to | #37324 |
On 8/20/2017 12:46 PM, lewbloch@gmail.com wrote: > Many choices. > - A constants class, considered by many an antipattern. Maybe. But recommended by Joshua Bloch. That is good enough for me. > - Injected, a very "in" pattern. > - Resource manager call, classic and reliable approach and even compatible with injection. Arne
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web