[英]Java mockito test method with void statements
Below is my AppleServiceImpl
class and a test class AppleServiceImpl
which I created.下面是我的AppleServiceImpl
class 和我创建的测试 class AppleServiceImpl
。 During my testing,在我的测试过程中,
creditCheck
gets called方法creditCheck
被调用appleService.creditCheck(request, response);
我们调用 class 和方法appleService.creditCheck(request, response);
to test.去测试。appleHelper
object is null.doesnt get initalised.当调用信用检查时, appleHelper
object 是 null.doesnt get initalised。public class AppleServiceImpl extends GenericService implements ApplicationService {
@Autowired
AppleHelper appleHelper;
public void creditCheck(ApplicationRequest request, ResponseMessage<ApplicationResponse> response) {
appleHelper.test123(request, response);
}
}
public class AppleServiceImplTest {
@Spy
AppleServiceImpl appleService= new AppleServiceImpl ();
@Test
public void creditCheck() {
appleService.creditCheck(request, response);
}
}
You probably want to add a constructor to AppleServiceImpl
that takes AppleHelper as a parameter and autowire on the constructor.您可能想向AppleServiceImpl
添加一个构造函数,它将 AppleHelper 作为参数并在构造函数上自动装配。 Next in your test you want to create an @Mock AppleHelper appleHelper
field that you inject using @InjectMocks
接下来在您的测试中,您想要创建一个@Mock AppleHelper appleHelper
字段,您使用@InjectMocks
注入该字段
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.