Given the following class:

public class ClassToBeTest{

${property.utility}

private String property;

private Utility utility;

public ClassToBeTested(Utility utility){

this.utility = utility;

}

public void doSomething(){

utility.doSomething(property);

}

}

We want to Mock Utility (we are using Mockito for instance) and verify that utility.doSomething is called passing the property parameter.

We don't really care about the config value (we have other tests for that). But we don't want to open the class with a method like (or passing the property by constructor).

public void setPropertyForTest(String property){

this.property = property;

}

Is there other ways to check that this private property (with no value) is passed to Utility?

Thanks a lot.

有帮助吗?

解决方案

If you just want to set the private field, Spring (not Sprint) provides support for that in the spring-test module. See the setField() variants in ReflectionTestUtils.

Regards,

Sam

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top