简体   繁体   中英

How mock private method that modify private variables?

How mock private method that modify private variables?

class SomeClass{
    private int one;
    private int second;

    public SomeClass(){}

    public int calculateSomething(){
        complexInitialization();
        return this.one + this.second;
    }

    private void complexInitialization(){
        one = ...
        second = ...
    }
}

You don't , because your test will depend on implementation details of the class it is testing and will therefore be brittle. You could refactor your code such that the class you are currently testing depends on another object for doing this calculation. Then you can mock this dependency of the class under test. Or you leave the implementation details to the class itself and sufficiently test it's observable behavior.

The problem you could suffer from is that you are not exactly separating commands and queries to your class. calculateSomething looks more like a query, but complexInitialization is more of a command.

Provided the fact that other answers are pointing out that such test cases are brittle and that the test cases should not be based on implementation and should be dependent on the behavior if you still want to mock them then here are some ways:

PrivateMethodDemo tested = createPartialMock(PrivateMethodDemo.class,
                                "sayIt", String.class);
String expected = "Hello altered World";
expectPrivate(tested, "sayIt", "name").andReturn(expected);
replay(tested);
String actual = tested.say("name");
verify(tested);
assertEquals("Expected and actual did not match", expected, actual);

This is how you would do it with PowerMock.

expectPrivate() of PowerMock does this.

Test cases from PowerMock which test the private method mocking

UPDATE: Partial Mocking with PowerMock there are some disclaimers and catches

class CustomerService {

    public void add(Customer customer) {
        if (someCondition) {
            subscribeToNewsletter(customer);
        }
    }

    void subscribeToNewsletter(Customer customer) {
        // ...subscribing stuff
    }
}

Then you create a PARTIAL mock of CustomerService, giving a list of methods you want to mock.

CustomerService customerService = PowerMock.createPartialMock(CustomerService.class, "subscribeToNewsletter");
customerService.subscribeToNewsletter(anyObject(Customer.class));

replayAll();

customerService.add(createMock(Customer.class));

So add() within the CustomerService mock is the REAL thing you want to test and for the method subscribeToNewsletter() you now can write an expectation as usual.

Power mock might help you here. But generally I would make the method protected and override the previously-private method to do what ever I wanted it to do.

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