我是Junit的新手,最近遇到了这个问题。无论我在代码中使用了完整的未来,我都无法编写测试用例。像下面的Java文件
更新
AUDITSERVICE.JAVA
@Autowired
Executor existingThreadPool;
@Override
public void auditData(List<ErrorDetails> alertList) {
CompletableFuture.runAsync(() -> {
if (alertList.isEmpty())
//privateMethodCall1
else
//privateMethodCall2
}, existingThreadPool);
}
我遵循此链接,并尝试了以下解决方案仍然可以在以下错误之类的完整future中获得NPE。
auciitserviceTest.java
@InjectMock
AuditService auditService;
@Mock
private CompletableFuture<Void> completableFuture = null;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
completableFuture = CompletableFuture.runAsync(new Runnable() {
@Override
public void run() {}
},Executors.newSingleThreadExecutor());
}
@Test
public void shouldAuditData() {
List<ErrorDetails> alertList = new ArrayList();
auditService.auditData(alertList);
}
错误
java.lang.NullPointerException
at java.util.concurrent.CompletableFuture.screenExecutor(CompletableFuture.java:415)
at java.util.concurrent.CompletableFuture.runAsync(CompletableFuture.java:1858)
at com.service.impl.AuditService.auditData(AuditService.java:15)
at com.service.impl.AuditServiceTest.shouldAuditData(AuditServiceTest.java:249)
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.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.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
您需要测试逻辑,而无需模拟静态方法CompletableFuture.runAsync(...)
。因此,您的测试应该看起来像普通测试,并且您需要等待一些时间以确保执行异步代码,因为它未在同一线程中执行。因此,就目前而言,我会给您示例,您可以与Thread.sleep()
一起使用,这不是一个好的惯例,其他问题您可以询问如何避免使用Thread.sleep()
。
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
@RunWith(MockitoJUnitRunner.class)
public class AuditServiceTest {
@Mock
Service serviceMock;
@Test
public void shouldAuditData() {
AuditService auditService = new AuditService(serviceMock);
List<Object> alertList = new ArrayList();
auditService.auditData(alertList);
// you can wait with Thread.sleep()
// because the execution is asynchronous
Mockito.verify(serviceMock).method1();
Mockito.verify(serviceMock, Mockito.never()).method2();
}
}
class AuditService {
Executor existingThreadPool = Executors.newSingleThreadExecutor();
Service service;
public AuditService(Service service) {
this.service = service;
}
public void auditData(List<Object> alertList) {
CompletableFuture.runAsync(() -> {
if (alertList.isEmpty()) {
service.method1();
} else {
service.method2();
}
}, existingThreadPool);
}
}
class Service {
public void method1(){};
public void method2(){};
}
在审核服务类中,执行程序是自动化的。这是单位测试的完美设置。您要做的是,提出单独的配置进行测试,而执行程序实现应该是一个内联执行程序(您可以提供自己的实现,以在同一调用线程中呼叫runnable.run(。为此,您可以使用一些提供的弹簧测试的实现。例如:AbstractJunit4SpringContextTests
如果您不喜欢获得春季测试支持,则现在您将模拟执行者注入了审计服务。因此,您可以通过提供自定义stub.swer并执行Runnable.run。
来模拟执行方法。Mockito.doAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
((Runnable)args[0]).run();
return null; // void method, so return null
}
}).when(executor).execute(Mockito.any(Runnable.class));