简体   繁体   English

Java mockito 带无效语句的测试方法

[英]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,在我的测试过程中,

  1. method creditCheck gets called方法creditCheck被调用
  2. we call the class and method appleService.creditCheck(request, response);我们调用 class 和方法appleService.creditCheck(request, response); to test.去测试。
  3. when credit check is called,the appleHelper object is null.doesnt get initalised.当调用信用检查时, appleHelper object 是 null.doesnt get initalised。
  4. How do I mock it or work around it to get testing done?我如何模拟它或绕过它来完成测试?
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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM