Skip to Content
Apr 04, 2011 at 01:40 PM

ABAP Unit: Should constants be hardcoded into unit tests?



I suspect there's no right or wrong, but interested in people's opinions on whether class constants should be re-used or hardcoded in an ABAPUnit test?

e.g.: I have a class ZCL_MYCLASS with a constant FOO, value 'BAR'.

In my unit test should we do:

if lv_test = zcl_myclass=>foo.


constants: lc_foo type string value 'BAR'. 
if lv_test = lc_foo.

As I see it, the first keeps management of constants in one place, and you don't need to update your unit test if constants change. The second on the other hand tests that your constants are correct, as well as guarding against people modifying constants with unintended consequences.

Hmmm.... both valid cases. The question is whether the validity and/or unauthorised alteration of constants should be the subject of a unit test?