英文:
Testing a method calling a method in the same class which calls a method of service class
问题
我对 JUnit 还相对不太了解,我正在尝试在 JUnit 中测试 isApproved 方法。我正在使用 Mockito 的 when 和 thenReturn 来模拟 isDateValidOfData 方法,代码如下:
Mockito.when(isDateValidOfData(anyLong(), anyString())).thenReturn(true);
这会导致索引越界异常。在这里,serviceClass 在参数匹配器上被调用,因此在数据列表中没有返回任何内容。我想知道是否有一种方法可以使用 Mockito 和 JUnit 模拟数据并进行测试。
我使用了 spy 来获取同一类的对象以调用方法。以下是代码示例:
MyClass {
// Service class 调用 repository 来获取数据。
@Autowired
ServiceClass serviceClass;
public boolean isApproved(Long id, String code) {
// 对参数进行验证
// 如果 id 是特定类型,则默认返回 true
// 如果 code 已经在现有列表中,则继续执行下面的代码,否则默认返回 true。
return isDateValidOfData(Long id, String code);
}
public boolean isDateValidOfData(Long id, String code) {
List<Data> data = serviceObject.getData(id, code);
LocalDateTime eDate = data.get(0).getEDate();
LocalDateTime rDate = data.get(0).getRDate();
// 检查 eDate 和 rDate 是否大于当前日期,如果是则返回 true,否则返回 false
}
}
@RunWith(SpringRunner.class)
TestClass {
@InjectMocks
MyClass myClass;
@Test
public void isApprovedTest() {
MyClass myClass1 = Mockito.spy(myClass);
Mockito.when(myClass1.isDateValidOfData(anyLong(), anyString())).thenReturn(true);
Assert.assertTrue(myClass1.isApproved(1234L, "1234"));
}
}
英文:
I'm pretty new to Junit and I'm trying to test isApproved method in Junit. I'm am mocking isDateValidOfData method using when and thenReturn using
Mockito.when(isDateValidOfData(anyLong(), anyString()).thenReturn(true);
This is getting an indexOutOfBounds exception. Here the serviceClass is being called on argument matchers so returning nothing in the list of data. I just want to know is there a way to mock the data and test it using Mockito and Junit.
Using spy to get the object of the same class to call the method.
MyClass {
//Service class calls the repository to fetch data.
@Autowired
ServiceClass serviceClass;
public boolean isApproved(Long id, String code) {
// Validation is done on the arguments
//if id is of a particular type then return true by default
//if code is in a list already present then continue with the below code or else return true by default.
return isDateValidOfData(Long id, String code);
}
public boolean isDateValidOfData(Long id, String code) {
List<Data> data = serviceObject.getData(id, code);
LocalDateTime eDate = data.get(0).getEDate();
LocalDateTime rDate = data.get(0).getRDate();
// check if eDate and rDate is greater than current date, if yes return true or return false
}
}
@RunWith(SpringRunner.class)
TestClass {
@InjectMocks
MyClass myClass;
@Test
public void isApprovedTest() {
MyClass myClass1 = Mockito.spy(myClass);
Mockito.when(myClass1.isDateValidOfData(anyLong(), anyString())).thenReturn(true);
Assert.assertTrue(myClass1.isApproved(1234L, "1234");
}
}
答案1
得分: 2
使用@InjectMocks注解时,您应该在类级别上使用@RunWith(MockitoJUnitRunner.class),一切都会正常工作。
如果您想使用@RunWith(SpringRunner.class),那么请使用@MockBean和@SpyBean来编写测试。
编辑:
您可以在此处查看更多关于@RunWith(SpringRunner.class)与@RunWith(MockitoJUnitRunner.class)的区别。
另外,您可以查看这个链接:https://stackoverflow.com/questions/44794403/why-we-use-mockitojunitrunner-class-in-our-junit-test
以及关于@RunWith(SpringRunner.class)与Spring Boot的内容。
另外,请查看这个链接:https://stackoverflow.com/questions/20353846/mockito-difference-between-doreturn-and-when
英文:
Since you are using @InjectMocks you should use @RunWith(MockitoJUnitRunner.class) on class level an everything will work fine.
If you want to use @RunWith(SpringRunner.class) then use @MockBean and @SpyBean to write test.
EDIT:
You can see more @RunWith(SpringRunner.class) vs @RunWith(MockitoJUnitRunner.class) here
Additional, you can check this https://stackoverflow.com/questions/44794403/why-we-use-mockitojunitrunner-class-in-our-junit-test
and @RunWith(SpringRunner.class) with Spring Boot
Also, please check this one https://stackoverflow.com/questions/20353846/mockito-difference-between-doreturn-and-when
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论