小编nav*_*a2b的帖子

禁用Spring Boot Starter Cassandra进行单元测试

我需要在具有以下gradle依赖的项目中使用Cassandra:

compile "org.springframework.boot:spring-boot-starter-data-cassandra"
Run Code Online (Sandbox Code Playgroud)

问题是,尽管将Cassandra AutoConfiguration类标记为从单元测试中排除,但spring仍会以某种方式尝试实例化与此依赖性相关的类。

这是我的单元测试:

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@RunWith( SpringRunner.class )
@SpringBootTest
@AutoConfigureMockMvc
@EnableAutoConfiguration( exclude = {
        org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration.class,
        org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration.class
} )
public class HealthTest {

    @Autowired
    private MockMvc mockMvc;


    @Test
    public void contextLoads() {
        Assert.assertNotNull( mockMvc );
    }

    @Test
    public void healthControllerIsUp() throws Exception {
        mockMvc.perform( MockMvcRequestBuilders.get( "/health" ) )
                .andExpect( MockMvcResultMatchers.status().isOk() );
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误,显然是因为Spring无法实例化群集对象。

java.lang.IllegalStateException: Failed …
Run Code Online (Sandbox Code Playgroud)

cassandra spring-mvc-test spring-boot spring-data-cassandra spring-boot-starter

5
推荐指数
1
解决办法
1180
查看次数