05-04-2018 7:12 AM
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
05-07-2018 7:51 AM
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
05-07-2018 7:51 AM
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
05-07-2018 9:32 AM
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.
05-07-2018 1:04 PM
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.
05-07-2018 2:17 PM
I was thinking of dependency lookup- so if the static method is a dependency.and has an unpleasant side-effect.
05-07-2018 2:35 PM
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.
05-07-2018 8:43 PM
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.
05-07-2018 8:57 PM
"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.
05-07-2018 9:19 PM
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.
05-08-2018 8:16 AM
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. 😮
05-08-2018 4:50 PM
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?
05-08-2018 5:10 PM
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!
05-08-2018 5:16 PM
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.
05-08-2018 5:20 PM
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.
05-08-2018 6:55 PM
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
05-08-2018 8:11 PM
Ah, you need Gerard Meszaros xUnit Test Patterns - it covers such situations. Could be that a Data Driven Test is the pattern you need,
05-08-2018 8:14 PM
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
05-14-2018 12:11 PM
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.