Spring -Boot -Tests funktionieren nicht auf Jenkins, sondern arbeiten seit dem Frühjahr 3.4.0 auf lokaler

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Spring -Boot -Tests funktionieren nicht auf Jenkins, sondern arbeiten seit dem Frühjahr 3.4.0 auf lokaler

by Anonymous » 16 Mar 2025, 15:45

Aktuelle Spring -Boot -Version: 3.4.2 Testng -Version: 7.10.2 Feder -Test: 6.2.2 (aus der Eltern Spring Depeedcy) < /p>
Nach dem Frühjahr 3.2.0 Wir haben gewarnt, dass @Mockbean und @spybean veraltet waren, aber wir benutzten sie weiter. Seit Frühjahr 3.4.0 sind sie für die Entfernung markiert und haben sie daher entsprechend auf @Mockitobean und @Mockityspybean migriert. Auf lokaler Bedeutung - alles funktioniert, sie laufen, aber auf den Jenkins sehen wir den Fehler, an den das Objekt übergeben wurde, an anwan () null ist. Tried many solutions, no luck, here is a scenario of usage (simplified).To keep the project's NDA I refactored code but kept the main purpose of usage:
Hierarchy: MyTest
@SuppressWarnings("unchecked")
public class SomeBaseAPITest extends BaseAPITest {

@MockitoSpyBean(reset = MockReset.NONE)
private SomeCsvServiceImpl someCsvService;

@BeforeSuite(alwaysRun = true, dependsOnMethods = "prepareTestContext")
protected void prepareMocks() {
doAnswer(invocationOnMock -> TestService.translate(invocationOnMock.getArgument(1)))
.when(morningstarCsvService)
.translate(any(), any(), any(), any(), any());

}

}
< /code>
public class MyTest extends SomeBaseAPITest {

@Test
void myTest() {
var response = given()
.get("http://blablabla.com");

assertTrue(response != null);
}

}
< /code>
The error:
18:11:09 [INFO]
18:11:09 [INFO] Results:
18:11:09 [INFO]
18:11:09 [ERROR] Failures:
18:11:09 [ERROR] SomeBaseAPITest.prepareMocks:83 » NullInsteadOfMock
18:11:09 Argument passed to when() is null!
18:11:09 Example of correct stubbing:
18:11:09 doThrow(new RuntimeException()).when(mock).someMethod();
18:11:09 Also, if you use @Mock annotation don't miss openMocks()
18:11:09 [INFO]
18:11:09 [ERROR] Tests run: 357, Failures: 1, Errors: 0, Skipped: 356
< /code>
The error means the code here:
doAnswer(invocationOnMock -> TestService.translate(invocationOnMock.getArgument(1)))
.when(morningstarCsvService)
.translate(any(), any(), any(), any(), any());
< /code>
Why do we use such hierarchy of classes? To run the context once and reuse context in mocks configuration. Why 2 methods for beforeSuite ? The one method loads the context (because the context is by default loaded in beforeClass, but in beforeSuite it is manually forced to load). The second one deligates creating mocks (in the real example there are more classes and more methods, here is an abstraction).
Again: it is working on local, and isn't on jenknins. Jenkins can see some error, and maybe he is correct. When hovering the mouse on the
@MockitoSpyBean(reset = MockReset.NONE)
private SomeCsvServiceImpl someCsvService;
< /code>
it is showing, that someCsvService is not assigned (means the value is not initialized). When there was @MockBean annotation, there wasn't such warning. Hope to get help from communite!!!
Simplified jenkins pipeline:
import hudson.util.Secret

node('maven38'){
try {
stage('Checkout code') {
git(branch: '$BRANCH', credentialsId: 'GITHUB', url: 'https://NDA')
}
stage('Run Test') {
withCredentials([usernamePassword(credentialsId: 'DEVOPS', passwordVariable: 'NEXUS_PASS', usernameVariable: 'NEXUS_USER')]) {
sh ''' mvn --settings ${WORKSPACE}/settings.xml -Dspring.profiles.active=test -Dsurefire.suiteXmlFiles=src/test/resources/suites/api-testng.xml clean test '''
}
}
} catch (e) {
echo 'This will run only if failed' throw e
} finally { ...
< /code>
Tried changing private to protected Tried to move field to the test classes Tried to change the configuration with different combinations Cannot migrate to the @MockBean to test as it is marked for removal (of course I can suppress removal warnings, but that's not a solution)
Expect not to see error with null instead of object in mock configuration on Jenkins

Top