我想编写控制器测试来测试我的注释。到目前为止,我读到的是 RestAssured 是一种方法。
当我只有一个控制器测试到位时,它运行顺利。但是,当有 2 个或更多控制器测试类时,@MockBeans 似乎没有正确使用。根据测试执行顺序,第一个测试类的所有测试都成功,其他所有测试都失败。
在接下来的测试运行中,首先执行的是 PotatoControllerTest,然后是 FooControllerTest。
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles({"test", "httptest"})
class FooControllerTest {
@MockBean
protected FooService mockFooService;
@MockBean
protected BarService mockBarService;
@LocalServerPort
protected int port;
@BeforeEach
public void setup() {
RestAssured.port = port;
RestAssured.authentication = basic(TestSecurityConfiguration.ADMIN_USERNAME, TestSecurityConfiguration.ADMIN_PASSWORD);
RestAssured.requestSpecification = new RequestSpecBuilder()
.setContentType(ContentType.JSON)
.setAccept(ContentType.JSON)
.build();
}
@SneakyThrows
@Test
void deleteFooNotExists() {
final Foo foo = TestUtils.generateTestFoo();
Mockito.doThrow(new DoesNotExistException("missing")).when(mockFooService).delete(foo.getId(), foo.getVersion());
RestAssured.given()
.when().delete("/v1/foo/{id}/{version}", foo.getId(), foo.getVersion())
.then()
.statusCode(HttpStatus.NOT_FOUND.value());
Mockito.verify(mockFooService, times(1)).delete(foo.getId(), foo.getVersion());
}
...
}
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles({"test", "httptest"})
class PotatoControllerTest {
@MockBean
protected PotatoService mockPotatoService;
@LocalServerPort
protected int port;
@BeforeEach
public void setup() {
RestAssured.port = port;
RestAssured.authentication = basic(TestSecurityConfiguration.ADMIN_USERNAME, TestSecurityConfiguration.ADMIN_PASSWORD);
RestAssured.requestSpecification = new RequestSpecBuilder()
.setContentType(ContentType.JSON)
.setAccept(ContentType.JSON)
.build();
}
...
}
Wanted but not invoked:
fooService bean.delete(
"10e76ae4-ec1b-49ce-b162-8a5c587de2a8",
"06db13f1-c4cd-435d-9693-b94c26503d40"
);
-> at com.xxx.service.FooService.delete(FooService.java:197)
Actually, there were zero interactions with this mock.
我试图用一个ControllerTestBase
配置所有模拟和所有其他扩展基类的控制器测试的通用来修复它。这在我的机器上运行良好,但例如不在管道中。所以我想它不是很稳定。
为什么 Spring 不使用模拟重新加载上下文?这是测试我的控制器的“最佳”方式吗?