mardi 11 octobre 2016

Spring Boot 1.4 controller test slice without security

I have a restful API that implements OAuth2 with external auth server and I would like to test the controller layer of my application without testing security (which I would like to test independently later). I thought that this could be done with this new slicing feature of Spring Boot 1.4 but I am not able to figure out how.

Could you point me to some example dealing with this matter?

I have the following piece of code:

@RunWith(SpringRunner.class)
@WebMvcTest(PersonController.class)
public class PersonControllerTest {

    @Autowired
    private MockMvc mvc;

    @MockBean
    private PersonRepository personRepo;

    @Test
    public void testExample() throws Exception {
        Person p = new Person();
        p.setFirstName("John");
        p.setLastName("Doe");
        p.setGender(Gender.MALE);
        p.setId(1l);
        p.setEmail("john@doe.com");

        given(this.personRepo.findOne(1l)).willReturn(p);
        this.mvc.perform(get("/gigsterous-api/people/1").accept(MediaType.APPLICATION_JSON_VALUE))
                .andExpect(status().isOk()).andExpect(content().string("..."));
    }

}

and I am always getting 401 Unauthorized when running this test.

Aucun commentaire:

Enregistrer un commentaire