JaveDeveloper
JaveDeveloper

Reputation: 133

Junit to test if the method call is made

I've a transaction class with a validate method. There is a condition in the method that when satisfied will not be calling an alert notification method, which, on the other hand, will only be called when the check condition failed (if condition is false, call the alert and notify the user). I want to test with a JUnit, if that alert method get's called or not. Could someone advise how to proceed, as I'm new to JUnits.

Upvotes: 4

Views: 24204

Answers (4)

Max Cascone
Max Cascone

Reputation: 833

I'm new to this myself in this late age of 2021. But I found an easy way to validate that a mock is called that doesn't require additional libraries. I'm not sure it's "correct", it doesn't feel exactly "correct", but it's adequate for my needs.

// test/org/mycompany/MyScriptTests.groovy
// testing vars/MyScript.groovy

import org.junit.*
import com.lesfurets.jenkins.unit.*
import static groovy.test.GroovyAssert.*

class MyScriptTests extends BasePipelineTest {
  def MyScript

  @Before
  void setUp() {
    super.setUp()
    
    // set up mocks
    helper.registerAllowedMethod('functionToMock', [<param type>], { return 'a validation string' } )

    // load MyScript
    MyScript = loadScript("vars/MyScript.groovy")
  }

  @Test
  void aTest() {
    // set up mocks
    def myparam = 'myparam'

    // call MyScript and check result
    def result = MyScript(myparam)
    assert 'a validation string'.equals(result)

  }

Upvotes: 0

Christian
Christian

Reputation: 22343

Use the Mockito framework. This framework makes your kind of testing much easier.

You can use the verify method:

//Check if called once
verify(dependency, times(1)).yourMethod()

Check the API for more informations.

Upvotes: 3

Michiel Scheepmaker
Michiel Scheepmaker

Reputation: 21

From your question it is not clear whether or not the alert notification method belongs to the same class you are testing.

If the alert notification method is in a different class than the method you are testing, you can use a mocking library like Mockito to verify the method call, as suggested by other answers.

If, on the other hand, the notification method is in the same class as the validation method, I would advice to not verify the call to the notification method, but to verify the output of this method (you probably want to use a mocking library for this as well, but to verify method calls on any other objects used to create the alert)

A third possibility is that both methods are in the same class, but you really only want to test that the notification method gets called and not its output, because you feel that notification is a different function that you want to test separately. This would be a sign that your class may be doing too many things, and that it may be best to refactor so that these methods end up in separate classes.

Upvotes: 1

vrudkovsk
vrudkovsk

Reputation: 363

Look at mocking libraries. In Mockito it will be like

    Notifier mock = Mockito.mock(Notifier.class); 
    ClassUnderTest myClass = new ClassUnderTest(mock);
    myClass.doSomething(-1);
    Mockito.verify(mock).notification(Mockito.eq("Negative value passed!"));
    myClass.doSomething(100);
    Mockito.verifyNoMoreInteractions(mock);

Upvotes: 4

Related Questions