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

时间:2018-10-19 18:31:13

标签: java spring-boot testng spring-test

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

1 个答案:

答案 0 :(得分:1)

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

在将test软件包中的更改也应用后,它将再次运行。

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

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

尝试从头开始在开发环境中设置应用程序。如果发现错误,则将对其进行正确分析并予以纠正。