gpt4 book ai didi

java - 在与 arquillian 的集成测试中未找到 org.apache.log4j.Logger 类

转载 作者:行者123 更新时间:2023-12-01 21:25:52 24 4
gpt4 key购买 nike

我有以下问题:

环境:

  1. Weblogic 12.1.3
  2. MySQL
  3. Gradle
  4. Log4j

共享库:

-rwxrwxrwx   1 josediaz  staff     0B Jan 19 17:06 Readme.txt
drwxrwxrwx 2 josediaz staff 68B Mar 31 20:48 build
-rwxr-xr-x 1 josediaz staff 1.1K Jun 30 01:05 build.gradle
drwxrwxrwx 9 josediaz staff 306B Jul 1 01:31 dao-ifz.jar
drwxrwxrwx 10 josediaz staff 340B Jul 1 01:31 dao-imp.jar
drwxrwxrwx 5 josediaz staff 170B Mar 31 20:48 dependencias
drwxrwxrwx 9 josediaz staff 306B Jul 1 01:31 model.jar
drwxrwxrwx 9 josediaz staff 306B Jul 1 01:31 service-ifz.jar
drwxrwxrwx 9 josediaz staff 306B Jul 1 01:31 service-imp.jar
-rwxr-xr-x 1 josediaz staff 578B Apr 27 23:02 settings.gradle
drwxrwxrwx 11 josediaz staff 374B Jul 1 01:31 slb.ear
drwxr-xr-x 11 josediaz staff 374B Jul 1 01:31 test.jar

我的共享库的 dao-imp 中有一个 JPAPersonDAO,并且我使用 log4j 进行日志记录:

@Stateless
public class JPAPersonDAO implements PersonDAO {



private static final Logger LOG = Logger.getLogger(JPAPersonDAO.class);

@PersistenceContext(unitName = "persistence-unit")
private EntityManager entityManager;


@SuppressWarnings("unchecked")
@Override
public List<Person> findPersons(int startPosition, int maxResults, String sortFields, String sortDirections) {

LOG.warn("warn");
LOG.info("info");
LOG.trace("trace");
LOG.debug("debug");
LOG.error("error");
LOG.fatal("fatal");



return entityManager
.createNativeQuery("select * from person order by name limit " + startPosition + "," + maxResults,
Person.class)
.getResultList();

}

这是我的 log4j.properties:

log4j.rootLogger=INFO, stdout, file

log4j.debug=true

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d] %-5p %c %x - %m%n

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.File=/Users/josediaz/Projects/logs/logging_sharedlib.log
log4j.appender.file.Append = false
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.MaxFileSize = 20000Kb
log4j.appender.file.MaxBackupIndex = 5
log4j.appender.file.layout.ConversionPattern=[%d] %-5p %c %x - %m%n


log4j.category.com.theserverlabs.blog.wls=DEBUG

当我部署我的sharedlib并且其他EAR使用它时,日志的生成没有问题。

问题

我添加了一个测试项目,其中包含基于 arquillian 的集成测试。

@RunWith(Arquillian.class)
public class NativeSqlTest {


@Inject
PersonDAO dao;

@Deployment
public static WebArchive createDeployment() {
return ShrinkWrap.create(WebArchive.class)
.addClasses(PersonDAO.class, JPAPersonDAO.class, Person.class, Profiled.class,
ProfilingInterceptor.class)
.addAsWebInfResource("test-beans.xml", "beans.xml")
.addAsResource("test-persistence.xml", "META-INF/persistence.xml");
}

/**
* In the test, we're just going to invoke the only available operation in
* the +EmployeeBean+ and assert a few details to confirm that the native
* query was successfully executed.
*/
@Test
public void testNativeSql() {



int result = dao.countPersons();
assertTrue(result > 0);
}
}

无论我从 IDE 执行此测试还是从终端调用 gradle clean build,我都会收到以下错误:

org.jboss.arquillian.container.spi.client.container.DeploymentException: Deployment Failed on server: java.lang.ClassNotFoundException: org.apache.log4j.Logger

我做错了什么?

最佳答案

谢谢Mincong Huang你的建议是根本性的。我将其添加到我的部署 .addPackages(true, "org.apache.log4j") 中并测试通过。

@Deployment
public static WebArchive createDeployment() {
return ShrinkWrap.create(WebArchive.class, "testSharedlib.war")
.addClasses(PersonDAO.class, JPAPersonDAO.class, Person.class, Profiled.class,
ProfilingInterceptor.class)
.addPackages(true, "org.apache.log4j")
.addAsResource("test-persistence.xml", "META-INF/persistence.xml")
.addAsResource("log4j.xml")
.addAsWebInfResource("test-beans.xml", "beans.xml");

}

谢谢。

关于java - 在与 arquillian 的集成测试中未找到 org.apache.log4j.Logger 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38150361/

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