Springboot Mockito Junit 5 Command Builder模式服务单元测试
我正在为我为服务类编写的代码编写单元测试。这是我班级的简短部分,因为我有一个问题:
public interface Command { public void execute(); }
public class MyServiceClass {
private void doBusinessLogicValidation() {...}
private void doBusinessLogicValidation2() {...}
public void myMethod() {
doBusinessLogicValidation();
doBusinessLogicValidation2();
Command myCommand = callPrivateMethodToCreateMyCommand();
myCommand.execute();
}
private Command callPrivateMethodToCreateMyCommand() {
MyObject commandObject = MyObject.builder().add("something").additional("else").build();
return commandObject;
}
}
上述代码很好,没有问题,并且正确使用Java访问修饰符:简短而简洁。对于单元测试,我需要覆盖CallprivateMethodTocreateMyCommand,但它是(正确)私有的。 PowerMock不支持Junit 5。
我可以将CallPrivateMethodCocreateMyCommand()更改为公众,并轻松使用Junit的()...但这打破了封装,也打破了使用私人的原因。并且不想使用受保护。
我的测试单位是myServiceClass,而不是mycommand的execute()方法。我想念什么?
任何帮助将不胜感激。
谢谢,吉姆
I'm writing the unit test to code that I wrote for a service class. Here is the short part of my class for the question(s) I have:
public interface Command { public void execute(); }
public class MyServiceClass {
private void doBusinessLogicValidation() {...}
private void doBusinessLogicValidation2() {...}
public void myMethod() {
doBusinessLogicValidation();
doBusinessLogicValidation2();
Command myCommand = callPrivateMethodToCreateMyCommand();
myCommand.execute();
}
private Command callPrivateMethodToCreateMyCommand() {
MyObject commandObject = MyObject.builder().add("something").additional("else").build();
return commandObject;
}
}
The above code is fine and works w/ no problem and uses Java access modifiers correctly: short and concise. For the unit test, I need to override the callPrivateMethodToCreateMyCommand, but it is (correctly) private. PowerMock does not support JUnit 5.
I can change the callPrivateMethodToCreateMyCommand() to public and easily use JUnit's when()...thenReturn() semantics. BUT that breaks encapsulation and the reason why one would use private. And do not want to use protected.
My unit of test is the MyServiceClass and NOT into the execute() method of myCommand. What am I missing?
Any help would be appreciated.
Thanks,Jim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论