How can I test the idempotency of a method using junit?

I need to perform a method idempotency check.

Let's say I have a Person class with the following method:

    public String doSomething(String a){ 
//do some stuff


and I need to check for when something goes wrong before deleting, that the next time you call the doSomething method it will produce the same result as you did when it should have worked correctly the first time. This can happen, for example, when running a script that calls this method but fails, for example by stopping the script. The next time you run the script, it should give the same output without crashing.

Can you do this in a unit test?

Thank you in advance


source to share

2 answers

The test should run the method twice. The result / results should be the same in both cases. It's as simple as it really is.






So the first part of the answer is to use layout for DAO. Write two tests, one of which the method is called twice and the DAO does not throw an exception. Another is where DAO throws an exception on the first call.

The expected behavior in these two cases depends on your DAO. Is it possible to call delete

in your DAO on a value that has already been removed? If so, great. Expect two calls. If not, you need logic to check the state.



All Articles