package ru.mongo.acl.controller; import org.junit.Before; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import javax.servlet.Filter; import java.nio.charset.Charset; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration public abstract class SpringTest { public static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); protected MockMvc mockMvc; @Autowired private WebApplicationContext context; @Autowired private Filter springSecurityFilterChain; abstract String getLogin(); abstract String getPassword(); /** * This method initializes the mock objects */ @Before public void setUp() { mockMvc = MockMvcBuilders .webAppContextSetup(context) .defaultRequest(get("/").with(authentication(new UsernamePasswordAuthenticationToken(this.getLogin(), this.getPassword())))) .addFilters(springSecurityFilterChain) .build(); } }