不能让我的@SpringBootTest工作.它说身份验证已启用,我不想要.
我已经设置好了 @AutoConfigureMockMvc(secure = false)
我提交了一个带有一些JSON的模拟请求,我的集成测试应该测试整个堆栈,通过Web层将SDR带到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 …Run Code Online (Sandbox Code Playgroud) Spring Boot 版本:2.0.4.RELEASE
对于下面的 Spring Boot 测试,测试返回一个不需要的 401 响应:
“401”状态,“错误”:“未授权”
为测试禁用 Spring Security 的最佳方法是什么?
我尝试添加一个配置“security.basic.enabled=false”属性,如下所示:
@SpringBootTest(<br>
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,<br>
classes = TestApp.class,<br>
properties = {
"security.basic.enabled=false"
})
Run Code Online (Sandbox Code Playgroud)
并将此注释添加到类中:
@AutoConfigureMockMvc(secure = false)
Run Code Online (Sandbox Code Playgroud)
但不幸的是,测试仍然返回“401”未经授权的错误代码。
原始测试
@RunWith(SpringRunner.class)
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = App.class
)
public class ExampleTest{
@Autowired
public void setup(@LocalServerPort int port) {
RestAssured.port = port;
}
@Test
public voidtestMethod() {
// This test code is not important to my question.
given().log().all().get("/resources").then().log().all().statusCode(HttpURLConnection.HTTP_BAD_REQUEST).body("error", equalTo("invalid_request")).body(not(containsString("error_reason")));
}
}
Run Code Online (Sandbox Code Playgroud)
被单元测试的类很简单:
@SpringBootApplication
@RestController
public class App …Run Code Online (Sandbox Code Playgroud) java spring-security spring-boot spring-security-test spring-boot-test