Java Mockito没有模拟出成员变量方法返回值

Java Mockito没有模拟出成员变量方法返回值,java,spring-boot,junit,mockito,Java,Spring Boot,Junit,Mockito,我有一个包含成员变量的类,但是Mockito似乎无法模拟成员变量的方法。以下是我正在测试的系统: public class MessageConsumer { private ConsumerResponse consumerResponse; private NotificationConsumer notificationConsumer; @Scheduled(cron = "${com.example.value}") public void fetc

我有一个包含成员变量的类,但是Mockito似乎无法模拟成员变量的方法。以下是我正在测试的系统:

public class MessageConsumer {

    private ConsumerResponse consumerResponse;
    private NotificationConsumer notificationConsumer;

    @Scheduled(cron = "${com.example.value}")
    public void fetch() {
        consumerResponse = notificationConsumer.fetchWithReturnConsumerResponse(); //no exception thrown on this line at all -- but could this be the cause of the problem in the test?
        System.out.println("consumerResponse's responseCode: " + consumerResponse.getResponseCode()); // NullPointerException thrown here
    }

    public ConsumerResponse setConsumerResponse(ConsumerResponse consumerResponse) {
        this.consumerResponse = consumerResponse;
    }

    public ConsumerResponse getConsumerResponse() {
        return consumerResponse;
    }
}
下面是该类的相关JUnit测试:

@SpringBootTest
@RunWith(MockitoJUnitRunner.class)
public class MessageConsumerTest {

    @Mock
    private ConsumerResponse consumerResponse;

    @InjectMocks
    private MessageConsumer messageConsumer;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }

    //Failing unit test
    @Test
    public void getResponseCodeShouldReturn200() {
        Mockito.when(consumerResponse.getResponseCode()).thenReturn("200");
        messageConsumer.fetch()
    }

}
如您所见,我模拟了
ConsumerResponse ConsumerResponse
变量,以在调用
ConsumerResponse.getResponseCode()
方法时返回
“200”
。相反,我得到了一个
NullPointerException


我很确定我正确地模拟了成员变量并对其进行了适当的初始化(
initMocks
)。我花了好几天的时间想弄明白这一点。我哪里出错了?

由于
NotificationConsumer
也是此类的外部依赖项,您还必须模拟此类,否则
consumerResponse=NotificationConsumer.fetchWithReturnConsumerResponse()将在测试中导致
null
,因为您没有模拟
NotificationConsumer
。此外,我建议不要在这个单元中使用
@SpringBootTest
,因为这个注释将引导整个Spring上下文。以下代码段应该可以帮助您:

@RunWith(MockitoJUnitRunner.class)
public class MessageConsumerTest {

    @Mock
    private ConsumerResponse consumerResponse;

    @Mock
    private NotificationConsumer notificationConsumer;

    @InjectMocks
    private MessageConsumer messageConsumer;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void getResponseCodeShouldReturn200() {
        Mockito.when(notificationConsumer.fetchWithReturnConsumerResponse()).thenReturn(consumerResponse);
        Mockito.when(consumerResponse.getResponseCode()).thenReturn("200");
        messageConsumer.fetch();
    }

}

由于
NotificationConsumer
也是该类的外部依赖项,因此您还必须模拟该类,否则
consumerResponse=NotificationConsumer.fetchWithReturnConsumerResponse()将在测试中导致
null
,因为您没有模拟
NotificationConsumer
。此外,我建议不要在这个单元中使用
@SpringBootTest
,因为这个注释将引导整个Spring上下文。以下代码段应该可以帮助您:

@RunWith(MockitoJUnitRunner.class)
public class MessageConsumerTest {

    @Mock
    private ConsumerResponse consumerResponse;

    @Mock
    private NotificationConsumer notificationConsumer;

    @InjectMocks
    private MessageConsumer messageConsumer;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void getResponseCodeShouldReturn200() {
        Mockito.when(notificationConsumer.fetchWithReturnConsumerResponse()).thenReturn(consumerResponse);
        Mockito.when(consumerResponse.getResponseCode()).thenReturn("200");
        messageConsumer.fetch();
    }

}

你能分享ConsumerResponse类吗?@GauravRai1512不幸的是,我无法访问外部库to@JB但这不重要,因为我正在模拟的方法属于consumerResponse对象,对吗这不是莫基托的全部观点吗?(隔离依赖项并让它们做您希望它们做的事?)您在代码中使用了notificationConsumer,但它没有被模仿。所以它是空的。SpringBootTest注释也是无用的,因为您没有使用SpringRunner运行测试。所以它没有任何效果。确切地说:您希望将被测试的类(MessageConsumer)与其依赖项(NotificationConsumer)隔离开来。所以你需要模拟NotificationConsumer。你能共享ConsumerResponse类吗?@GauravRai1512很遗憾,我无法访问外部库to@JB但这不重要,因为我正在模拟的方法属于consumerResponse对象,对吗这不是莫基托的全部观点吗?(隔离依赖项并让它们做您希望它们做的事?)您在代码中使用了notificationConsumer,但它没有被模仿。所以它是空的。SpringBootTest注释也是无用的,因为您没有使用SpringRunner运行测试。所以它没有任何效果。确切地说:您希望将被测试的类(MessageConsumer)与其依赖项(NotificationConsumer)隔离开来。所以你需要模仿NotificationConsumer。