citrus icon indicating copy to clipboard operation
citrus copied to clipboard

Datasource is not injected with @CitrusEndpoint annotation

Open svettwer opened this issue 6 years ago • 6 comments

Citrus Version 2.8.0

Expected behavior Annotating a DataSource with @CitrusEndpoint injects a Datasource bean into the test case. This is not an issue with Junit4 and TestNG as you could simply use the @Autowired annotation but with Junit5, you have to use @CitrusEndpoint for endpoint injections which leads to this issue.

Actual behavior Currently the DataSource field annotated with @CitrusEndpoint is null.

Test case sample

@ExtendWith(CitrusExtension.class)
class testDatasourceInjection{

    @CitrusEndpoint
    private DataSource dataSource;

    @CitrusTest
    @Test
    void testAddNewXingAccount(@CitrusResource final TestRunner runner){
        assertNotNull(dataSource);
    }
}

One could argue that a DataSource is not an endpoint from a technical perspective but in this case, Citrus uses the DataSource to create a connection to the database. Maybe it would make sense to create an SQL endpoint which just wraps the DataSource for further usage. Another more hot-fixish attempt would be to inject DataSoruces in addition to Endpoints from the Spring context. But I think an dedicated endpoint wrapping the DataSource would improve the convention of using endpoints for communication.

svettwer avatar May 16 '19 15:05 svettwer

If you add the SpringExtension for JUnit5 you should be able to use @Autowired here, too. So not sure if this is an issue.

christophd avatar Jul 22 '19 19:07 christophd

Hi, this does not work unfortunately. That's why I opened that issue.

svettwer avatar Nov 15 '19 08:11 svettwer

The sample test case in the problem description does not use the SpringExtension nor @Autowired so I could not tell by the sample that this is not working.

If you are using those Spring features and it still does not work it implies that the combination of extensions is not working?

christophd avatar Nov 15 '19 08:11 christophd

I'll double check that just to be sure. It's too long ago that I tested it.

svettwer avatar Nov 15 '19 10:11 svettwer

I exactly have the same problem with 2.8.0.

I populated the Datasource in a citrus config class but no injection is possible in a test case. @Bean public DataSource dataSource() { ..}

chrisupb avatar Jul 12 '21 10:07 chrisupb

This is the only solution, how I got it working...

` @ExtendWith({SpringExtension.class,CitrusExtension.class}) @ContextConfiguration(classes = {CitrusConfig.class}) public class testDatasourceInjection {

@Autowired private DataSource dataSource;

} `

chrisupb avatar Jul 14 '21 09:07 chrisupb