Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Testing static methods without instantiating

pokrakam
Active Contributor
0 Likes
3,470

Hi all,

A really simple question: How do I test static methods without instantiating it? Test from SE24 and it wants to instantiate straight away - there is no access to instance-independent methods or interface components until the instance has been created.

In other words I want to test my static method before the constructor is called.

Am I missing something obvious?

Cheers,

Mike

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
2,240

Hello Mike

If your static method is public then - of course - you can test it within the class builder (SE24).

If your static method is either protected or private then you can use a simple trick as following:

(1) Class ZCF_TEST_CLASS contains private/protected static method STATIC_METHOD (=> your class) which you want to test.

(2) Copy your class to a test class (e.g. ZCL_FRIEND_CLASS) and set the visibility of the static method to public.

Within the test class code:


method TEST_STATIC_METHOD.

  CALL METHOD zcf_test_class=>static_method.

endmethod.

(3) Define the test class as FRIEND in your class which you want to test (Example: in class ZCF_TEST_CLASS enter ZCL_FRIEND_CLASS as friend).

Now you can use the test class to test the methods in your main class because as a friend it has access to all its private and protected components as well.

Regards

Uwe

11 REPLIES 11
Read only

Former Member
0 Likes
2,240

Hi Mike,

I think you will need create an instance while testing, because these need a test object during the test run. But then once you create an instance, come back to the screen where you started executing the class methods. The static methods will get executed.

Otherwise the system throws a dump. Logically the static method should be executed without creating an object also, but this does not happen.

I hope it helps.

Regards,

Sagar

Edited by: Sagar Nawathe on Mar 3, 2008 12:38 PM

Read only

matt
Active Contributor
0 Likes
2,240

I go into SE24, and enter the class CL_ABAPWD_FLIGHT_MODEL. I then press F8. I see three static methods that I can run. There's a button to create an instance. I don't see the problem you describe.

Are you sure that you've got your methods as statics and public?

What version are you running?

matt

Read only

uwe_schieferstein
Active Contributor
0 Likes
2,241

Hello Mike

If your static method is public then - of course - you can test it within the class builder (SE24).

If your static method is either protected or private then you can use a simple trick as following:

(1) Class ZCF_TEST_CLASS contains private/protected static method STATIC_METHOD (=> your class) which you want to test.

(2) Copy your class to a test class (e.g. ZCL_FRIEND_CLASS) and set the visibility of the static method to public.

Within the test class code:


method TEST_STATIC_METHOD.

  CALL METHOD zcf_test_class=>static_method.

endmethod.

(3) Define the test class as FRIEND in your class which you want to test (Example: in class ZCF_TEST_CLASS enter ZCL_FRIEND_CLASS as friend).

Now you can use the test class to test the methods in your main class because as a friend it has access to all its private and protected components as well.

Regards

Uwe

Read only

0 Likes
2,240

Ah, mea culpa... must describe problem accurately!

Yes, a straightforward method does work and always have. It's interface methods that are being a pain.

(I had accidentally set a method to private, which reminded me that I meant to ask about this for some time now).

So in a class ZCL_TEST with an interface IF_WHATEVER, I cannot test

ZCL_TEST~IF_WHATEVER->STATIC_METHOD

from SE24 without instantiating the class. Good suggestion to use a separate test class as workaround, but I can't shake the feeling I'm doing double work - we already have Unit Test for that.

What I'd like to do is SE24, hit the test icon and navigate to my interface-static method...

Or am I still missing something?

Thanks,

Mike

Edited by: Mike Pokraka on Mar 3, 2008 1:48 PM

v. 700 SP10 by the way

Read only

0 Likes
2,240

Hello Mike

I added an interface with a public static method to my class (ZCF_TEST_CLASS) and implemented it. In addition, I added an instance attribute and the CONSTRUCTOR method where the instance attribute is filled.

Now when I test the class in SE24 the testing framework does indeed create an instance of the class (because the instance attribute is filled).

However, why do you care about the instantiation of your class when you want to test static methods? Only static attributes are available in static methods and, therefore, any changes of instance attributes should not bother you.

Regards

Uwe

Read only

0 Likes
2,240

I think you've found a bug.

The test framework thinks that there aren't any static method, because it doesn't check for static methods of the interface.

I created an interface - YIF_WHATEVER with one static method STATIC_METHOD.

I created a class - YCL_TEST implementing the interface, with static methods and instance methods of its all. When I run the test, I can only see the interface static methods after instantiation.

matt

Read only

0 Likes
2,240

Hello Matt

Apparently the testing framework behaves different depending on whether public methods are available or not.

Here is my scenario:

A. Class with private static class method and public static interface method

1. Execute testing.

2. Private static class method not visible (ok), click on interface make public static interface method visible (ok)

B. Class with public static class method and public static interface method

1. Execute testing

2. Public static class method visible (ok), interface not visible (?)

The difference between (A) and (B) is that in (A) the testing framework implicitly instantiates the class whereas in (B) there is no instantiation.

Bug or Feature?

Kind Regards

Uwe

Read only

0 Likes
2,240

I think it's a feature. Thinking about it, why would you have static methods in an interface? The most useful properties of an interface only relate to instances.

IIRC, you can't override static methods in subclasses either.

matt

Read only

0 Likes
2,240

Sorry bout late reply, missed thread update in all the coffee corner ramblings...

> I think it's a feature. Thinking about it, why would you have static methods in an interface? The most useful properties of an interface only relate to instances.

Why? On the testdrive version alone there are 1400+ reasons why in form of SAP's own interfaces with static methods. What I'm working with are static FIND_... methods to validate and object's existence as well as CREATE_... methods.

The issue with instantiating an object just to test a static interface is that the constructor in my case touches static attributes, therefore invalidating my test.

And before anyone asks... yes I'm using ABAP unit test framework and it's great! However there are instances where things are not quite as expected and you need to test something interactively.

Therefore my conclusion: Bug!

I'll drop OSS a note on this and let you all know how it goes. Thanks for all the feedback.

Cheers,

Mike

Read only

pokrakam
Active Contributor
0 Likes
2,240

Got a response back from OSS:

It's a feature. A missing feature to be precise. There's a development request for it, so it will be fixed implemented sometime between now and the future. Live with it.

Read only

pokrakam
Active Contributor
0 Likes
2,240

Just to follow up:

I explained to OSS that it's not a missing feature because it does in fact work with a brand new class and only an interface. The possibility to test static interface methods disappears at some random point.

e.g.

create a new class and add BI_PERSISTENT.

The interface is available and you can test

BI_PERSISTENT~FIND_BY_LPOR

Now add a static private event TEST and test -> it still works.

Now change the event visibility to public and test -> the interface is no longer available.

This makes it a bug in my books.

Quality response from SAP says it's by design. They issued an official note to that effect: [https://service.sap.com/sap/support/notes/1163754]

Cheers,

Mike