简体   繁体   中英

is there a way to test methods of these kind using mockito

since this method is executed at runtime is there need to test it

public void addListener(RepeatRejectAnalysis_Listener rralistener)
{
    Save_Button.addActionListener(rralistener);
    Cancel_Button.addActionListener(rralistener);
}

You might not even need a Mocking framework here.

If you have access to your buttons; you can simply invoke that method; and afterwards assert that each button has that new listener within its list of ActionListeners.

In other words: you only mock things when that object is hard to create in your unit test environment. For example: some client that wants to talk to a database probably needs mocking; but a simple JButton ... probably not.

Please understand: mocking frameworks are always the second choice. If possible, you want to assert/verify directly on your production code elements. So instead of mocking buttons, focus on being able to run your tests with real buttons and use the interfaces that those provide in order to verify the expected behavior.

In general it doesn't feel that there is a need to test this type of methods because there is no business logic.

However, if you are eager to do it, you could check that addActionListener method is called for two different objects with the same input parameter. Similar like this:

// SETUP SUT
RepeatRejectAnalysis_Listener yourRraListener = new RepeatRejectAnalysis_Listener(); 

// EXERCISE
yourClass.addListener(yourRraListener);

// VERIFY
Mockito.verify(Save_Button, Mockito.times(1)).addActionListener(yourRraListener);
Mockito.verify(Cancel_Button, Mockito.times(1)).addActionListener(yourRraListener);

In this case you need to mock your Save_Button and Cancel_Button in your test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM