【问题标题】:How to Mock a Service Class in Spring Boot to be used in the testing of another Service Class? JUNIT 5如何在 Spring Boot 中模拟一个服务类以用于测试另一个服务类?六月 5
【发布时间】:2021-11-29 13:12:11
【问题描述】:

我有两个服务 ServiceOne.classServiceTwo.class。 我试图模拟 ServiceTwo,但没有在线解决方案有效,并且在 ServiceOne.class 的测试中,尽管明确提及,但它表现得好像没有得到任何结果。

我知道有很多类似的解决方案,但没有一个解决方案适合我。

methodTest 是我应该测试的 ServiceOne.class 中的方法。


@SpringBootTest
class ServiceOneTest {

    @MockBean
    private Repo1 repo1;
    @MockBean
    private Repo2 repo2;
    @MockBean
    private Repo3 repo3;
    @MockBean
    private Repo4 repo4;
    @MockBean
    private Repo5 repo5;
    @MockBean
    private Repo6 repo6;
    @MockBean
    private Repo7 repo7;
    @MockBean
    private Repo8 repo8;
    @MockBean
    private Repo9 repo9;
    @MockBean
    private Repo10 repo10;
    @MockBean
    private Repo11 repo11;

    @MockBean
     private ServiceTwo serviceTwo;

    @Autowired
    private ServiceOne serviceOne;

@BeforeEach
    void setUp() throws Exception { 
//static objs1-11 and serviceObj to be returned as mock data here
}

@AfterEach
    void tearDown() throws Exception {
}

@Test
    void methodTest() throws JsonProcessingException {
   when(repo1.findAll()).thenReturn(obj1);
   when(repo2.findAll()).thenReturn(obj2);
   //for other repo3...repo11
   when(repo11.findAll()).thenReturn(obj11);
   
  when(serviceTwo.getObj(params).thenReturn(serviceObj);

  String result= serviceOne.method();
  assertEquals(expectedResult, result);
}

serviceObj 没有返回,因此 sn-p 抛出错误并且没有到达 assert 语句。

【问题讨论】:

    标签: spring-boot mockito junit5


    【解决方案1】:

    我有两个服务类:ServiceAServiceB

    ServiceA 有一个 getInteger() 总是返回 5 ServiceB 有一个 getLong(),它在 ServiceA 上调用 getInteger() 并返回 long 值。

    ServiceA

    @Service
    public class ServiceA {
    
        public Integer getInteger() {
            return 5;
        }
    
    }
    

    ServiceB

    @Service
    public class ServiceB {
    
        @Autowired
        ServiceA serviceA;
    
        public Long getLong() {
            return serviceA.getInteger().longValue();
        }
    
    }
    
    

    以下是我的单元测试:

    ServiceATest

    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import static org.assertj.core.api.Assertions.assertThat;
    
    @SpringBootTest
    class ServiceATest {
    
    
        @Autowired
        ServiceA serviceA;
    
        @Test
        void getInteger() {
    
            assertThat(serviceA.getInteger()).isEqualTo(5);
        }
    }
    

    ServiceBTest

    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.boot.test.mock.mockito.MockBean;
    import static org.assertj.core.api.Assertions.assertThat;
    import static org.mockito.Mockito.when;
    
    @SpringBootTest
    class ServiceBTest {
    
        @Autowired
        ServiceB serviceB;
    
        @MockBean
        ServiceA serviceA;
    
    
    
        @Test
        void getLong() {
    
            when(serviceA.getInteger()).thenReturn(7);
            assertThat(serviceB.getLong()).isEqualTo(7L);
    
        }
    }
    

    【讨论】:

    • 有没有办法用@SpringBootTest 和@MockBean 做到这一点?
    • @AmishRaj 我已经编辑了我的答案,在 @SpringBootTest@MockBean 中做同样的事情。
    • @AmishRaj 如果对您有帮助,请接受答案。 :)
    • 它仍然无法使用模拟服务A并抛出错误
    • @AmishRaj 请发布它抛出的错误
    猜你喜欢
    • 1970-01-01
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 2017-10-31
    相关资源
    最近更新 更多