为什么我们需要在同一班级的每个测试案例之前模拟静态课程
我有一个测试班,有两个测试用例。我正在使用Junit 4。我的这些测试用例正在使用静态类。因此,我用@beforeclass方法模拟了一个静态类,以便在执行测试用例开始之前仅嘲笑一次。但是在这种情况下,只有第一个测试案例效果很好,其余所有测试用例都会失败。因此,我在@before方法中嘲笑我的静态类,以便在每个测试案例执行之前被模拟。因此,我想了解为什么需要在每个测试案例之前嘲笑静态类,为什么我们不能在执行课程之前只模拟一次。
import com.walmart.fulfillment.encryption.logging.HttpThreadBusinessContext;
import com.walmart.rxorderdetailsfulfillment.data.LabelOverFlowOrderRepo;
import com.walmart.rxorderdetailsfulfillment.models.entity.LabelOverFlowOrder;
import com.walmart.rxorderdetailsfulfillment.models.entity.LabelOverFlowPK;
import com.walmart.rxorderdetailsfulfillment.models.request.LabelOverFlowRequest;
import com.walmart.rxorderdetailsfulfillment.models.response.LabelOverFlowResponse;
import com.walmart.rxorderdetailsfulfillment.util.LabelOverFlowUtility;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.junit.Assert.*;
import static org.mockito.Mockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({LabelOverFlowUtility.class,HttpThreadBusinessContext.class,LabelOverFlowOrderRepo.class,LabelOverFlowResponse.class,LabelOverFlowOrder.class,LabelOverFlowPK.class})
public class LabelOverFlowRequestHandlerTest {
static int count =0;
LabelOverFlowRequest labelOverFlowRequest;
@InjectMocks
LabelOverFlowRequestHandler labelOverFlowRequestHandler;
@Mock
HttpThreadBusinessContext httpThreadBusinessContext;
@Mock
LabelOverFlowOrderRepo labelOverFlowOrderRepo;
@Mock
LabelOverFlowResponse labelOverFlowResponse;
@Mock
LabelOverFlowOrder labelOverFlowOrder;
@Mock
LabelOverFlowPK labelOverFlowPK;
@BeforeClass
public static void initialization(){
//PowerMockito.mockStatic(LabelOverFlowUtility.class);
}
@Before
public void initialization_Before_Every_Test(){
PowerMockito.mockStatic(LabelOverFlowUtility.class);
labelOverFlowRequest = LabelOverFlowRequest.builder().textOverFlow(true).warningOverFlow(true).rxFillId(555).siteNbr(5550).countryCode("US").build();
}
/**
* This test case is use to check success is returned after saving data to DB.
*/
@Test
public void processSaveLabelOverFlowResquestWithSuccess() {
when(LabelOverFlowUtility.getLabelOverFlowPK(Mockito.any())).thenReturn(labelOverFlowPK);
when(LabelOverFlowUtility.getLabelOverFlowOrder(Mockito.any())).thenReturn(labelOverFlowOrder);
when(labelOverFlowOrderRepo.save(Mockito.any())).thenReturn(labelOverFlowResponse);
when(LabelOverFlowUtility.getLabelOverFlowResponse(Mockito.any())).thenCallRealMethod();
Mockito.doReturn(labelOverFlowOrder).when(labelOverFlowOrderRepo).save(Mockito.any());
assertTrue(labelOverFlowRequestHandler.processSaveLabelOverFlowResquest(labelOverFlowRequest).getResponseText().equals("success"));
}
/**
* This test case is ued to check if data is not saved to DB
*/
@Test
public void processSaveLabelOverFlowResquestWithFailure() {
when(LabelOverFlowUtility.getLabelOverFlowPK(Mockito.any())).thenReturn(labelOverFlowPK);
when(LabelOverFlowUtility.getLabelOverFlowOrder(Mockito.any())).thenReturn(labelOverFlowOrder);
when(labelOverFlowOrderRepo.save(Mockito.any())).thenReturn(labelOverFlowResponse);
when(LabelOverFlowUtility.getLabelOverFlowResponse(Mockito.any())).thenCallRealMethod();
Mockito.doThrow(new RuntimeException()).when(labelOverFlowOrderRepo).save(Mockito.any());
assertTrue(labelOverFlowRequestHandler.processSaveLabelOverFlowResquest(labelOverFlowRequest).getResponseText().equals("failure"));
}
}
I have a test class with two test cases . I am using Junit 4. My these test cases are using static class. So I mocked a static class in @BeforeClass method so that it is only mocked once before the start of execution of test cases. but in this case only first test case works fine and rest of all test cases fails. so for that I mocked my static class in @Before method so that it is been mocked before every test case execution. So I want to understand that why is there need to mock the static class before every test case and why cant we mock it only once before start of execution of class.
import com.walmart.fulfillment.encryption.logging.HttpThreadBusinessContext;
import com.walmart.rxorderdetailsfulfillment.data.LabelOverFlowOrderRepo;
import com.walmart.rxorderdetailsfulfillment.models.entity.LabelOverFlowOrder;
import com.walmart.rxorderdetailsfulfillment.models.entity.LabelOverFlowPK;
import com.walmart.rxorderdetailsfulfillment.models.request.LabelOverFlowRequest;
import com.walmart.rxorderdetailsfulfillment.models.response.LabelOverFlowResponse;
import com.walmart.rxorderdetailsfulfillment.util.LabelOverFlowUtility;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.junit.Assert.*;
import static org.mockito.Mockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({LabelOverFlowUtility.class,HttpThreadBusinessContext.class,LabelOverFlowOrderRepo.class,LabelOverFlowResponse.class,LabelOverFlowOrder.class,LabelOverFlowPK.class})
public class LabelOverFlowRequestHandlerTest {
static int count =0;
LabelOverFlowRequest labelOverFlowRequest;
@InjectMocks
LabelOverFlowRequestHandler labelOverFlowRequestHandler;
@Mock
HttpThreadBusinessContext httpThreadBusinessContext;
@Mock
LabelOverFlowOrderRepo labelOverFlowOrderRepo;
@Mock
LabelOverFlowResponse labelOverFlowResponse;
@Mock
LabelOverFlowOrder labelOverFlowOrder;
@Mock
LabelOverFlowPK labelOverFlowPK;
@BeforeClass
public static void initialization(){
//PowerMockito.mockStatic(LabelOverFlowUtility.class);
}
@Before
public void initialization_Before_Every_Test(){
PowerMockito.mockStatic(LabelOverFlowUtility.class);
labelOverFlowRequest = LabelOverFlowRequest.builder().textOverFlow(true).warningOverFlow(true).rxFillId(555).siteNbr(5550).countryCode("US").build();
}
/**
* This test case is use to check success is returned after saving data to DB.
*/
@Test
public void processSaveLabelOverFlowResquestWithSuccess() {
when(LabelOverFlowUtility.getLabelOverFlowPK(Mockito.any())).thenReturn(labelOverFlowPK);
when(LabelOverFlowUtility.getLabelOverFlowOrder(Mockito.any())).thenReturn(labelOverFlowOrder);
when(labelOverFlowOrderRepo.save(Mockito.any())).thenReturn(labelOverFlowResponse);
when(LabelOverFlowUtility.getLabelOverFlowResponse(Mockito.any())).thenCallRealMethod();
Mockito.doReturn(labelOverFlowOrder).when(labelOverFlowOrderRepo).save(Mockito.any());
assertTrue(labelOverFlowRequestHandler.processSaveLabelOverFlowResquest(labelOverFlowRequest).getResponseText().equals("success"));
}
/**
* This test case is ued to check if data is not saved to DB
*/
@Test
public void processSaveLabelOverFlowResquestWithFailure() {
when(LabelOverFlowUtility.getLabelOverFlowPK(Mockito.any())).thenReturn(labelOverFlowPK);
when(LabelOverFlowUtility.getLabelOverFlowOrder(Mockito.any())).thenReturn(labelOverFlowOrder);
when(labelOverFlowOrderRepo.save(Mockito.any())).thenReturn(labelOverFlowResponse);
when(LabelOverFlowUtility.getLabelOverFlowResponse(Mockito.any())).thenCallRealMethod();
Mockito.doThrow(new RuntimeException()).when(labelOverFlowOrderRepo).save(Mockito.any());
assertTrue(labelOverFlowRequestHandler.processSaveLabelOverFlowResquest(labelOverFlowRequest).getResponseText().equals("failure"));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
labeloverFlowUtility.class
是静态类,如果方法是确定性的,则不需要完全模拟它(给定相同的输入发生了相同的事情)。我可能会误解这堂课的运作方式。
LabelOverFlowUtility.class
is a static class, there should be no need to mock it at all if the methods are deterministic(given same input same thing happens).I may be misunderstanding how this class operates.