English 中文(简体)
一些测试没有进行梯度测试指挥(Spring Boot 3)
原标题:Some tests fails with gradle test command (Spring Boot 3)

My environment is Spring Boot 3.0.2, Gradle and Java 17. I implemented an application with more than 300 unit and integration tests.

经过200次测试,我的套体变得不稳定。 如果我从Intellij开始测试,所有测试都是正确的。 如果我从梯度指挥处接受一次试验,他们也会做罚款。 但是,如果我一起进行所有测试,一些随机失败。

错误是,它没有装上某些 mo子。

如果我把这一配置放在大楼里的话。 然后,所有测试都很顺利,但需要很长时间。

tasks.named( test ) {
    forkEvery = 1
    useJUnitPlatform()
}

如果我把这一组合放在一边,那么其他试验就会失败。

tasks.named( test ) {
    maxParallelForks = Runtime.runtime.availableProcessors()
    useJUnitPlatform()
}

难道这是一个记忆问题吗? 平行进程或类似情况? 事实非常奇怪,没有给我任何信任。

例如,测试班。

package ...
import ...

@SpringBootTest
@ActiveProfiles("test")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class OfferCreateTest {

    @Autowired
    PropertyCreate propertyCreate;

    @Autowired
    OfferCreate offerCreate;

    @MockBean
    ObHttpClient obHttpClient;

    @MockBean
    RoomtypeFind roomtypeFind;

    @MockBean
    RateplanFind rateplanFind;

    @MockBean
    PropertyChannelFind propertyChannelFind;

    private Long propertyId;

    @BeforeAll
    void setup(){
        propertyId = createProperty();
    }

    private Long createProperty(){
        PropertyCreateDto propertyCreateDto = PropertyCreateDtoMother.randomWithFullData();
        return propertyCreate.create(propertyCreateDto).getResult().get("id");
    }

    @Test
    void create_offer(){

        OfferCreateDto offerCreateDto = OfferCreateDtoMother.random();
        Long offerId = offerCreate.create(propertyId, offerCreateDto).getResult().get("id");
        assertTrue(offerId >= 100000L);
    }

    @Test
    void create_offer_on_non_existing_property(){

        OfferCreateDto offerCreateDto = OfferCreateDtoMother.random();
        assertThrows(Exception.class, () -> offerCreate.create(ContentGenerator.customIdLower(), offerCreateDto));
    }


}

非常感谢

问题回答

In my case, the issue was due to improper usage of MockStatic. If you use MockStatic and do not release the resources, the mock object is used when another class is tested.

因此,根据测试顺序,这些试验要么成功,要么失败。

旧法典:

mockStatic(UserFactory.class))
    .when(() -> UserFactory.of(anyString(), anyMap()))
    .thenReturn(User.builder().build());
        
when(userRepositoryOAuth2UserHandler.apply(any())).thenReturn(User.builder().build());
when(jwtProvider.createAccessToken(any())).thenReturn("accessToken");
when(jwtProvider.createRefreshToken(any())).thenReturn("refreshToken");

oAuth2LoginSuccessHandler.onAuthenticationSuccess(request, response,
    createAuthentication());

assertAll(
    () -> assertThat(response.getStatus()).isEqualTo(SC_OK),
    () -> assertThat(response.getContentType()).isEqualTo(APPLICATION_JSON_VALUE),
    () -> verify(objectMapper).writeValue(eq(response.getOutputStream()),
              any(LoginSuccessResponse.class))
);

经修改的法典:

try (MockedStatic<UserFactory> mockUserFactory = mockStatic(UserFactory.class)) {
    mockUserFactory
        .when(() -> UserFactory.of(anyString(), anyMap()))
        .thenReturn(User.builder().build());

    when(userRepositoryOAuth2UserHandler.apply(any())).thenReturn(User.builder().build());
    when(jwtProvider.createAccessToken(any())).thenReturn("accessToken");
    when(jwtProvider.createRefreshToken(any())).thenReturn("refreshToken");

    oAuth2LoginSuccessHandler.onAuthenticationSuccess(request, response,
        createAuthentication());

    assertAll(
        () -> assertThat(response.getStatus()).isEqualTo(SC_OK),
        () -> assertThat(response.getContentType()).isEqualTo(APPLICATION_JSON_VALUE),
        () -> verify(objectMapper).writeValue(eq(response.getOutputStream()),
              any(LoginSuccessResponse.class))
    );
}

通过作出这些修改,我得以解决这个问题。





相关问题
Selenium not working with Firefox 3.x on linux

I am using selenium-server , selenium rc for UI testing in my application . My dev box is Windows with FireFox 3.5 and every thing is running fine and cool. But when i try to run selenium tests on my ...

Best browser for testing under Safari Mobile on Linux?

I have an iPhone web app I m producing on a Linux machine. What s the best browser I can use to most closely mimic the feature-limited version of Safari present on the iPhone? (It s a "slimmed down" ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Is there any error checking web app cralwers out there?

Wondering if there was some sort of crawler we could use to test and re-test everything when changes are made to the web app so we know some new change didn t error out any existing pages. Or maybe a ...

热门标签