Application Development 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: 

Regarding Issue with Local Class ABAP Unit test

Muthu_raja
Active Participant
0 Kudos

I have created Local Test Class in SE24 to test my Class. Lets say my local class name is zlcl_test.

When I call a method1 from zlcl_test which is supposed to be create a database entry But it is not (issue here). When I call the same method1 from my Program(SE38) which is creating db entry correctly.

I am using Local Test class first time. I think I am missing something.

Any idea to resolve this??...

Thanks & Regards,

Muthu

1 ACCEPTED SOLUTION

pokrakam
Active Contributor

Lots of issues here.

- How are you calling it?

- You can't call ABAP Unit test methods, only via the unit test framework. So I'm pretty sure you're not creating a unit test class (it must have CLASS ... FOR TESTING as part of the definition and must be written in the unit test class component.

- You definitely can't call it from an SE38 report

- What do you mean by "But it is not (issue here)"?

- Unit tests should generally not interact with the database, the whole point is that your code should be isolated from external components that could influence your result, such as function calls, database tables etc.

Suggest you read up a little more, there are some great blogs on here and also good documentation.

https://www.google.com/search?q=site%3Asap.com+abap+unit&oq=site%3Asap.com+abap+unit

5 REPLIES 5

pokrakam
Active Contributor

Lots of issues here.

- How are you calling it?

- You can't call ABAP Unit test methods, only via the unit test framework. So I'm pretty sure you're not creating a unit test class (it must have CLASS ... FOR TESTING as part of the definition and must be written in the unit test class component.

- You definitely can't call it from an SE38 report

- What do you mean by "But it is not (issue here)"?

- Unit tests should generally not interact with the database, the whole point is that your code should be isolated from external components that could influence your result, such as function calls, database tables etc.

Suggest you read up a little more, there are some great blogs on here and also good documentation.

https://www.google.com/search?q=site%3Asap.com+abap+unit&oq=site%3Asap.com+abap+unit

Muthuraja,

I just want to add one more source of information to Mike's answer:

There's been also a great course recently, that covers ABAP Unit tests among other things: Writing Testable Code for ABAP , https://open.sap.com/courses/wtc1/resume

Muthu_raja
Active Participant
0 Kudos

Hi Mike,

Thanks for your time.

- You can't call ABAP Unit test methods, only via the unit test framework. So I'm pretty sure you're not creating a unit test class (it must have CLASS ... FOR TESTING as part of the definition and must be written in the unit test class component.

Sorry my mistake, I didn't given a proper explanation. I have created unit test class only CLASS ... FOR TESTING

but i am not calling the unit test class. I have created separate program in SE38 and called my class (not unit test class).


- What do you mean by "But it is not (issue here)"?

Lets consider I have a program in SE38 and i am calling this method1 (not unit test class) it will create a database entry. But When doing unit test class it is calling the same method1 and doesn't create a database entry (this is my issue).


- Unit tests should generally not interact with the database, the whole point is that your code should be isolated from external components that could influence your result, such as function calls, database tables etc.

When Unit tests it should not create database entry ?? Could you please elaborate this point??, this will solve my issue.

Thanks in Advance,


Muthu

pokrakam
Active Contributor

The unit test framework does a rollback, so if you're not committing then nothing is saved.

Re. the last point, this is not a technical limitation but a design approach. Interacting with the database increases the scope from unit test towards integration tests and should be avoided.

I'm sorry but this is a big topic we can't really cover in a single answer post, and much has been written about it (entire books in fact). The course suggested by Egor is excellent. There is also a lot of documentation in the online help, and some good info right here, such as

https://wiki.scn.sap.com/wiki/display/ABAP/ABAP+Unit+Best+Practices

Do feel free to post individual questions about areas you're not clear about, but I'd suggest spending some time with the available resources. ABAP Unit is really a great tool if used correctly, and can be an enjoyable part of development as it makes you really think about your code and how it's structured.

horst_keller
Product and Topic Expert
Product and Topic Expert