我請求你的幫助,
對于我的專案https://github.com/hdsdi3g/prodlib模塊 jobkit,我無法通過 4 個假裝通過 @Aspect 驗證呼叫的測驗。
可以使用 Eclipse(最新版本)。用Maven Ko,最后一個版本也一樣。
如何重現:
git clone https://github.com/hdsdi3g/prodlib
git checkout issue51
mvn install -DskipTests && mvn test -rf :jobkit
# or just mvn test
在課堂上:https ://github.com/hdsdi3g/prodlib/blob/issue51/jobkit/springboot-service/src/test/java/tv/hd3g/jobkit/mod/component/SupervisableAspectTest.java#L70
void testWithDefaultName() throws Exception {
when(supervisableServiceSupplier.createAndStart("TestWithSupervisable.aSupervisableMethod"))
.thenReturn(supervisable);
testWithSupervisable.aSupervisableMethod(runnableWithException);
verify(runnableWithException, times(1)).run(); //OK
verify(supervisableServiceSupplier, times(1)).createAndStart("TestWithSupervisable.aSupervisableMethod"); // KO
verify(supervisableServiceSupplier, times(1)).end(supervisable, Optional.empty());
}
方面類:https ://github.com/hdsdi3g/prodlib/blob/issue51/jobkit/springboot-service/src/main/java/tv/hd3g/jobkit/mod/component/SupervisableAspect.java
@Around("annotationWithSupervisable()")
public Object manageSupervisable(final ProceedingJoinPoint joinPoint) throws Throwable {
/* [...] */
// THIS code run with Eclipse Test, but not with Maven... WHY ?
[...]
final var supervisable = supervisableServiceSupplier.createAndStart(jobName);
final var result = joinPoint.proceed(joinPoint.getArgs());
supervisableServiceSupplier.end(supervisable, Optional.empty());
return result;
[...]
}
@Pointcut("@annotation(tv.hd3g.jobkit.WithSupervisable)")
public void annotationWithSupervisable() {
}
將“方面”類作為測驗:https ://github.com/hdsdi3g/prodlib/blob/issue51/jobkit/springboot-service/src/test/java/tv/hd3g/jobkit/mod/component/TestWithSupervisable.java
@Component
public class TestWithSupervisable {
@WithSupervisable
void aSupervisableMethod(final RunnableWithException toTest) throws Exception {
toTest.run();
}
[...]
可以看到報錯https://github.com/hdsdi3g/prodlib/pull/52
[ERROR] Failures:
[ERROR] SupervisableAspectTest.testWithDefaultName:76
Wanted but not invoked:
getSupervisableSupplier bean.createAndStart(
"TestWithSupervisable.aSupervisableMethod"
);
-> at tv.hd3g.jobkit.engine.SupervisableServiceSupplier.createAndStart(SupervisableServiceSupplier.java:31)
Actually, there were zero interactions with this mock.
查看主要 pom:https ://github.com/hdsdi3g/prodlib/blob/issue51/pom.xml和專案 pom https://github.com/hdsdi3g/prodlib/blob/issue51/jobkit/springboot-service/pom .xml
查看 THIRD-PARTY.txt 檔案以了解 deps 版本(似乎都是最新版本):https ://github.com/hdsdi3g/prodlib/blob/issue51/jobkit/springboot-service/THIRD-PARTY.txt
感謝您的幫助或想法!
在 Eclipse 中一切正常,但在 Maven 測驗中卻不行(surefire)。
我不想知道我的代碼/方法是否錯誤,或者我的 maven setup/pom 是否錯誤!
uj5u.com熱心網友回復:
如果您在同一會話SupervisableServiceSupplierTest中按以下順序一起運行測驗,您的測驗在 IDE 中也會失敗:
SupervisableServiceSupplierTestSupervisableAspectTest
像這樣呼叫sss.end第一個測驗可以解決問題:
@AfterEach
void end() {
verifyNoMoreInteractions(supervisableManager, supervisable, exception);
sss.end(supervisable, Optional.empty());
}
這是一個典型的測驗案例,沒有清理夾具,而是將背景關系滲入另一個測驗。它與 Maven 完全無關。
PS:您的MCVE鏈接是我解決此案的關鍵。你提供它是對自己最大的幫助。這里的每個新手和更多有經驗的開發人員都可以從中學習。如果沒有一個完整的、可重現的例子,沒有人會有絲毫的機會來除錯它并用問題中給出的資訊提供正確的答案,因為像這樣經常
- 試圖提供幫助的人需要自己看看并稍微嘗試一下代碼,
- 問題出在問題中未顯示的代碼中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/534383.html
