我是Junit的新手,最近遇到了这个问题。无论我在代码中使用CompletableFuture的任何地方,我都无法编写测试用例。像下面的Java文件
已更新
AuditService.java
@Autowired
Executor existingThreadPool;
@Override
public void auditData(List<ErrorDetails> alertList) {
CompletableFuture.runAsync(() -> {
if (alertList.isEmpty())
//privateMethodCall1
else
//privateMethodCall2
}, existingThreadPool);
}
我遵循了这个link并尝试了以下解决方案,仍然为CompletableFuture获得了NPE,如以下错误所示。
AuditServiceTest.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)
答案 0 :(得分:2)
您需要测试逻辑,而无需模拟静态方法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(){};
}
答案 1 :(得分:0)
在AuditService类中,执行器是自动装配的。这是单元测试的理想设置。您要做的是,为测试提供单独的配置,并且Executor实现应该是内联执行器(您可以提供自己的实现,该实现在同一调用线程中调用runnable.run)。 为此,您可以使用spring-test提供的一些实现。 例如:AbstractJUnit4SpringContextTests
如果您不希望获得弹簧测试支持,那么现在您已经将模拟执行器注入了AuditService。因此,您可以通过提供自定义存根来模拟execute方法。回答并执行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));