Unit testing overridden methods which call super()

为君一笑 提交于 2020-01-04 02:17:19

问题


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 class 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.

回答1:


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.



来源:https://stackoverflow.com/questions/6645263/unit-testing-overridden-methods-which-call-super

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!