Java 用Mockito和Spring Boot进行深度模拟
我需要(用Mockito)模拟“B”类使用的“A”类,但是我的测试用例是针对“D”类的 参见我的示例:Java 用Mockito和Spring Boot进行深度模拟,java,spring-boot,mockito,Java,Spring Boot,Mockito,我需要(用Mockito)模拟“B”类使用的“A”类,但是我的测试用例是针对“D”类的 参见我的示例: @Service public class Aclass { public Long sumTwoNumbers(Long a, Long b){ return a + b; } } @Service public class Bclass { @Autowired private Aclass classA; publ
@Service
public class Aclass {
public Long sumTwoNumbers(Long a, Long b){
return a + b;
}
}
@Service
public class Bclass {
@Autowired
private Aclass classA;
public Long sunThreeNumbers(Long a, Long b, Long c){
return classA.sumTwoNumbers(a, b) + c;
}
}
@Service
public class Cclass {
@Autowired
private Bclass classB;
public Long sunFourNumbers(Long a, Long b, Long c, Long d){
return classB.sunThreeNumbers(a, b, c) + d;
}
}
@Service
public class Dclass {
@Autowired
private Cclass classC;
public Long sunFourNumbers(Long a, Long b, Long c, Long d){
return classC.sunFourNumbers(a, b, c, d);
}
}
我的测试是:
@ExtendWith(MockitoExtension.class)
public class DclassTest{
@InjectMocks
private Dclass classD;
@Mock
private Aclass classA;
/* Look , I need to mock Aclass , not Bclass or Cclass. */
@Test
public void testSunFourNumbers(){
Mockito.when(classA.sumTwoNumbers(any(Long.class), any(Long.class))).thenReturn(0L);
Long sum = classC.sunFourNumbers(1L, 1L, 1L, 1L);
Assert.assertEquals(2L, sum);
}
}
这是我所需要的一个简单的例子
当我运行这个时,我在Dclass中收到了NullPointerException
有可能吗?我认为您应该用Mockito测试您的类B类,如下所示:
public class BclassTest {
Long a = 0L;
Long b = 0L;
Long c = 1L;
@InjectMocks
private Bclass bclass;
@Mock
private Aclass aclass;
//You Should initialize your Mock Objects in @Before method.
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
@Test
public void testSunThreeNumbers(){
Mockito.when(aclass.sumTwoNumbers(anyLong(),anyLong())).thenReturn(0L);
assertEquals(Long.valueOf(1), bclass.sunThreeNumbers(Long a, Long b, Long c));
}
}
}
我认为你应该用Mockito测试你的类B类,如下所示:
public class BclassTest {
Long a = 0L;
Long b = 0L;
Long c = 1L;
@InjectMocks
private Bclass bclass;
@Mock
private Aclass aclass;
//You Should initialize your Mock Objects in @Before method.
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
@Test
public void testSunThreeNumbers(){
Mockito.when(aclass.sumTwoNumbers(anyLong(),anyLong())).thenReturn(0L);
assertEquals(Long.valueOf(1), bclass.sunThreeNumbers(Long a, Long b, Long c));
}
}
}
根据示例代码,很明显,下面的服务依赖如下
@InjectMocks Dclass classD; // It has all 3 mocks now (but A and B mocks not needed)
@Mock Aclass classA; //not needed (Dclass not dependent on Aclass)
@Mock Bclass classB; //not needed (Dclass not dependent on Bclass)
@Mock Cclass classC; // **Mandatory** - Because Dclass is directly dependent on Cclass
As per your code - you have a mock for Aclass and injected mock into Dclass.
When(...).then(...) written for Aclass mock.
@Test
public void testSunFourNumbers(){
Mockito.when(classA.sumTwoNumbers(any(Long.class), any(Long.class))).thenReturn(0L);
//Since you don't have mock for 'classC' in your code, it will go as null and result in nullpointer
Long sum = classC.sunFourNumbers(1L, 1L, 1L, 1L);
Assert.assertEquals(2L, sum);
}
根据示例代码,很明显,下面的服务依赖如下
@InjectMocks Dclass classD; // It has all 3 mocks now (but A and B mocks not needed)
@Mock Aclass classA; //not needed (Dclass not dependent on Aclass)
@Mock Bclass classB; //not needed (Dclass not dependent on Bclass)
@Mock Cclass classC; // **Mandatory** - Because Dclass is directly dependent on Cclass
As per your code - you have a mock for Aclass and injected mock into Dclass.
When(...).then(...) written for Aclass mock.
@Test
public void testSunFourNumbers(){
Mockito.when(classA.sumTwoNumbers(any(Long.class), any(Long.class))).thenReturn(0L);
//Since you don't have mock for 'classC' in your code, it will go as null and result in nullpointer
Long sum = classC.sunFourNumbers(1L, 1L, 1L, 1L);
Assert.assertEquals(2L, sum);
}
不清楚当
Dclass
取决于Cclass
时,为什么要模拟Aclass
,是否愿意解释您的推理?不清楚当Dclass
取决于Cclass
时,为什么要模拟Aclass
,是否愿意解释您的推理?