-1

How we can write system.assert statement for the method returning void.

2
  • 3
    Kid, do you have any specific scenario where you are facing this problem? If yes please add your code here. – Rahul Gawale Sep 30 '19 at 13:51
  • 1
    I have voted this post down because it is very vague. If you edit your question to include a more specific example, I may remove or reverse that vote. – Adrian Larson Sep 30 '19 at 15:56
4

Honestly, you make assertions for a method that returns void the same way you write assertions for anything else you're testing. Assertions are about validating the results of running a piece of code.

  • If your method is inserting a record, assert that you have one more record after running the code compared to before (query before and after)
  • If your method is updating a record, assert that the value of the field(s) you're updating were actually changed (again, query the record(s) before and after running your code)
  • If your method returns a value, assert that the return value is what you expect it to be
  • If your method modifies a class's public state (i.e. changes a value in a public class variable), assert that that change happened and that the new value is what you expect

If the method you're testing does more than one of those things, then write assertions to verify all of the different output (though better practice would probably be to break that up into separate test methods).

My list above isn't exhaustive, but it should do fine as an example. Again, the key to success here is that you're making assertions to verify that that thing(s) you think your code should be doing are actually being done (and done correctly).

1
  • That method is deleting the records and return type is void but as per practice we should add assertion.This item was related to issue reported by Sonarqube. Thanks for your response and it is really worth for one(Like me) who just started to code. – Kid Sep 30 '19 at 15:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.