gpt4 book ai didi

java - Spring 测试因 NoClassDefFoundError 失败

转载 作者:行者123 更新时间:2023-12-02 08:59:58 24 4
gpt4 key购买 nike

我有一个配置类

@Configuration
@EnableTransactionManagement
@EnableSpringDataWebSupport
public class WebConfig {

@Bean
public RequestContextListener requestContextListener() {
return new RequestContextListener();
}

@Bean
public DataSource dataSource() {

EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
return builder.setType(EmbeddedDatabaseType.H2).build();
}

@Bean
public EntityManagerFactory entityManagerFactory() {

HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
vendorAdapter.setShowSql(true);

LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("mypackage");
factory.setDataSource(dataSource());
factory.afterPropertiesSet();

return factory.getObject();
}

}

我有一个测试类

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class UserServiceTest {


@Autowired
private MyRepository myRepository;


@Before
public void initDB(){

MyObj my = new MyObj();
my.setValue(1000L);

myRepository.save(my);
}

@Test
public void test1() {
assertEquals(4, userMoneyRepository.findAll());
}

}

我的错误是

Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
at org.hibernate.boot.spi.XmlMappingBinderAccess.<init>(XmlMappingBinderAccess.java:43)
at org.hibernate.boot.MetadataSources.getXmlMappingBinderAccess(MetadataSources.java:98)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.prepare(MetadataBuildingProcess.java:101)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:242)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:168)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:52)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:378)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341)
at config.WebConfig.entityManagerFactory(WebConfig.java:48)
at carx.config.WebConfig$$EnhancerBySpringCGLIB$$2f198977.CGLIB$entityManagerFactory$0(<generated>)
at carx.config.WebConfig$$EnhancerBySpringCGLIB$$2f198977$$FastClassBySpringCGLIB$$d53a2482.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)
at config.WebConfig$$EnhancerBySpringCGLIB$$2f198977.entityManagerFactory(<generated>)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 44 more
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 65 more

config.WebConfig.entityManagerFactory(WebConfig.java:48)我有factory.afterPropertiesSet();我应该怎么办?当我使用 Spring boot 启动应用程序时,我使用此配置并且它有效。当我的应用程序运行时,我有运行时数据库。我想使用这个配置进行测试。

最佳答案

如果您使用 Gradle,请添加以下内容:

compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.0'

如果您使用 Maven,请添加以下内容:

<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>

关于java - Spring 测试因 NoClassDefFoundError 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60240509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com