I have a class for rest calls. This class holds a bean injection. What I want to now is write a unit test for the method that is using the injected class. However I can't seem to get this working. Is anyone able to tell me what is going wrong?
The rest class looks like this
@Path("/rekeningrijders")
@RequestScoped
public class RekeningrijderService {
@Inject
UserService userservice;
@GET
@Path("/status")
@Produces({MediaType.APPLICATION_XML})
public String getMonitoring() {
return "<Status><Note>ALLES-IS-OK-HIER</Note></Status>";
}
@GET
@Path("/users")
@Produces({MediaType.APPLICATION_JSON})
public List<User> getUsers() {
return userservice.findAll();
}
}
The UserService looks like this
@Stateless
public class UserService {
@PersistenceContext(name = "RekeningrijdersApplicatiePU")
EntityManager em;
......
public List<User> findAll() {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(User.class));
try {
return em.createQuery(cq).getResultList();
} catch (NoResultException ex) {
Logger.getLogger(UserService.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
}
What I did so for in my unit test is the
public class RestTest {
private final RekeningrijderService rekeningrijdersService = new RekeningrijderService();
private final RekeningrijderService t2 = Mockito.mock(RekeningrijderService.class);
@InjectMocks
UserService userService;
@Before
public void setUp(){
RestAssured.baseURI = "http://ift.tt/28VIvxu";
RestAssured.basePath = "RekeningrijdersApplicatie/api/rekeningrijders";
}
@Test
public void testStatus(){
Response response = when().get("/status").then().contentType(ContentType.XML).extract().response();
String responseBody = response.getBody().asString();
assertEquals("<Status><Note>ALLES-IS-OK-HIER</Note></Status>", rekeningrijdersService.getMonitoring());
assertEquals("<Status><Note>ALLES-IS-OK-HIER</Note></Status>", responseBody);
}
@Test
public void testGetUsers(){
Response response = when().get("/users").then().contentType(ContentType.JSON).extract().response();
String responseBody = response.getBody().asString();
responseBody = "[]";
String a = t2.getUsers().toString();
assertEquals(responseBody, t2.getUsers().toString());
}
the problem lays in the testGetUser method. As you can tell I'm messing with it but when I use the rekeningrijdersService object it will return a null pointer exception. When I use a mocked object it will return an empty string while it should hold 1 user.
Could anyone possible tell me what I'm doing wrong?
Aucun commentaire:
Enregistrer un commentaire