Reputation: 251
I'm trying to configure a Jersey (2.4) test using Spring application context, using Spring profiles.
My test class is this:
public class OrderControllerTest extends JerseyTest {
@Override
protected Application configure() {
enable(TestProperties.LOG_TRAFFIC);
enable(TestProperties.DUMP_ENTITY);
ApplicationResourceConfig config = new ApplicationResourceConfig();
config.property("contextConfigLocation", "classpath:spring-context.xml");
config.property("spring.profiles.active", "development");
return config;
}
...
Where ApplicationResourceConfig
class is this:
public class ApplicationResourceConfig extends ResourceConfig {
public ApplicationResourceConfig() {
register(RequestContextFilter.class)
.register(OrderController.class);
}
...
and OrderController
class is:
@Component
@Path("/order")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class OrderController {
@Autowired
private OrderService orderService;
@PUT
public Response createOrder(String order) {
return Response.ok().build();
}
...
The problem is that DI is not working (orderService
is null
). The DI is working well in other tests (not Jersey-based). I suppose the reason could be that Spring profile is not correctly loaded, since Spring logs say:
426 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Searching for key 'spring.profiles.active' in [systemProperties]
426 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Searching for key 'spring.profiles.active' in [systemEnvironment]
426 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.profiles.active' in any property source. Returning [null]
Is there anybody who can provide suggestions about this issue, please?
Upvotes: 3
Views: 3671
Reputation: 221
I've created an example application how to integrate Jersey 2 with Spring Framework and also how to test these application. I hope it's helpful.
https://github.com/Hylke1982/jersey2-spring-test-example
-- Edit I also created a new test framework that allows you to use test with Jersey, Spring Framework and Mockito.
https://github.com/Hylke1982/jersey-spring-exposed-test-framework-core
Upvotes: 1