Spring mvc Spring引导集成测试忽略AutoConfigureMockMvc注释中的secure=false,获取401
无法使我的Spring mvc Spring引导集成测试忽略AutoConfigureMockMvc注释中的secure=false,获取401,spring-mvc,spring-boot,spring-security,tdd,spring-data-rest,Spring Mvc,Spring Boot,Spring Security,Tdd,Spring Data Rest,无法使我的@SpringBootTest工作。它说身份验证已打开,这是我不想要的 我已经用@AutoConfigureMockMvc(secure=false) 我提交了一个带有一些JSON的模拟请求,我的集成测试应该测试整个堆栈,使用SDR将其通过web层传输到JPA,然后放入内存数据库,这样我就可以使用JdbcTemplate对其进行测试 但响应为401,需要身份验证。为什么@AutoConfigureMockMvc(secure=false)不够?少了什么 @RunWith(SpringR
@SpringBootTest
工作。它说身份验证已打开,这是我不想要的
我已经用@AutoConfigureMockMvc(secure=false)
我提交了一个带有一些JSON的模拟请求,我的集成测试应该测试整个堆栈,使用SDR将其通过web层传输到JPA,然后放入内存数据库,这样我就可以使用JdbcTemplate
对其进行测试
但响应为401
,需要身份验证。为什么@AutoConfigureMockMvc(secure=false)
不够?少了什么
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = { TestDataSourceConfig.class })
@EnableAutoConfiguration
@AutoConfigureMockMvc(secure = false)
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2)
@Transactional
public class SymbolRestTests {
@Autowired
private MockMvc mockMvc;
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private SymbolRepository symbolRepository;
@PersistenceContext
private EntityManager entityManager;
@Test
public void shouldCreateEntity() throws Exception {
String testTitle = "TEST.CODE.1";
String testExtra = "Test for SymbolRestTests.java";
String json = createJsonExample(testTitle, testExtra, true);
log.debug(String.format("JSON==%s", json));
MockHttpServletRequestBuilder requestBuilder =
post("/symbols").content(json);
mockMvc.perform(requestBuilder)
.andExpect(status().isCreated())
.andExpect(header().string("Location",
containsString("symbols/")));
entityManager.flush();
String sql = "SELECT count(*) FROM symbol WHERE title = ?";
int count = jdbcTemplate.queryForObject(
sql, new Object[]{testTitle}, Integer.class);
assertThat(count, is(1));
}
输出日志记录:
MockHttpServletRequest:
HTTP Method = POST
Request URI = /symbols
Parameters = {}
Headers = {}
Handler:
Type = null
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 401
Error message = Full authentication is required to access this resource
Headers = {X-Content-Type-Options=[nosniff],
X-XSS-Protection=[1; mode=block],
Cache-Control=[no-cache, no-store, max-age=0, must-revalidate],
Pragma=[no-cache],
Expires=[0],
X-Frame-Options=[DENY],
Strict-Transport-Security=[max-age=31536000 ; includeSubDomains],
WWW-Authenticate=[Basic realm="Spring"]}
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
我从中发现,我可以通过以下属性禁用安全性:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = { TestDataSourceConfig.class },
properties = {
"security.basic.enabled=false"
})
但是实际上,@AutoConfigureMockMvc(secure=false)
应该可以工作,那么是什么阻止了它呢
因为我最近在将
更新为2.1.3.发布
和
到5.1.4.发布
后也遇到了这个问题,这迫使添加SpringWeb安全性,如果有人不想提供安全解决程序,那么他们需要在测试环境中禁用安全性,所以我决定分享我最终如何解决这个问题
在使用@SpringBootTest
开发应用程序和编写集成测试用例时,我也在挠头。使用@WebMvcTest
比这痛苦得多,tbh
在我的情况下,以下方法有效
@EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class)//This annotation was required to run it successfully
@DisplayName("UserControllerTest_SBT - SpringBootTest")
class UserControllerTest_SBT extends BaseTest_SBT {
@Autowired
private MockMvc mockMvc;
@Test
void getUsersList() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.get("/user/listAll")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(print());
}
}
@ExtendWith(SpringExtension.class) //This is not mandatory
@SpringBootTest
@AutoConfigureMockMvc(secure = false) // Secure false is required to by pass security for Test Cases
@ContextConfiguration //This is also not mandatory just to remove annoying warning, i added it
public class BaseTest_SBT {
}
什么不起作用:
1-@SpringBootTest(属性={“security.basic.enabled=false”})
security.basic.enabled=false
希望这会对某些人有所帮助。改变
@AutoConfigureMockMvc(secure = false)
到
适合我。这可能不是问题的根本原因,但您的配置有点不寻常。为什么您使用
@SpringBootTest
配置为在随机端口上启动嵌入式容器,而使用MockMvc
进行测试?我希望看到@springbootest
使用模拟web环境,或者@WebMvcTest
取而代之。无论我使用WebEnvironment.RANDOM
还是WebEnvironment.mock
,都没有任何区别,尽管无可否认,使用mock
似乎更一致。而@WebMvcTest
在让JPA运行时给了我很多问题。类似地,@DataJpaTest
给了我有关MockMvc
的问题。我认为对堆栈进行切片不是一个好办法。你要么使用@springbootest
来引导一个完整的应用程序,要么使用测试切片,你不应该将它们组合起来@AutoConfigureMockMvc(secure=false)
是要使用的。因此,在您当前的设置中,当您正在引导完整的应用程序时,它将不起作用。在这种情况下,您应该通过属性禁用它。您是否准备好将这些语句转换为答案?谢谢您的回答。在那个问题提出后的一年里,我被迫放弃寻找答案。我发现Spring在某些方面非常好,但我放弃了Spring测试作为一个框架,只是坚持手工完成这些额外的工作。没花太长时间,坦白说,我没有选择的余地。由于所有的注释,通过遵循源代码几乎不可能找出问题所在,而且也没有真正的方法将其减少到最低限度以使其正常工作,以此作为基础重新构建它以找到它的故障点。尽管有两个关键开发人员的评论:(是的,老兄,这太糟糕了。即使对我来说,我也不得不花整整两天的时间来解决可能出现的问题,因为其他人提到使用@SpringBootTest的属性就足够了,但它不起作用。很高兴听到你设法以其他方式完成它。这可能会帮助像我们这样的人,他们在后续操作后会遇到问题正在修改过时的教程。@Tejasjain很高兴它对您有帮助:)看起来不错,但仍然没有帮助。@Configuration
类仍然是从@springbootest
调用的。我使用“restemplate,没有模仿”调用端点。这适用于@WebMvcTest
测试,但不适用于@springbootest
测试。
@AutoConfigureMockMvc(addFilters=false)