【问题标题】:Could not find an 'annotation declaring class' for unit tests找不到单元测试的“注释声明类”
【发布时间】:2019-03-24 16:27:30
【问题描述】:

我正在 Jenkins 上设置一个 Spring Boot 应用程序。对于单元测试,我遇到了错误。此错误并非特定于一个测试用例。每次我运行它都会给我不同的测试错误。我不确定出了什么问题。同一个项目在本地和其他环境(如(开发、阶段)上运行良好(构建和单元测试)。有以下错误的任何想法?

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());
        }
    }
}

【问题讨论】:

  • 嗯,这并不是真正的错误,而是调试信息。 Jenkins 上的测试是否真的失败了,或者你只是想知道日志输出的含义是什么?如果它们失败了,请显示您的一个测试类的一些代码(尤其是带有所有使用注释的类声明)。
  • 对不起,实际上它并没有失败..它在调试日志之后永远在那里等待。所以我怀疑与activeprofile有关。
  • @Quagaar 我已经修剪了一些单元测试并添加到问题中。
  • 基类上设置了哪些注释?您可以尝试设置@DirtiesContext 以确保在以特定顺序运行测试时应用程序上下文不会出现问题。
  • @Quagaar 我已将@DirtiesContext 添加到基类中,但没有运气。我在问题部分发布了基类。请检查

标签: java spring-boot testng spring-test


【解决方案1】:

在将类移动到 java 文件夹下的某个新包中后,我遇到了这个错误,但没有在 test 文件夹中移动相应的测试类。

在应用 test 包中的更改后,它会再次运行。

您写道,您仅在 Jenkins 环境中遇到问题。

我的猜测是,Jenkins 总是从 100% 干净状态的新项目签出开始。在其他环境中,您可能会从以前的开发中留下一些残留物,这些残留物以某种方式允许测试“工作”,但我希望是 Jenkins 做对了......

尝试在开发环境中从头开始设置应用。如果你得到错误,那么你将正确地分析它并纠正它。

【讨论】:

  • 谢谢,很好,我在 maven 运行的简单测试中遇到了同样的问题,没有涉及 Jenkins,甚至简单的 spring 上下文加载也失败了。
猜你喜欢
  • 2013-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-22
  • 2019-04-29
  • 2013-03-22
  • 2015-07-04
  • 2022-08-17
相关资源
最近更新 更多