找不到单元测试的“注释声明类”

问题描述 投票:1回答:1

我正在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());
        }
    }
}
java spring-boot testng spring-test
1个回答
0
投票

在将类移到java文件夹下某个位置的新程序包中之后,却忽略了将相应的测试类移到test文件夹中,因此遇到了这个错误。

同样在test包中应用了更改之后,它再次运行。

您写道您仅在詹金斯环境中遇到问题。

[我的猜测是,詹金斯总是从100%干净状态开始对项目进行新的检出。在其他环境中,您可能会从以前的开发中残留一些东西,这些残留物以某种方式可以使测试“起作用”,但是我希望Jenkins能够正确完成工作...

尝试从头开始在开发环境中设置应用程序。如果收到错误,则可以正确分析并更正。

© www.soinside.com 2019 - 2024. All rights reserved.