具有模拟存储库的春季测试服务

时间:2019-07-18 14:51:19

标签: junit spring-data-mongodb

我想在单元测试中模拟一些mongo存储库。我设法使用以下注释在控制器上运行此程序:

@RunWith(SpringRunner.class)
@Import(TestConfig.class)
@WebMvcTest(controllers = TemplatesController.class, excludeAutoConfiguration = EmbeddedMongoAutoConfiguration.class)
public class TemplatesControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private TemplateRepository repository;
}

现在,我也想测试一项服务,但在那里我无法在CI环境中正常工作。我缺少什么,这是我当前的注释:

@RunWith(SpringRunner.class)
@Import(TestConfig.class)
@SpringBootTest
@TestPropertySource(properties = "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration")
public class UserServiceTest {

    @Autowired
    private UserService userService;

    @MockBean
    private UserRepository userRepository;
}

这在本地有效,因为我在默认端口上运行了mongoDB。但是,我们的Bamboo服务器当然没有运行mongoDB实例。我认为@TestPropertySource可以达到与excludeAutoConfiguration的{​​{1}}相同的效果。

0 个答案:

没有答案
相关问题