【发布时间】: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