I am writing a unit test for my Jersey API that uses MyBatis in the background.
This is the structure of my classes:
recreation service:
@Path("/api") public class HelloRestService { @Inject HelloBean helloBean; @GET @Path("/echo/{name}") public Response echo(@PathParam("name") String name) { return Response.status(200).entity(helloBean.sayHello(name)).build(); } }
Stateless EJB:
@Stateless public class HelloStatelessBean implements HelloBean {
MapBox Interface:
@Dependent @Mapper public interface EmployeeMapper { @Select("select * from EMPLOYEE where id = #{id}") Employee getEmployeeById(Long id); @Select("select * from EMPLOYEE") ArrayList<Employee> getAllEmployee(); }
I have a good unit test for testing my MyBatis mapmaker. It is working fine. The next step is that I would write a test for my leisure jersey class. This is what I have:
public class HelloRestServiceTest extends JerseyTest { @Override public Application configure() { enable(TestProperties.LOG_TRAFFIC); enable(TestProperties.DUMP_ENTITY); return new ResourceConfig(HelloRestService.class) { { register(new HelloStatelessBean()); register(Mockito.mock(EmployeeMapper.class)); } }; } @Test public void echo() throws Exception { Response response = target("/api/echo/John").request().get(); Assert.assertEquals(200, response.getStatus()); Assert.assertNotNull(response.getEntity()); } }
But this test throws an exception:
org.glassfish.jersey.internal.inject.Providers checkProviderRuntime WARNING: A provider abHelloStatelessBean registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider abHelloStatelessBean will be ignored. org.glassfish.jersey.internal.inject.Providers checkProviderRuntime WARNING: A provider abEmployeeMapper$$EnhancerByMockitoWithCGLIB$$ee86c913 registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider abEmployeeMapper$$EnhancerByMockitoWithCGLIB$$ee86c913 will be ignored. A MultiException has 1 exceptions. They are: 1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=EmployeeMapper,parent=HelloStatelessBean,qualifiers={},position=-1,optional=false,self=false,unqualified=null,52451302) MultiException stack 1 of 1 org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=EmployeeMapper,parent=HelloStatelessBean,qualifiers={},position=-1,optional=false,self=false,unqualified=null,52451302)
What is the right way to mock a MyBatis mapper with mockito?
UPDATE 1 The EJB test works like a charm:
@RunWith(MockitoJUnitRunner.class) public class HelloStatelessBeanTest { private static SqlSessionFactory sqlSessionFactory; @Spy private HelloBean helloBean = new HelloStatelessBean(); @Test public void sayHello() throws Exception { String actual = helloBean.sayHello("Pear"); System.out.println(actual); } @Test public void testGetEmployeeById() {
But this does not work with a T-shirt. I tried this this way, but before that I get the same exception:
public class HelloRestServiceTest extends JerseyTest { @Override public Application configure() { enable(TestProperties.LOG_TRAFFIC); enable(TestProperties.DUMP_ENTITY); return new ResourceConfig(HelloRestService.class) { { try (Reader reader = Resources.getResourceAsReader("configuration.xml")) { SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession sqlSession = sqlSessionFactory.openSession(); EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class); register(new HelloStatelessBean()); register(mapper, EmployeeMapper.class); } catch (Exception e) { throw new RuntimeException(e); } } }; } @Test public void echo() throws Exception { Response response = target("/api/echo/Arnold").request().get(); Assert.assertEquals(200, response.getStatus()); Assert.assertNotNull(response.getEntity()); } }
Any idea?