如何使用 Mockito 测试Java列表接口方法?
Mockito是一个开源测试框架,用于对Java应用程序进行单元测试。它在开发可测试的应用程序中起着至关重要的作用。 Mockito 用于模拟接口,以便可以将虚拟功能添加到可用于单元测试的模拟接口中。 Java中的 List 接口提供了一种存储有序集合的方法。它是 Collection 的子接口。它是可以存储重复值的对象的有序集合。 Java List 接口中有许多可用的方法,例如
- 添加()
- 放()
- 消除()
- 尺寸()
- get() 等等。
所以在本文中,我们将在 Mockito 和 Junit 框架的帮助下测试一些方法。
分步实施
第 1 步:创建一个 Maven 项目
在您喜欢的Java IDE 中创建一个 maven 项目(在本文中,我们使用 IntelliJ IDEA)
第 2 步:成功创建 maven 项目后,您必须在 pom.xml 文件中添加一些依赖项。您必须在 pom.xml 文件中添加以下依赖项。
Mockito 的依赖为 如下:
XML
org.mockito
mockito-all
2.0.2-beta
XML
junit
junit
4.8.2
XML
4.0.0
org.example
mockito-demo
1.0-SNAPSHOT
junit
junit
4.8.2
org.mockito
mockito-all
2.0.2-beta
11
11
Java
List mockList = mock(List.class);
Java
when(mockList.size()).thenReturn(2);
Java
assertEquals(2, mockList.size());
Java
@Test
public void mockListSizeMethod(){
// Your test code
}
Java
// Java Program to Test code for the list.size() Method
// @Test tells the JUnit that the public void method
// in which it is used can run as a test case
@Test public void mockListSizeMethod()
{
// Mock the List Class
List mockList = mock(List.class);
// when is used when we want to mock to return specific
// values when particular methods are called and
// thenReturn() methods lets you define the return value
// when a particular method of the mocked object is been
// called
when(mockList.size()).thenReturn(2);
// assertEquals is used to check if
// two objects is equally defined or not
assertEquals(2, mockList.size());
}
Java
@Test
public void mockListSizeMethod_multipleValues(){
List mockList = mock(List.class);
when(mockList.size()).thenReturn(2).thenReturn(3);
assertEquals(2, mockList.size());
assertEquals(3, mockList.size());
}
Java
@Test
public void mockListGetMethod(){
List mockList = mock(List.class);
// Argument Matcher
when(mockList.get(anyInt())).thenReturn(2);
assertEquals(2, mockList.get(anyInt()));
}
Java
@Test(expected = RuntimeException.class)
public void mockListGetMethod_throwAnException(){
List mockList = mock(List.class);
when(mockList.get(anyInt())).thenThrow(new RuntimeException("Something Wrong"));
mockList.get(anyInt());
}
Java
// Java Program to Illustrate JavaListMock File
// Importing required classes
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.List;
import org.junit.Test;
// Class
public class JavaListMock {
// @Test annotation tells the JUnit that
// the public void method
// in which it is used can run as a test case
@Test public void mockListSizeMethod()
{
// Mock the List Class
List mockList = mock(List.class);
// when is used when we want to mock to return
// specific values when particular methods are
// called and thenReturn() methods lets you define
// the return value when a particular method of the
// mocked object is been called
when(mockList.size()).thenReturn(2);
// assertEquals is used to check if
// two objects is equally defined or not
assertEquals(2, mockList.size());
}
// Method
@Test public void mockListSizeMethod_multipleValues()
{
List mockList = mock(List.class);
when(mockList.size()).thenReturn(2).thenReturn(3);
assertEquals(2, mockList.size());
assertEquals(3, mockList.size());
}
// Method
@Test public void mockListGetMethod()
{
List mockList = mock(List.class);
// Argument Matcher
when(mockList.get(anyInt())).thenReturn(2);
assertEquals(2, mockList.get(anyInt()));
}
// Method
@Test(expected = RuntimeException.class)
public void mockListGetMethod_throwAnException()
{
List mockList = mock(List.class);
when(mockList.get(anyInt()))
.thenThrow(
new RuntimeException("Something Wrong"));
mockList.get(anyInt());
}
}
Junit 的依赖为 如下:
XML
junit
junit
4.8.2
实现:下面是 pom.xml 文件的完整代码
XML
4.0.0
org.example
mockito-demo
1.0-SNAPSHOT
junit
junit
4.8.2
org.mockito
mockito-all
2.0.2-beta
11
11
第 3 步:测试Java列表接口方法
为了测试Java列表接口方法,我们必须在src > test > Java > 右键单击 > New > Java Class文件夹中创建一个测试类,如下图所示。在这里,我们将类命名为JavaListMock 。
第 4 步:现在我们要为Java列表接口方法编写测试用例。
测试 list.size() 方法
4.1: Mockito 为我们提供了mock()方法来模拟 List 类。
Java
List mockList = mock(List.class);
4.2: Mockito 为我们提供了另外两个方法when()和thenReturn() 。当我们想要在调用特定方法时模拟返回特定值时使用 when() ,然后当调用模拟对象的特定方法时,thenReturn() 方法允许您定义返回值。
Java
when(mockList.size()).thenReturn(2);
4.3: Junit 为我们提供了assertEquals()用于检查两个对象是否相等定义。
Java
assertEquals(2, mockList.size());
4.4:必须在公共 void 方法中编写上面的 3 行,并且必须使用@Test注释该方法。 @Test 注释告诉 JUnit 使用它的公共 void 方法可以作为测试用例运行。
Java
@Test
public void mockListSizeMethod(){
// Your test code
}
例子:
Java
// Java Program to Test code for the list.size() Method
// @Test tells the JUnit that the public void method
// in which it is used can run as a test case
@Test public void mockListSizeMethod()
{
// Mock the List Class
List mockList = mock(List.class);
// when is used when we want to mock to return specific
// values when particular methods are called and
// thenReturn() methods lets you define the return value
// when a particular method of the mocked object is been
// called
when(mockList.size()).thenReturn(2);
// assertEquals is used to check if
// two objects is equally defined or not
assertEquals(2, mockList.size());
}
By now, we have successfully tested the list.size() method, so moving onto next
测试具有多个值的 list.size() 方法
我们也可以用多个值测试 list.size() 方法。
例子
Java
@Test
public void mockListSizeMethod_multipleValues(){
List mockList = mock(List.class);
when(mockList.size()).thenReturn(2).thenReturn(3);
assertEquals(2, mockList.size());
assertEquals(3, mockList.size());
}
测试 list.get() 方法
插图 A:
Java
@Test
public void mockListGetMethod(){
List mockList = mock(List.class);
// Argument Matcher
when(mockList.get(anyInt())).thenReturn(2);
assertEquals(2, mockList.get(anyInt()));
}
也可以抛出异常并测试您的方法。
说明 B:抛出异常
Java
@Test(expected = RuntimeException.class)
public void mockListGetMethod_throwAnException(){
List mockList = mock(List.class);
when(mockList.get(anyInt())).thenThrow(new RuntimeException("Something Wrong"));
mockList.get(anyInt());
}
执行:
Java
// Java Program to Illustrate JavaListMock File
// Importing required classes
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.List;
import org.junit.Test;
// Class
public class JavaListMock {
// @Test annotation tells the JUnit that
// the public void method
// in which it is used can run as a test case
@Test public void mockListSizeMethod()
{
// Mock the List Class
List mockList = mock(List.class);
// when is used when we want to mock to return
// specific values when particular methods are
// called and thenReturn() methods lets you define
// the return value when a particular method of the
// mocked object is been called
when(mockList.size()).thenReturn(2);
// assertEquals is used to check if
// two objects is equally defined or not
assertEquals(2, mockList.size());
}
// Method
@Test public void mockListSizeMethod_multipleValues()
{
List mockList = mock(List.class);
when(mockList.size()).thenReturn(2).thenReturn(3);
assertEquals(2, mockList.size());
assertEquals(3, mockList.size());
}
// Method
@Test public void mockListGetMethod()
{
List mockList = mock(List.class);
// Argument Matcher
when(mockList.get(anyInt())).thenReturn(2);
assertEquals(2, mockList.get(anyInt()));
}
// Method
@Test(expected = RuntimeException.class)
public void mockListGetMethod_throwAnException()
{
List mockList = mock(List.class);
when(mockList.get(anyInt()))
.thenThrow(
new RuntimeException("Something Wrong"));
mockList.get(anyInt());
}
}
第 5 步:运行测试用例
现在我们将运行我们的测试用例。要运行您的测试用例,您可以单击测试方法按钮或一键测试所有方法。如果您想分别测试每种方法,则可以单击测试单个方法按钮。在 Run 选项卡中,您可以看到所有测试用例都已通过。