相关疑难解决方法(0)

java.lang.IllegalArgumentException:需要ServletContext来配置默认的servlet处理

我有以下测试类:

@ActiveProfiles({ "DataTC", "test" })
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {BaseTestConfiguration.class, DataTestConfiguration.class, JpaConfiguration.class, PropertyPlaceholderConfiguration.class })
public class RegularDayToTimeSlotsTest {
...
Run Code Online (Sandbox Code Playgroud)

问题似乎来自BaseTestConfiguration类:

@Configuration
@ComponentScan(basePackages = { "com.bignibou" }, excludeFilters = { @Filter(type = FilterType.CUSTOM, value = RooRegexFilter.class),
        @Filter(type = FilterType.ANNOTATION, value = Controller.class), @Filter(type = FilterType.ANNOTATION, value = ControllerAdvice.class) })
public class BaseTestConfiguration {

}
Run Code Online (Sandbox Code Playgroud)

我系统地得到这个例外:

Caused by: java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer.<init>(DefaultServletHandlerConfigurer.java:54)
    at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping(WebMvcConfigurationSupport.java:329)
    at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerByCGLIB$$bb4ceb44.CGLIB$defaultServletHandlerMapping$22(<generated>)
    at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerByCGLIB$$bb4ceb44$$FastClassByCGLIB$$368bb5c1.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:326)
    at …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc spring-test

55
推荐指数
3
解决办法
5万
查看次数

如何使用JUnit在Spring中获取数据库连接?

我正在尝试使用服务和方法的正确逻辑来测试DB提供的正确信息.在这个简单的例子中,我只是使用语句assertEquals来比较给出的id roleService,但我仍然遇到错误.我有以下代码:

[更新]

测试方法:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = { "classpath:applicationContext.xml" })
@Transactional
@WebAppConfiguration
@ComponentScan(basePackages ={ "com.project.surveyengine" },
            excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = Configuration.class) ,
                              @ComponentScan.Filter(type = FilterType.ANNOTATION, value = WebConfig.class)})
public class RoleServiceTest {

@Configuration
    static class ContextConfiguration {    
        @Bean
        public RoleService roleService() {
            RoleService roleService = new RoleService();
            // set properties, etc.
            return roleService;
        }
    }

    private final LocalServiceTestHelper helper =
            new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig()
                    .setDefaultHighRepJobPolicyUnappliedJobPercentage(100));

    private Closeable closeable;

    @Before
    public void setUp() {
        helper.setUp();
        ObjectifyService.register(Role.class); …
Run Code Online (Sandbox Code Playgroud)

java google-app-engine junit spring unit-testing

7
推荐指数
1
解决办法
2209
查看次数

找不到java.lang.NoClassDefFoundError类:javax/servlet/SessionCookieConfig

我在项目中有依赖项

 compile("org.springframework.boot:spring-boot-starter-data-jpa") {
    exclude group: "org.apache.tomcat", module: "tomcat-jdbc"
    exclude group: "org.hibernate", module: "hibernate-entitymanager"
}
compile("org.springframework.boot:spring-boot-starter-security")
compile("org.springframework.boot:spring-boot-starter-mail")
compile("org.springframework.boot:spring-boot-configuration-processor")
compile("org.eclipse.persistence:org.eclipse.persistence.jpa")
compile("org.eclipse.persistence:org.eclipse.persistence.jpa.modelgen.processor")
compile("com.google.api-client:google-api-client")
compile("com.google.oauth-client:google-oauth-client-jetty")
compile("com.google.apis:google-api-services-drive")

   // dependencies from the inherited module (compile(project("..."))
    api("com.fasterxml.jackson.core:jackson-databind")
    api("org.hibernate.validator:hibernate-validator")
    api("commons-validator:commons-validator")
    api("org.apache.commons:commons-lang3")
    implementation("com.google.guava:guava")
Run Code Online (Sandbox Code Playgroud)

我想做集成测试,所以我添加了依赖项

testCompile("com.github.springtestdbunit:spring-test-dbunit:1.3.0")
testCompile("org.dbunit:dbunit:2.5.4")
Run Code Online (Sandbox Code Playgroud)

我创建了基本配置类

/**
 * Spring configuration class for integration tests.
 */
@Configuration 
@EnableAutoConfiguration 
@ComponentScan
public class PopcornCoreTestApplication {}
Run Code Online (Sandbox Code Playgroud)

和一个抽象类

/**
 * Base class to save on configuration.
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = PopcornCoreTestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@TestExecutionListeners(
        {
                DependencyInjectionTestExecutionListener.class,
                DirtiesContextTestExecutionListener.class,
                TransactionalTestExecutionListener.class,
                TransactionDbUnitTestExecutionListener.class
        }
)
public …
Run Code Online (Sandbox Code Playgroud)

java spring integration-testing spring-mvc spring-boot

5
推荐指数
2
解决办法
8149
查看次数

如何在 Spring boot 中集成测试存储库

我是 Spring Boot 的新手,制作了我的第一个实体。如何测试存储库?我正在使用本地 MySQL 数据库。那么第一步是模拟数据库吗?

    @RunWith(SpringRunner.class)
    @DataJpaTest
    public class ClusterTest {

        @Autowired
        private TestEntityManager testEntityManager;

        @Autowired
        private ClusterRepository clusterRepository;

        @Test
        public void testExample() throws Exception{
            this.testEntityManager.persist(new Cluster("Project", "System"));
Cluster cluster = this.clusterRepository.findOne(1);
            assertThat(testcluster.getProject()).isEqualTo("System");
        }

    }
Run Code Online (Sandbox Code Playgroud)

这是我的集群实体

@Entity
@Table(name = "Cluster")
public class Cluster {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    private String project;

    public Cluster(){

    }

    public Cluster(String project) {
        this.project = project;

    }
Run Code Online (Sandbox Code Playgroud)

我的存储库只是扩展了 JpaRepository。

我收到的错误是 java.lang.IllegalStateException: 无法加载 ApplicationContext。还有一点不足:无法用嵌入式数据库替换 DataSource 进行测试。如果您想要一个嵌入式数据库,请将受支持的数据库放在类路径上或调整@AutoconfigureTestDatabase 的替换属性。

这是我的 application.properties 文件,来自 src/main/resources/application.properties:

security.basic.enabled=false …
Run Code Online (Sandbox Code Playgroud)

java mysql spring integration-testing

4
推荐指数
1
解决办法
7160
查看次数