无法对调用API的类进行单元测试

时间:2019-11-21 08:44:55

标签: unit-testing junit mockito

我有以下两个课程-

服务等级

和组件

@Service
public class ProfileInfoCacheService {

    public ProfileInfoResponse getProfileInfo(String uuid) {

            ProfileInfoResponse response = getUserInfoFromIdentity(uuid);
            return response;

    }

public ProfileInfoResponse getUserInfoFromIdentity(String identity) {
        LinkedHashMap<String, List<UserInfo>> profileInfo = userService.getUserInfoFromIdentity(identityList);

        return profcache.getProfileInfo();

    }

现在,这是我的测试用例文件。请耐心等待,因为我还很陌生。

@Component
public class UserInfoServiceImpl implements UserInfoService {

@SuppressWarnings("unchecked")
    @Override
    public LinkedHashMap<String, List<UserInfo>> getUserInfoFromIdentity(List<String> identityList) {
        //Calls an API and returns data in LinkedHashMap.
    }

getProfileInfo()调用的响应应采用这种格式-

package com.citruspay.prepaid;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

//class file imports

import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class ProfileInfoCacheServiceTest {

    private PrepaidAccount account;
    private ProfileInfoCacheService pics;

    @Before
    public void setUp() throws Exception {
        account = new PrepaidAccount("8588844251");
        //pics = new ProfileInfoCacheService();
        ProfileInfoCacheService pics = mock(ProfileInfoCacheService.class);
    }

    @Test
    public void testConstructor() {
        assertEquals("8588888888", account.owner());
    }

    @Test
    public void testNumber() throws Exception {
        ProfileInfoResponse response = pics.getProfileInfo("7266704751953077361"); //pics is null here

        assertEquals("", response.getEmail() );
        assertEquals("8588888888", response.getMobile() );
    }
}

每个移动/电子邮件对都有一个数字(例如7266704751953077361),使用该数字在调用API时会返回相同的数字。

但是,由于在我调用pics.getProfileInfo(“ 7266704751953077361”)的图片为null时,出现了空指针异常。

请在这里帮助我纠正错误。

更新

将mock()更改为新的类调用后,我得到了ProfileInfoCacheService对象,但随后将调试移至setup()方法的最后一行,得到了以下内容-

{
        "email": "some.email@something.in",
        "mobile": "8888888888",
    }

更新2

添加ProfileInfoCacheService类的更多详细信息-

java.lang.ExceptionInInitializerError
    at org.slf4j.impl.StaticLoggerBinder.<init>(StaticLoggerBinder.java:72)
    at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:45)
    at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
    at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383)
    at com.citruspay.prepaid.common.user.beans.ProfileInfoCacheService.<init>(ProfileInfoCacheService.java:42)
    at com.citruspay.prepaid.ProfileInfoCacheServiceTest.setUp(ProfileInfoCacheServiceTest.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.IllegalStateException: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError. See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.
    at org.slf4j.impl.Log4jLoggerFactory.<clinit>(Log4jLoggerFactory.java:54)
    ... 33 more

0 个答案:

没有答案