An alternative test structure for ABAP projects

ABAP unit tests are written inside a global class, as a local test class. This is the most usual and the recommended.


I particularly do not like this approach because it seems like the unit tests are only testing the methods of this class. Perhaps you want to write some integration tests, where would you put it? Maybe, after some refactoring, you derive a new class. Doesn't this local test sound strange?

Thing is, most programming languages, such as Java and Ruby, separate the test class and the global productive class, even in separate packages:





Global test classes in ABAP

You can actually declare a global test class in ABAP. Let's see how that works:

Great. The test assertion is stupid, I know, but it works for our example. Now let us run this test:



Hmmm, bad. Apparently we need to declare this test as abstract, otherwise we get this gray warning (and I want my tests to be green).

So let's add the abstract to the definition:
Great, so now let's run the tests again..



It  is green! Wait. Zero test methods ran? Oh, the class is abstract so ABAP Unit can't instantiate this test class. Now we are in a deadlock, but this is as it is. ABAP unit was designed so that global test classes are only helpers or classes to be inherited.

An alternative


What do we do now? Well, this is the tricky part: we can declare a local test class inside our global test class, and make it inherit from the global test class:





So here is what our test code woud look like. We move everything to protected and let the local test class inherit it:
Now, let's run it...


In  this way, you can detach your test classes from your productive classes.

Template for Unit Test

Check the template for Abap in Eclipse to make development quicker.

Comments