我正在 Jenkins 上设置一个 Spring 启动应用程序。对于单元测试,我遇到了以下错误。此错误并非特定于一个测试用例。每次我运行它都会给我不同的测试错误。我不确定出了什么问题。同一个项目在本地和其他环境(如开发、阶段)上运行良好(构建和单元测试)。有以下错误的想法吗?
00:49:42.836 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.abc.services.tokens.crypto.aws.AesGcmDynamoCryptoCipherProviderTest]
00:49:42.836 [main] INFO org.springframework.test.context.support.DefaultTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@43195e57, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@333291e3, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@479d31f3, org.springframework.test.context.support.DirtiesContextTestExecutionListener@40ef3420]
这是测试类
@SuppressWarnings("unchecked")
public class AesGcmDynamoCryptoCipherProviderTest extends AbstractTestNGBeanMockingTests {
@MockBean
AwsCrypto awsCrypto;
@MockBean
DynamoDBProvider dynamoDBProvider;
@MockBean
MasterKeyProvider masterKeyProvider;
@MockBean
Table table;
private static Item mockCipherItem(UUID cipherId) {
Item item = mock(Item.class);
return item;
}
private static <T> CryptoResult<T, ?> mockCryptoResult(T result) {
// do something
return cryptoResult;
}
@BeforeMethod
private void init() {
CryptoResult<String, ?> decryptoResult = mockCryptoResult(Base64.getEncoder().encodeToString("*decrypted*".getBytes()));
CryptoResult<String, ?> encryptoResult = mockCryptoResult("*encrypted*");
}
@Test
public void testGetCipher() {
AesGcmDynamoCryptoCipherProvider provider = new AesGcmDynamoCryptoCipherProvider("table", awsCrypto, dynamoDBProvider, masterKeyProvider);
UUID cipherId = UUID.randomUUID();
Item cipherItem = mockCipherItem(cipherId);
AesGcmCipher cipher = provider.getCipher(cipherId);
assertNotNull(cipher);
assertEquals(cipher.getCipherId(), cipherId);
}
}
基类
@ContextConfiguration(classes = { //...
AbstractTestNGBeanMockingTests.MockBeanConfiguration.class //...
})
@DirtiesContext
public class AbstractTestNGBeanMockingTests extends AbstractTestNGSpringContextTests {
private static ThreadLocal<Class<? extends AbstractTestNGBeanMockingTests>> currentTestClass = new ThreadLocal<>();
@AfterClass(alwaysRun = true)
@Override
protected void springTestContextAfterTestClass() throws Exception {
super.springTestContextAfterTestClass();
}
@BeforeClass(alwaysRun = true, dependsOnMethods = { "springTestContextBeforeTestClass" })
@Override
protected void springTestContextPrepareTestInstance() throws Exception {
currentTestClass.set(this.getClass());
super.springTestContextPrepareTestInstance();
currentTestClass.set(null);
}
@BeforeMethod
public void initializeMockedBeans() {
MockBeanRegistration.initializeMockedBeans(this);
}
protected static class MockBeanConfiguration {
MockBeanConfiguration(ApplicationContext context) {
MockBeanRegistration.registerMocks((BeanDefinitionRegistry) context, currentTestClass.get());
}
}
}
最佳答案
在将类移动到 java
下某处的新包后,我遇到了这个错误。文件夹,但省略了在 test
中移动相应的测试类文件夹。
应用 test
中的更改后包,它再次运行。
您写道,您仅在 Jenkins 环境中遇到此问题。
我的猜测是 Jenkins 总是从一个 100% 干净状态的项目的新结帐开始。在其他环境中,您可能会从以前的开发中获得一些残留物,而这些以某种方式允许测试“工作”,但我希望 Jenkins 做对了……
尝试从头开始在开发环境中设置应用程序。如果你得到错误,那么你会正确地分析它并纠正它。
关于java - 找不到单元测试的 'annotation declaring class',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52898126/