今天我遇到了一个不寻常的情况..为了测试Java,我将使用glassfish-embedded all。在生产环境中,我将使用apache derby数据库。因此,当我编写用于测试DB的小测试类时,我得到了这个错误:
java.lang.SecurityException: sealing violation: can't seal package org.apache.derby.impl.services.locks: already loaded
我的pom:
<dependencies>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.14.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbynet</artifactId>
<version>10.14.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.14.2.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>问题是嵌入式服务器也有derby类。所以我有用于编译的dervy类和用于测试的derby类。并且在测试过程中产生了冲突,我不知道该如何解决。
在测试期间,我似乎需要忽略嵌入式服务器中的derиy类。有什么想法吗?
我的测试:
public class JpaTest {
private static final String PERSISTENCE_UNIT_NAME = "people";
private EntityManagerFactory factory;
@Before
public void setUp() throws Exception {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
// Error appear here
EntityManager em = factory.createEntityManager();附言:我不明白Maven阴影插件是如何工作的。这个插件似乎是针对final jar的。现在不是单元测试...
P.S.2。如果我尝试在一些Main方法中执行测试,它工作得很好(因为没有glassfish依赖)
发布于 2020-02-03 20:54:38
如果我没记错的话,maven编译作用域用于生命周期的每个阶段,因此带有编译作用域的依赖项也用于测试阶段。该解决方案声称从glassfish中排除了derby依赖项,以强制使用您的依赖项。
更重要的是,您具有显式作用域定义为compile的依赖项,而除了nothing is specified...theese之外的其他依赖项都是相同的,因为maven将其作为默认作用域compile
https://stackoverflow.com/questions/60039497
复制相似问题