问题标签 [springmockito]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
1175 浏览

spring - 纠缠弹簧上下文中的单元测试

给定一个遗留应用程序有 1500 个 spring.xmls。我想为服务编写单元测试。我深陷依赖地狱。我必须按原样接受该应用程序,没有出路。

所以我们使用 spring-3.something 和 mockito-1.9,我想要测试服务的好方法。较新的代码大量使用@Autowired 注解。

间接地,该服务使用了我实际想要在测试中使用的 ~25 个助手(工厂方法等),以及 ~25 个我对此测试不感兴趣的对象。

我目前尝试以上述方式设置上下文,但我对@Mock、@InjectMocks、@Autowired 的影响感到困惑。

我的测试如下。我需要帮助才能正确设置。

问题:

  • @InjectMocks 的实际效果是什么?
  • 我如何才能决定(技术上)真正使用哪些自动装配的 bean,哪些被模拟替换?
  • 我知道,我在滥用模拟来获取假货。有没有一种更简单的方法可以在单线中获取假货?
  • *请注意,我想了解这一点,因为我有大量此类服务... *

这是我的示例:

0 投票
2 回答
2577 浏览

spring - Junit Mockito with spring - Json String 返回的测试用例

我正在学习 Junit Mockito 以在 Intellij 中使用 Spring 3.2 测试 spring-mvc 控制器。我的控制器是

我所做的是我有一种表格可以按名称搜索用户。UserClient 对象是一个会话属性,我试图为我的控制器编写一个 junit 测试用例

如何测试我的getUserByName方法以及如何添加会话属性?请任何人都可以帮助我编写测试用例,并对该方法进行可能的测试。提前致谢

0 投票
1 回答
18761 浏览

spring-mvc - Spring MVC 控制器异常测试

我有以下代码

我正在尝试在引发 NotFoundException 时进行单元测试,所以我编写了这样的代码

但是失败了。有什么建议如何测试异常吗?

或者我应该在 CategoryService 中抛出异常,这样我就可以做这样的事情

0 投票
2 回答
5484 浏览

java - Springockito 怎么用?

我想在我的一个 IT 中使用 Springockito 模拟 DAO bean。在我的 IT 中,我必须使用 spring context.xml 来自动装配一些服务,还必须使用 mockApplication.xml 来模拟 DAO。那么,如何同时使用这两个 xml 配置文件呢?

我已将模拟上下文包含为 @ContextConfiguration(loader = SpringockitoContextLoader.class, locations = {"classpath*:/MockApplicationContext.xml"})

但我也必须包括春天的背景@ContextConfiguration(locations = {"classpath*:/testApplicationContext.xml"})

问候拉吉布

0 投票
1 回答
3627 浏览

java - 使用 mockito 模拟休息客户端类抛出空指针异常

嗨,我有一个类RestClientUtil,它与服务器执行所有与连接相关的操作,并使用休息服务获得响应。当我尝试模拟我的 restClientUtil 成功模拟但当我尝试调用此类的方法时,方法将返回响应为 null。当我在调试模式下看到 restClientUtil 变量时,此类的属性(如pbsOrderStatusUrl sellerCode等)为 null 可能是因为这个原因响应为空,所以请告诉我如何编写OrderStatusUpdate基于 RestClientUtil 类响应的我的 mockito

这是 RestClientUtilClass 的方法

这是我调用 RestClientUtil 方法的类

这是我试图实现的模拟测试

这是错误日志

0 投票
2 回答
20651 浏览

junit - JUnit: Unable to mock the RestTemplate object to call postForObject method

I am new to Mockito as well as Spring's RestTemplate. I am working on JUnit tests for a functionality which sends a request to a web-service and gets the response through the use of RestTemplate. I want the server to respond with a response that i want so that i can test the functionalities based on this response. I am using Mockito for mocking.

I am not sure where I am going wrong. Am I not creating proper mocks? Is my JSON object mapper not been configured properly?

Configuration file defining the RestTemplate bean:

My DTO's:

Class containing the method to test:

The JUnit test class

Getting the following exception:

org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: No serializer found for class org.mockito.internal.stubbing.defaultanswers.GloballyConfiguredAnswer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.biogenidec.dto.TypedUserDTO$$EnhancerByMockitoWithCGLIB$$bee3c447["callbacks"]->org.mockito.internal.creation.MethodInterceptorFilter["handler"]->org.mockito.internal.handler.InvocationNotifierHandler["mockSettings"]->org.mockito.internal.creation.settings.CreationSettings["defaultAnswer"]); nested exception is org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.mockito.internal.stubbing.defaultanswers.GloballyConfiguredAnswer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.biogenidec.dto.TypedUserDTO$$EnhancerByMockitoWithCGLIB$$bee3c447["callbacks"]->org.mockito.internal.creation.MethodInterceptorFilter["handler"]->org.mockito.internal.handler.InvocationNotifierHandler["mockSettings"]->org.mockito.internal.creation.settings.CreationSettings["defaultAnswer"])

And:

0 投票
1 回答
5799 浏览

spring-mvc - 你怎么可能在 mockito,spring mvc 环境中为 boolean 编写测试用例

我怎么可能在 mockito、spring mvc 环境中为 boolean 编写测试用例

例如,像下面的响应

我们会像这样编写测试用例,

对?但是,当我们得到如下响应时

我应该如何编写测试用例?

0 投票
1 回答
2493 浏览

java - 如何在junit中为mockservletcontext设置真实路径

我需要为 mockservletcontext 设置真实路径,以便我可以使用

getServletContext().getRealPath("/")。

这是我在 Junit 中使用的以下代码

但仍然 getServletContext().getRealPath("/") 返回 null。

为 mockservlet 设置路径有什么建议吗?

0 投票
2 回答
10368 浏览

java - 用于集成测试的模拟 JmsTemplate

需要模拟 JmsTemplate 以在我的应用程序中进行集成测试。

在我的 appcontext.xml

需要在我的 testcontext.xml 中模拟 jmstemplet。提前致谢。

0 投票
1 回答
553 浏览

java - 控制器的 Junit 正在编译但显示错误

我正在使用一个遵循 Spring MVC 模式的开源项目 petclininc 来使用 spring 模拟服务编写 Junit 测试用例。

我从一个独立的控制器开始

和我的junit测试用例:

当我尝试运行此测试用例时,我将其更改为 spring/mvc-core-config.xml 和