6

I am setting up an Spring boot application on Jenkins. For the unit tests i am getting below error. This error is not particular to one test cases. Every time I run it is giving me error for different test. I am not sure what is wrong. Same project is working fine (build and unit tests) on local and other environments like (development, stage). Any idea with below errors?

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]

Here is the test class

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


}

Base class

@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());
        }
    }
}
Kiran
  • 839
  • 3
  • 15
  • 45
  • Well, this isn't really an error but rather debug info. Do the tests really fail on Jenkins or do you just wonder what the log output means? If they fail, please show some code of one of your test classes (especially the class declaration with all used annotations). – Quagaar Oct 19 '18 at 20:07
  • sorry actually it is not failed..it is waiting there forever after that debug log. So i was suspecting something to do with `activeprofile`. – Kiran Oct 19 '18 at 20:25
  • @Quagaar i have trimmed bit of unit test and added in the question. – Kiran Oct 19 '18 at 20:29
  • Which annotations are set on the base class(es)? You could try setting `@DirtiesContext` to ensure that it's not a problem with the application context when tests are run in a certain order. – Quagaar Oct 19 '18 at 21:40
  • @Quagaar i have added `@DirtiesContext` to the baseclass but no luck. I posted baseclass in the question section. please check – Kiran Oct 19 '18 at 23:09
  • And there's yet another base class... your test setup seems overcomplicated to me. I was able to reproduce the debug log message - it's just because you don't have an `@ActiveProfiles` annotation on your test class or its base classes. No error, just an info, so you can exclude this as the source of your problems. Are you using `@SpringBootTest` for your tests? And which test runner (`@RunWith`) are you using. If feasible, I would suggest that you try to simplify your tests and, if you didn't already, read the testing chapter in the Spring Boot documentation. – Quagaar Oct 20 '18 at 08:17

1 Answers1

5

I have bumped into this error after moving classes into new packages somewhere under the java folder, but omitting to move the corresponding test classes in the test folders.

After applying the changes in the test packages as well, it runs again.

You wrote that you experience the problem only in the Jenkins environment.

My guess is that Jenkins starts always with a new checkout of the project from a 100% clean status. In the other environments you might have some residues from the previous development, and these somehow allow the tests to 'work', but I would expect that it is Jenkins getting it right...

Try to setup the app in a development environment from scratch. If you get the error, so you will properly analyze it and correct it.

David L.
  • 3,149
  • 2
  • 26
  • 28
  • thanks, nice catch, I had the same problem with simple tests run by maven, no Jenkins involved, even the simple loading of the spring context was failing. – user1708042 Apr 16 '21 at 14:15