java - InjectMocks 对象在单元测试中为 null

标签 java junit mockito

这是我第一次使用 Mockito 进行 junit 测试。我面临 @InjectMocks 中使用的服务的 NPE 问题。我查看了其他解决方案,但即使在遵循它们之后,它也显示相同。这是我的代码。

@RunWith(MockitoJUnitRunner.class)

public class CustomerStatementServiceTests {

    @InjectMocks
    private BBServiceImpl bbService;

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

    }

/**
 *  This test is to verify SUCCESS response
 */

@Test
public void testSuccess() { 

    BBResponse response = bbService.processDetails(txs);
    assertEquals("SUCCESSFUL" ,response.getResult());
}
}

BBServiceImpl

@Service
public class BBServiceImpl implements BBService {

final static Logger log = Logger.getLogger(BBServiceImpl.class);



public BBResponse process(List<Customer> customers) { 
  // My business logic goes here
}
}

Pom.xml

        <!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>2.23.4</version>
        <scope>test</scope>
    </dependency>




    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

我的“bbService”对象在这里为空

我在这里遗漏了什么吗?

最佳答案

经过提供附加信息的讨论后,答案可以概括为 junit4junit5 之间的 Maven 配置问题。

java.lang.NullPointerException
at com.cts.rabo.CustomerStatementServiceTests.testSuccess(CustomerStatementServiceTests.java:83)
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:498)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:675)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60) 
...

堆栈跟踪显示了 junit5 引擎的清晰用法。

pom 还包含以下依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.2.5.RELEASE</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Before Spring Boot 2.2.0.RELEASE, spring-boot-starter-test included junit4 dependency transitively. Spring Boot 2.2.0 onwards, Junit Jupiter is included instead.

根据这个answer排除似乎阻止了执行。

删除排除解决了我的问题。

<小时/>

如果没有明显的要求使用 junit4,我建议切换到 junit5

检查这个answer有关如何将 mockitojunit5 一起使用的更多信息。

关于java - InjectMocks 对象在单元测试中为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60478285/

相关文章:

java - 在 Netbeans 调色板中分解 Java Swing 类

java - 在 Gradle 日志末尾列出所有构建错误

java - 从 0.7 升级到 0.8.1.1 后生成嵌入式 kafka 队列时出错

java - 如何使用 mockito 验证参数化方法

java - 如何将 BindingResult 作为请求参数传递?

java - 如何使用 Scala 的 AnyVal 调用 Java 可变参数方法?

java:服务器(glassfish/tomcat)可以自动调用jsp吗?

java - maven Surefire-junit47 未运行 JUnit4 测试

java - 如何使用 Mockito 验证 Junit 测试用例中的 logger.error(String message, Throwable t)

java - Java 中的引用传递