Spring Boot中的单元测试服务层

时间:2020-04-16 18:14:40

标签: unit-testing mockito junit5 spring-boot-test domain-service-class

我有一个服务层(下面的代码:)

@Service
public class EquityFeedsService {

    @Autowired
    private EquityFeedsRedisRepositoryImpl equityFeedsRedisRepositoryImpl;

    public void save(EquityFeeds equityFeeds) {

        logger.info("Inside the save method of EquityFeedsService.");

        equityFeedsRedisRepositoryImpl.save(equityFeeds);
    }

   // other methods 
}

现在,我正在尝试为下面的上述方法编写一个单元测试用例:

@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
public class EquityFeedsServiceTest {

    private MockMvc mockMvc;

    @InjectMocks
    private EquityFeedsService equityFeedsService;

    @Mock
    private EquityFeedsRedisRepositoryImpl equityFeedsRedisRepositoryImpl;

    @BeforeEach
    public void setUp() {
        mockMvc = MockMvcBuilders.standaloneSetup(equityFeedsService).build();
    }

    @Test
    public void testSaveMethod() {
        EquityFeeds equityFeeds = new EquityFeeds(423,"SAPEXTXN1", "GS");

        when(equityFeedsRedisRepositoryImpl.save(any(EquityFeeds.class))).thenReturn(new EquityFeeds());


    }
}

此代码为我提供了以下异常: 符合(any(EquityFeeds.class))

Required type:
EquityFeeds (This is my model class) 

Provided:
Matcher <com.investmentbank.equityfeedsprocessingupdated.model.EquityFeeds> (This is the fully qualified path name of the mode class) 
no instance(s) of type variable(s) T exist so that Matcher<T> conforms to EquityFeeds

和例外:

Cannot resolve method 'thenReturn(com.investmentbank.equityfeedsprocessingupdated.model.EquityFeeds)'     

我的单元测试用例有什么问题?我该如何解决?

0 个答案:

没有答案