在同一个类中测试调用另一个方法,该方法调用了服务类的方法。

huangapple go评论76阅读模式
英文:

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&lt;Data&gt; 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, &quot;1234&quot;);
        }       
}

答案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

huangapple
  • 本文由 发表于 2020年4月5日 19:29:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/61041846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定