Tag: mocking

Java模拟框架如何工作?

这不是一个关于哪个是最佳框架等的问题。 我从未使用过模拟框架,我对这个想法感到有点困惑。 它是如何知道如何创建模拟对象的? 它是在运行时完成还是生成文件? 你怎么知道它的行为? 最重要的是 – 使用这样一个框架的工作流程是什么(创建测试的步骤是什么)? 谁有人解释一下? 您可以选择您喜欢的框架,例如,说出它是什么。

使用Mockito模拟方法的局部变量

我有一个需要经过测试的A级课程。 以下是A的定义: public class A { public void methodOne(int argument) { //some operations methodTwo(int argument); //some operations } private void methodTwo(int argument) { DateTime dateTime = new DateTime(); //use dateTime to perform some operations } } 并且基于dateTime值,一些数据将被操纵,从数据库中检索。 对于此数据库,值通过JSON文件保留。 这使事情变得复杂。 我需要的是在测试时将dateTime设置为某个特定日期。 有没有办法可以使用mockito模拟局部变量的值?

如何在Junit中使用@InjectMocks和@Autowired注释

我有一个A类,它使用3个不同的类和自动assembly public class A () { @Autowired private B b; @Autowired private C c; @Autowired private D d; } 在测试它们时,我希望只有2个类(B&C)作为模拟,并且将D类自动assembly为正常运行,此代码对我不起作用: @RunWith(MockitoJUnitRunner.class) public class aTest () { @InjectMocks private A a; @Mock private B b; @Mock private C c; @Autowired private D d; } 甚至可以这样做吗?

@Mock注释后,mock实例为null

我尝试运行此测试: @Mock IRoutingObjHttpClient routingClientMock; @Mock IRoutingResponseRepository routingResponseRepositoryMock; @Test public void testSendRoutingRequest() throws Exception { CompleteRoutingResponse completeRoutingResponse = new CompleteRoutingResponse(); completeRoutingResponse.regression_latencyMillis = 500L; Mockito.when(routingClientMock.sendRoutingRequest(any(RoutingRequest.class))).thenReturn(completeRoutingResponse); RoutingObjHttpClientWithReRun routingObjHttpClientWithReRun = new RoutingObjHttpClientWithReRun (routingClientMock, routingResponseRepositoryMock); … } 但我得到NullPointerException: Mockito.when(routingClientMock. 我错过了什么?

powermockito:如何在枚举中模拟抽象方法

考虑以下(简化)枚举: MyEnum { ONE public int myMethod() { // Some complex stuff return 1; }, TWO public int myMethod() { // Some complex stuff return 2; }; public abstract int myMethod(); } 这用于以下function: void consumer() { for (MyEnum n : MyEnum.values()) { n.myMethod(); } } 我现在想为consumer编写一个unit testing,在每个枚举实例中模拟对myMethod()的调用。 我尝试过以下方法: @RunWith(PowerMockRunner.class) @PrepareForTest(MyEnum.class) public class MyTestClass { @Test […]

PowerMock Mockito:如何模拟所有静态方法?

在使用PowerMock(使用Mockito)时,我们是否需要模拟类的所有静态方法? 我的意思是,假设我们有: class MockMe { public static MockMe getInstance(){ //return new Instance via complex process; } public static List anotherStaticMethod(){ // does xyz } } 我的问题是,如果我需要模拟getInstance方法,是否有必要模拟“anotherStaticMethod”? PowerMock版本:1.3,Mockito版本:1.8

JMock允许其他方法调用

我正在使用JMock来测试使用对象的类的行为。 我想测试方法a()被调用。 但是, b()和c()也会在对象上调用。 因此,如果我期望a() ,它还必须期望b()和c()使测试通过。 有没有办法只测试某种方法,并允许其他任何方法?

如何使用mockMvc检查响应体中的JSON

这是我的控制器内部的方法,由@Controller注释 @RequestMapping(value = “/getServerAlertFilters/{serverName}/”, produces = “application/json; charset=utf-8”) @ResponseBody public JSONObject getServerAlertFilters(@PathVariable String serverName) { JSONObject json = new JSONObject(); List filteredAlerts = alertFilterService.getAlertFilters(serverName, “”); JSONArray jsonArray = new JSONArray(); jsonArray.addAll(filteredAlerts); json.put(SelfServiceConstants.DATA, jsonArray); return json; } 我期待{“data”:[{“useRegEx”:”false”,”hosts”:”v2v2v2″}]}作为我的json。 这是我的JUnit测试: @Test public final void testAlertFilterView() { try { MvcResult result = this.mockMvc.perform(get(“/getServerAlertFilters/v2v2v2/”).session(session) .accept(“application/json”)) .andDo(print()).andReturn(); String content = […]

如何测试该方法是否添加了redirectAttributes?(由MockMvc提供)

我写过这个方法: @RequestMapping(value=”/someURL”, method=GET) public String myMethod(RedirectAttributes redirectAttributes) { redirectAttributes.addAttribute(“rd”, “rdValue”); redirectAttributes.addFlashAttribute(“fa”, faValue); return “redirect:/someOtherURL”; } 测试代码: @Test public void myMethod() throws Exception{ MockHttpServletRequestBuilder request = MockMvcRequestBuilders .get(“/someURL”); ResultActions result = mockMvc.perform(request); } 如果我调用测试执行,我看到: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public java.lang.String com.epam.hhsystem.web.controllers.VacancyMenuController.myMethod(org.springframework.web.servlet.mvc.support.RedirectAttributes)]; nested exception is java.lang.IllegalStateException: Argument [RedirectAttributes] […]

单元从Groovy测试用例测试Java类中的静态方法

我试图在groovy中编写一个用java编写的类的测试用例。 Java类(名称:Helper)中有一个静态方法,其中获取HttpClient对象并在其上调用executeMethod。 为了对这个类进行Unittest,我试图在groovy测试用例中模拟这个httpClient.executeMethod(),但是无法正确模拟它。 下面是Java类 public class Helper{ public static message(final String serviceUrl){ HttpClient httpclient = new HttpClient(); HttpMethod httpmethod = new HttpMethod(); // the below is the line that iam trying to mock String code = httpClient.executeMethod(method); } } 关于如何从groovyunit testing这个静态方法的任何想法。 由于httpClient对象是类方法中的对象,我如何在groovy测试用例中模拟这个对象? 这是我到目前为止的测试用例。我试图模拟为null,但没有发生…… void testSendMessage(){ def serviceUrl = properties.getProperty(“ITEM”).toString() // mocking to return null def […]