mercredi 17 janvier 2018

RxJava - testing REST api - returned observable type

I just created rest controller with method which returns variable of type Observable - simple boolean (check if user was created).

@RestController
public class UserController{
@RequestMapping(value = "/create/user",  method = RequestMethod.POST)
public Observable<Boolean> createUser(){
    return Observable.just(Boolean.TRUE);
}

And now I want to test that method. I don't know how. Tried few times, but still don't understand it.

@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@SpringBootTest
public class UserControllerTest {

@Autowired
private MockMvc mockMvc;

@Mock
private UserController userController;

@Before
public void init() {
    MockitoAnnotations.initMocks(this);
    this.mockMvc = MockMvcBuilders
            .standaloneSetup(userController)
            .build();
}

@Test
public void createUser_methodInvocation() throws Exception {
    mockMvc.perform(post("/create/user"));

    verify(userController, times(1)).createUser();
}

@Test
public void createUserTest() throws Exception {
    Observable<Boolean> expected = Observable.just(Boolean.TRUE);

    MockHttpServletResponse returned = mockMvc.perform(post("/create/user"))
            .andReturn()
            .getResponse();

    assertEquals(expected, returned);
}

In test I would like to get response from server and compare it with expected Observable object. (to the sample code - I know that types in 'assertEquals' are different, I just present an idea).

Aucun commentaire:

Enregistrer un commentaire