mockito

Mockito 2 参数匹配器

你。 提交于 2020-02-28 12:43:46
Mockito 通过使用 equals() 这种自然的 Java 样式来校验参数值。有时候,当需要有其他一些灵活性的时候,你可能会要求使用参数匹配(argument matchers)。 请参考下面的代码: //stubbing using built-in anyInt() argument matcher when(mockedList.get(anyInt())).thenReturn( "element" ); //stubbing using custom matcher (let's say isValid() returns your own matcher implementation): when(mockedList.contains(argThat(isValid()))).thenReturn( "element" ); //following prints "element" System.out.println(mockedList.get( 999 )); //you can also verify using an argument matcher verify(mockedList).get(anyInt()); //argument matchers can also be written as Java 8 Lambdas verify

推荐10款Java程序员使用的单元测试工具

[亡魂溺海] 提交于 2020-02-28 09:25:02
前言 随着DevOp的不断流行,自动化测试慢慢成为Java开发者的关注点。因此,本文将分享10款优秀的单元测试框架和库,它们可以帮助Java开发人员在其Java项目上编写单元测试和集成测试。 1. JUnit 我绝对 JUnit 不需要太多的介绍了。即使您是Java初学者,也可能听说过它。它可以帮助您为Java代码编写单元测试。 几乎所有主要的IDE(例如Eclipse,NetBeans和 IntelliJIDEA )都提供JUnit集成,这意味着您可以直接从那些IDE编写和运行单元测试。 我们大多数人仍在使用JUnit 4,但 JUnit 5 已经发布。您可以将JUnit用于单元测试和集成测试,它还支持 Java 8 功能。 2. REST Assured REST Assured 是github上一个开源项目。 项目地址: https://github.com/rest-assured/rest-assured 优点: 简约的接口测试DSL 支持xml json的结构化解析 支持xpath jsonpath gpath等多种解析方式 对spring的支持比较全面 3.Selenium Selenium也是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE、Mozilla Firefox、Mozilla

0109 springboot的部署测试监控

為{幸葍}努か 提交于 2020-02-27 03:54:37
springboot的部署测试监控 <a name="SaqiM"></a> 部署 基于maven <a name="uyUMY"></a> 打包 <a name="YTzju"></a> JAR 打包方式一般采用的jar包,使用springboot的默认方式即可; 使用maven命令: mvn clean package -Dmaven.test.skip=true 执行成功之后,可以在对应的target目录下找到对应的包,比如: eg-zuul-0.0.1-SNAPSHOT.jar <a name="BNUeE"></a> WAR <a name="57FgJ"></a> 运行 <a name="2eysI"></a> 内置容器运行 springboot内置了web container容器 tomcat, 可以直接使用 java -jar命令运行; 例如: java -jar xxx/target/eg-zuul-0.0.1-SNAPSHOT.jar <a name="8Fw9B"></a> 外置容器运行 也可使用war的方式,使用外置的tomcat运行,不过代码需要改造一下:<br /> 1 改造打包方式 <br />打包方式改为 war包;<br />在pom.xml中,在version标签的下面添加配置: <package>war</package> 2 添加改造启动代码

Using Mockito with TestNG

巧了我就是萌 提交于 2020-02-27 02:22:21
问题 I took a working test written for JUnit using Mockito and tried to adapt it to work with TestNG but oddly using TestNG only one test will work. I think it is somehow related to the resetting of the mocks but I have played around with trying to call Mockito.reset and using BeforeMethod and BeforeClass and different combinations but still can only get one test to pass. What I need to do to get the test to work? @BeforeClass public void setUp() { MockitoAnnotations.initMocks(this); mockMvc =

Is it possible to do strict mocks with Mockito?

折月煮酒 提交于 2020-02-26 11:36:26
问题 I'd like to use strict mocks, at least when developing for the first time some tests against old code, so any methods invoked on my mock will throw an exception if I didn't specifically define expectations. From what I've come to see, Mockito if I didn't define any expectations will just return null, which will later on cause a NullPointerException in some other place. Is it possible to do that? If yes, how? 回答1: What do you want it to do? You can set it to RETURN_SMART_NULLS, which avoids

Is it possible to do strict mocks with Mockito?

人盡茶涼 提交于 2020-02-26 11:35:28
问题 I'd like to use strict mocks, at least when developing for the first time some tests against old code, so any methods invoked on my mock will throw an exception if I didn't specifically define expectations. From what I've come to see, Mockito if I didn't define any expectations will just return null, which will later on cause a NullPointerException in some other place. Is it possible to do that? If yes, how? 回答1: What do you want it to do? You can set it to RETURN_SMART_NULLS, which avoids

Junit throwing java.lang.AbstractMethodError: javax.ws.rs.core.Response$ResponseBuilder.status

牧云@^-^@ 提交于 2020-02-25 02:23:28
问题 Currently am writing Junit for our webservice code. WebService Code and Junit code is written in the code sectioon When I run the Junit am getting the below error java.lang.AbstractMethodError: javax.ws.rs.core.Response$ResponseBuilder.status(ILjava/lang/String;)Ljavax/ws/rs/core/Response$ResponseBuilder; at javax.ws.rs.core.Response$ResponseBuilder.status(Response.java:921) at javax.ws.rs.core.Response.status(Response.java:592) at javax.ws.rs.core.Response.status(Response.java:603) at javax

Mockito testing events

↘锁芯ラ 提交于 2020-02-24 09:11:36
问题 I have a class which generates events e.g. public class EventSource{ public addEventListener(EventListener listener)..... public raiseEvent(){ Event e=.... listener.handle(e); } } I'm using Mockito to mock an EventListener and want to do an equality on the event object (e.g. event.getTime()..event.getMessage() etc). The event object doesnt have an equals method so I cant easily create another object and do an assert. Whats the normal way of doing this with Mockito? 回答1: It looks like you want

Mockito testing events

假如想象 提交于 2020-02-24 09:11:34
问题 I have a class which generates events e.g. public class EventSource{ public addEventListener(EventListener listener)..... public raiseEvent(){ Event e=.... listener.handle(e); } } I'm using Mockito to mock an EventListener and want to do an equality on the event object (e.g. event.getTime()..event.getMessage() etc). The event object doesnt have an equals method so I cant easily create another object and do an assert. Whats the normal way of doing this with Mockito? 回答1: It looks like you want

Mocking Sftp class skip method call

偶尔善良 提交于 2020-02-23 03:54:26
问题 public class SFTP { public Map<Report, TransferStatus> transfer(List<Report> reports) { //testing logic here } private ChannelSftp channelSftp; private Session session; private TransferStatus send(File file) { connect(); send(stream, file.getName()); } private void send(FileInputStream stream, String name) throws SftpException, IOException { channelSftp.put(stream, fileNameWithId, new SftpLogMonitor(), ChannelSftp.OVERWRITE); stream.close(); } private void connect() throws JSchException { if