Android 使用junit和mockito单元测综合习题
时间: 2024-04-06 20:09:45 浏览: 96
Practical-Unit-Testing-with-JUnit-and-Mockito_2013-04_Tomek.Kaczanowsk
以下是一个使用junit和mockito进行Android单元测试的综合习题:
假设有一个名为Calculator的类,其功能是进行加、减、乘、除四则运算。现在需要进行单元测试来确保Calculator类的各个方法的正确性。
1. 编写测试用例,测试Calculator类的add方法,确保其能够正确地进行加法运算。测试用例应包括正数、负数、零等多种情况。
```java
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
public class CalculatorTest {
@Test
public void testAdd() {
Calculator calculator = new Calculator();
// Test positive numbers
assertEquals(4, calculator.add(2, 2));
assertEquals(10, calculator.add(5, 5));
// Test negative numbers
assertEquals(-2, calculator.add(-1, -1));
assertEquals(-10, calculator.add(-5, -5));
// Test zero
assertEquals(5, calculator.add(5, 0));
assertEquals(-7, calculator.add(-7, 0));
}
}
```
2. 编写测试用例,测试Calculator类的subtract方法,确保其能够正确地进行减法运算。测试用例应包括正数、负数、零等多种情况。
```java
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
public class CalculatorTest {
@Test
public void testSubtract() {
Calculator calculator = new Calculator();
// Test positive numbers
assertEquals(0, calculator.subtract(2, 2));
assertEquals(5, calculator.subtract(10, 5));
// Test negative numbers
assertEquals(0, calculator.subtract(-1, -1));
assertEquals(-3, calculator.subtract(-5, -2));
// Test zero
assertEquals(5, calculator.subtract(5, 0));
assertEquals(-7, calculator.subtract(-7, 0));
}
}
```
3. 编写测试用例,测试Calculator类的multiply方法,确保其能够正确地进行乘法运算。测试用例应包括正数、负数、零等多种情况。
```java
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
public class CalculatorTest {
@Test
public void testMultiply() {
Calculator calculator = new Calculator();
// Test positive numbers
assertEquals(4, calculator.multiply(2, 2));
assertEquals(25, calculator.multiply(5, 5));
// Test negative numbers
assertEquals(1, calculator.multiply(-1, -1));
assertEquals(10, calculator.multiply(-5, -2));
// Test zero
assertEquals(0, calculator.multiply(5, 0));
assertEquals(0, calculator.multiply(-7, 0));
}
}
```
4. 编写测试用例,测试Calculator类的divide方法,确保其能够正确地进行除法运算。测试用例应包括正数、负数、零等多种情况,并对除以零的情况进行特殊处理。
```java
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
public class CalculatorTest {
@Test
public void testDivide() {
Calculator calculator = new Calculator();
// Test positive numbers
assertEquals(2, calculator.divide(4, 2));
assertEquals(2.5, calculator.divide(5, 2), 0.001);
// Test negative numbers
assertEquals(1, calculator.divide(-1, -1));
assertEquals(-2.5, calculator.divide(-5, 2), 0.001);
// Test zero
assertEquals(0, calculator.divide(0, 5), 0.001);
assertEquals(0, calculator.divide(0, -7), 0.001);
// Test divide by zero
try {
calculator.divide(5, 0);
fail("Expected ArithmeticException");
} catch (ArithmeticException e) {
// Expected exception
}
}
}
```
5. 使用mockito框架,编写测试用例,测试Calculator类的add方法中是否正确调用了其依赖的Adder类的add方法。
```java
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
public class CalculatorTest {
@Test
public void testAddWithAdder() {
Adder adder = mock(Adder.class);
when(adder.add(2, 2)).thenReturn(4);
when(adder.add(5, 5)).thenReturn(10);
Calculator calculator = new Calculator(adder);
// Test positive numbers
assertEquals(4, calculator.add(2, 2));
assertEquals(10, calculator.add(5, 5));
verify(adder).add(2, 2);
verify(adder).add(5, 5);
verifyNoMoreInteractions(adder);
}
}
```
以上是一个使用junit和mockito进行Android单元测试的综合习题,希望对你有所帮助。
阅读全文