Skip to Content
0

Test Data Directory for ABAP class static methods

May 04 at 06:12 AM

135

avatar image
Former Member

Hello Experts,

is there a way to store the test data directory like we do for FM testing or any alternative way to test the static methods of a Class ?

Thanks

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Best Answer
Mike Pokraka May 07 at 06:51 AM
1

Yes, you can write ABAP Unit tests.

It's a big topic so you need to spend some time with it. Start with the documentation, but there is also a lot of info on here just search for ABAP Unit

Show 15 Share
10 |10000 characters needed characters left characters exceeded

Just to comment that ABAP Unit Tests are not always easy to apply to static methods - better to convert the class to the factory pattern, and change the static methods to instance.

1
Matthew Billingham

I agree that an instance method is usually a better option, but I don't see how a method being static makes it more difficult to test?

i.e. if we were to put the same code into an instance method, the test would be exactly the same.

As an aside, static methods written as instance methods is another bugbear of mine, but that's another topic.

0

I was thinking of dependency lookup- so if the static method is a dependency.and has an unpleasant side-effect.

0
Matthew Billingham

Agree, testing callers of static methods is more difficult, so from that point of view it is good advice.

I interpreted you comment as the static method itself is more difficult to test.

0

I'm curious - other than appeasing ABAP Unit Test, are there any other reasons to do so?

Recently I created a very simple class with a static method and was surprised, like OP, that we can't just easily save tests like we could in SE37. I ended up writing a short program to test the method.

0
Jelena Perfiljeva

"I ended up writing a short program to test the method."

Ouch. Guess what ABAP Unit is made for ...

And besides being able to test explicitly via Ctrl+Shift+F10 you have the benefit of test automation!

In the moment I'm doing some major refactoring. And luckily most of the code is unit tested. A big help in keeping the results stable.

1

The program doesn't run just one test, it actually reads the whole config table and then runs a test for all the possible values. I guess this could be also done in ABAP Unit (and it'd make sense to keep the test code together with the class itself) but honestly I still don't understand how exactly. And preferably without having to type 3x more code than in the method itself.

0
Jelena Perfiljeva

You did what??? After the Unit Testing course? Shame on you! :-)

Incidentally that's exactly how I got started with ABAP Unit: I thought to myself hey, instead of saving stuff in a test variant it just takes a small extra effort to write it into code where I can run it as many times as I like and don't even have to bother checking the results because it will tell me if there's a problem even months down the line.

To answer your question: Yes the OO philosophy is to always work with an instance unless it's truly static. The problem with static classes is that they're essentially just function groups. The problem with those is global data, a function call here can affect the data of another function call in a different part of the program. But you need a reference to interact with an object instance, so it's always under your control.

There's also a difference in the memory handling too, an instance gets cleared by the garbage collector, but a static class and all its data lives in program memory as long as the program session is active.

1

To be fair, this was right before the course started. :) I actually had high hopes for the course in this regard and I might be stupid or something but so far I can't really figure out how exactly to connect what the course presenters were trying to teach with what I needed to do.

For example, my method dealt with currency conversion (related SCN question). But when the course started and it went like "given 1 we expect 2" my reaction was "f*k this, how am I expected to do this with dozens of currencies?" I'm still planning to go back and review some parts of the course once again but honestly they just lost me right there. Don't want to dissent into the course criticism (yet again :) ) but some things they could've done much better IMHO.

In this particular case it seems like "potato-potahto" situation: doesn't matter if it's instance or static. But I like your explanation, it really helps and gives some food for thought. Thank you!

0

Ah, you need Gerard Meszaros xUnit Test Patterns - it covers such situations. Could be that a Data Driven Test is the pattern you need,

1

When I started with ABAP Objects I used to create static classes. But it happened so often that I needed to be able to instantiate that nowadays I rarely write static methods.

The point is not appeasing ABAP Unit Test, but rather if you adopt TDD you are unlikely to produce many static methods. Instead you'll have (ideally) an application made up of loosely coupled, robust, testable and tested classes.

Incidentally, if you look at classes like CL_RSPC_ABAP (which are used in process chains in BW), you'll see some of them, especially the older ones, have entirely static methods. :-O

1

Well, ironically, that's exactly how the story goes. Let's say all you want is really just a simple function module, value in, value out. After reading some ABAP Objects books, you still have only a vague idea of what inheritance and polymorphism has to do with your situation. People have been telling you FMs are for losers, so OK, you go to SE24. There you are confronted with some weird choices. OK, I figure class needs to be public, that's kind of obvious. Then you go to the method. "Instance or Static"... What's the difference? How the heck do I know? Ah, OK, I'll check F1:

Err... [Scratching head] WTF... Still no idea... Ah! I know! When in doubt - look at what SAP is doing. I know this useful class CL_GUI_FRONTEND_SERVICES, let me check what they're dong there. Oh, I see - Instance / Protected, Static / Public. OK, Static it is. Boom, done! Moving on.

Possibly a stupid question but if Static is bad then why even have it?

scn.jpg (62.9 kB)
1

I learned java, then applied what I learned to abap objects. It's a route I recommend

Static has its uses. Factory pattern for example.

0

if Static is bad then why even have it? I will compare it to the use of CONSTANTS and DATA with initial value.

Using a constant is fine until you realize it has to change, then you regret your definition. Defining a method as static is fine until you need flexible interfaces, like in unit tests. Then you regret your definition.

JNN

3

Here's another reason to use ABAP Unit Test rather than test data directories. https://answers.sap.com/questions/506764/how-to-retrieve-test-data-directory-for-a-function.html

1
avatar image
Former Member May 14 at 11:11 AM
0

Thanks everyone, as I understand from the long but interesting conversation that there is no simple way of storing the test data directory as in FMs.

Share
10 |10000 characters needed characters left characters exceeded