【问题标题】:Migrating from JUnit 4 to JUnit 5 issue (@RunWith to @ExtendWith)从 JUnit 4 迁移到 JUnit 5 问题(@RunWith 到 @ExtendWith)
【发布时间】:2018-05-17 05:13:29
【问题描述】:

我正在尝试将 Spring Boot 控制器的测试代码从 JUnit 4 转换为 JUnit 5。我几乎用 JUnit 5 替换了 JUnit 4 的所有注释,但是在尝试用 @987654322 替换 @RunWith 时遇到了一些问题@。

    @ExtendWith(SpringExtension.class)
    @WebMvcTest(value = HappyPostController.class, secure = false)
    @TestInstance(TestInstance.Lifecycle.PER_CLASS)
    public class HappyPostControllerTest {

        @Autowired
        private MockMvc mockMvc;

        @Mock
        private HappyPostServiceImpl happyPostService;

        @InjectMocks
        private HappyPostController happyPostController;

        @BeforeAll
        public void initialize() {
            happyPostController = new HappyPostController(happyPostService);
            MockitoAnnotations.initMocks(this);
            this.mockMvc = MockMvcBuilders
                    .standaloneSetup(happyPostController)
                    .build();
        }

        @Test
        public void testMockCreation() {
            Assertions.assertNotNull(happyPostService);
            Assertions.assertNotNull(mockMvc);
        }

        //..... other test methods
}

错误:

java.lang.IllegalStateException: Failed to load ApplicationContext
......

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'happyPostController' defined in file [....\HappyPostController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.rok.capp.service.HappyPostService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

.....

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.rok.capp.service.HappyPostService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

........

Test ignored.

我试图找到解决方案但失败了。请帮忙。

谢谢

【问题讨论】:

  • 我认为您必须在happyPostService 字段上使用@MockBean,以便将其添加到应用程序上下文中。
  • 有效!!但是为什么@Mock 在 Junit5 中却不能在 Junit4 中正常工作?
  • 我必须查看您在 JUnit 4 中使用的确切代码才能回答这个问题。

标签: unit-testing spring-boot junit5


【解决方案1】:

我不知道该测试类的基于 JUnit 4 的版本是什么样的,但您提供的示例是集成测试和单元测试的不必要混合。

您正在使用SpringExtension 创建ApplicationContext,但您从未使用过ApplicationContextSpring TestContext FrameworkSpring Boot Test 中的任何其他功能.

因此,最好的解决方案就是摆脱所有不必要的集成测试支持,并如下重写您的测试类。

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class HappyPostControllerTest {

    @Mock
    HappyPostServiceImpl happyPostService;

    MockMvc mockMvc;

    @BeforeAll
    void initialize() {
        MockitoAnnotations.initMocks(this);
        HappyPostController happyPostController = new HappyPostController(happyPostService);
        this.mockMvc = MockMvcBuilders
                .standaloneSetup(happyPostController)
                .build();
    }

    @Test
    void testMockCreation() {
        assertNotNull(happyPostService);
        assertNotNull(mockMvc);
    }

    //..... other test methods
}

问候,

Sam (Spring TestContext 框架的作者)

【讨论】:

  • 我试过你的方法。 pastebin.com/p0P5vMbv 这在第 43 行得到 NullPointerException happyPostService.savePost(happyPost);
  • 好吧,如果没有看到HappyPostService的源代码,就不可能为您提供帮助。
【解决方案2】:

我在这里找到了解决方案:http://www.bytestree.com/spring/spring-boot-2-junit-5-rest-api-unit-testing/

这里根据您的情况更新:

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@ExtendWith(SpringExtension.class)
@WebMvcTest(HappyPostController.class)
class LotteryControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private HappyPostServiceImpl happyPostService;

    @Test
    public void should_get() throws Exception {
        // Given
        Mockito.when(happyPostService.whatevermethod()).thenReturn(someresult);
        // When
        this.mockMvc.perform(MockMvcRequestBuilders.get("/your_api"))
        // Then
        .andExpect(MockMvcResultMatchers.status().isOk());
    }

使用这些依赖项

<dependency>
   <groupId>org.junit.jupiter</groupId>
   <artifactId>junit-jupiter-engine</artifactId>
   <version>5.2.0</version>
</dependency>
<dependency>
   <groupId>org.mockito</groupId>
   <artifactId>mockito-core</artifactId>
   <version>2.21.0</version>
</dependency>
<dependency>
   <groupId>org.mockito</groupId>
   <artifactId>mockito-junit-jupiter</artifactId>
   <version>2.21.0</version>
 </dependency>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 2018-04-03
    • 2021-10-27
    • 2013-08-07
    • 1970-01-01
    • 2016-11-29
    相关资源
    最近更新 更多