开发者

Unit testing overridden methods which call super()

开发者 https://www.devze.com 2023-03-19 11:46 出处:网络
I\'m trying to figure out the best way of writing a unit test for an overridden method which calls super() as the last step. Basically, I want to massage parameters before they\'re used in the base cl

I'm trying to figure out the best way of writing a unit test for an overridden method which calls super() as the last step. Basically, I want to massage parameters before they're used in the base class.

Here's an example of a method:

    @Override
    public JobExecution run(Job job, JobParameters passedParams) 
            throws JobExecutionAlreadyRunningException, JobRestartException {

        JobParameters launchParameters;

        if(passedParams.isEmpty()) {
            launchParameters = jobParameterSetter.getJobParameters();
        } else {
            launchParameters = passedParams;
        }

        return super.run(job, launchParameters);
    }

What it comes down to is that I can't find a seam to check the parameters that are in the eventual super.run() call, which is all I want to test. The base class has several dependencies, and it's really not necessary to test that class (not to mention a lot more work).

Composition is a solution, but it's a fairly complicated base class and I need to expose the whole thing while only overriding a couple of methods.

I'm also considering faking the base cla开发者_运维百科ss for testing, which is simple enough, but I'm not sure how to change the classpath just for running the unit test (Eclipse for single tests; Maven for build testing -- maybe a question on its own?).


I have to imagine this has already been asked, but I can't find an exact match.


It seems that the logic you are wanting to test is the if-else flow, and NOT the call to the base class. I would do this by creating a protected method called getLaunchParameters(JobParameters jobParameters), which does the bit of logic you are interested in testing.

This way, the logic you want tested is tested, the logic you are not interested in testing is not tested, and you have broken out a piece of code that may be able to be used else where in the future.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号