user3546762
user3546762

Reputation: 127

skip methods from tested class when using JUnit

I have the following situation. I have a class

A  {

    public void setProps() {
        // setting propertis

        makeSomeWeirdConfigs();

        //setting another properties

    }

    private void makeSomeWeirdConfigs() {
    // making configs
    }

    public Props getProps() {
    //getting properties
    }
}

Now I want to check with the help of JUnit test case if the properties are properly set by using method setProps() and then getting the properties with getProps().

The problem is this method makeSomeWeirdConfigs() in the middle of the setProps(), which when executed without the proper environment cause a lot of exceptions, so the props below this method are not set.

Also this method tries to do dangerous things for example executing some scripts etc. So my question is is it possible to skip this method makeSomeWeirdConfigs() somehow in the JUnit test case, so only setProps() and getProps() are executed.

The original class A should not be changed.

Thanks

Upvotes: 0

Views: 679

Answers (1)

Joop Eggen
Joop Eggen

Reputation: 109547

"Original class should not be changed" - then you are seeking something mocking, byte code fiddling, AOP.

If you made makeSomeWeirdConfig protected, you could override the method and test a child class. The same with testing an extra flag.

If you could put the code in a delegator even better towards sound code.

class SomeWeirdConfig {
    void makeSomeWeirdConfigs() { ... }
}

SomeWeirdConfig delegate = new SomeWeirdConfig();

public void initTest() { delegate = null; }

private void makeSomeWeirdConfigs() {
    if (delegate != null) {
        delegate.makeSomeWeirdConfigs();
    }
}    

Then the test could disable the setter.

Upvotes: 1

Related Questions