gpt4 book ai didi

java - Glassfish 嵌入式 v4.1.1 和 lambda

转载 作者:太空宇宙 更新时间:2023-11-04 12:57:37 26 4
gpt4 key购买 nike

我有一个带有嵌入式 glassfish 服务器的 junit 测试(集成测试)。如果我在无状态 ejb 中使用 lambda,我的测试就会失败。没有 lambda 表达式,我的测试工作正常。

嵌入式 glassfish 服务器有修复版或快照版吗?我还没有找到有关 glassfish-embedded-all 快照版本的任何信息:(

使用的依赖项:

<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>4.1.1</version>
<scope>test</scope>
</dependency>

如果我取消注释行 (1) 或 (2),则我的示例 bean 的引用在 junit 测试中为 null(出现查找失败异常):(

Bean/Ejb:

@Stateless
@LocalBean
public class ExampleBean {

@PersistenceContext
private EntityManager em;

public Person get(Long id) {
Optional<Person> p = Optional.ofNullable(em.find(Person.class, id));

List<Person> persons = new ArrayList<>();
persons.add(p.get());

// (1) properties.forEach(person -> System.out.println(person));
// (2) String name = person.map(Person::getName).orElse(null);
//System.out.println(name");
return p;
}
}

测试类:

public class ExampleBeanTest {
@EJB
private static ExampleBean examplebean;
private static Context context;
private static EJBContainer container;

@BeforeClass
public static void init() {
Map<String, Object> props = new HashMap<String,Object>();
props.put(EJBContainer.MODULES, new File[]{new File("target/test-classes"), new File("target/classes")});

try {
container = EJBContainer.createEJBContainer(props);
context = container.getContext();
examplebean = (ExampleBean) context.lookup("java:global/classes/ExampleBean");
} catch (Exception e) {
e.printStackTrace();
}
}

@AfterClass
public static void tearDown() throws Exception {
if (container != null) {
container.close();
}
}

@Test
public void testGet() throws NamingException {
assertNotNull(examplebean);
Person p = examplebean.get(1);
assertNotNull(p);
assertEquals("text", p.getName());
}
}

是否有任何用于 juint test + java 8 的嵌入式 EE 容器能够执行 java lambda 表达式?

编辑1:我刚刚使用 lambda 和经典迭代创建了另一个测试。具有经典迭代的 Bean 运行良好,但使用 forEach 时出现“查找失败”异常。这是怎么回事?

public Person get(Long id) {
Optional<Person> person = Optional.ofNullable(em.find(Person.class, id));
List<Person> persons = new ArrayList<>();
persons.add(person.get());

long l = persons.stream().count();
System.out.println("length: " + l); // <- ok, result: 1

for (Person p : persons) { // <- ok, one result: "text"
String name = p.getName();
System.out.println("name: " + name);
}

// if i keep the following line in my code i get a lookup failed exception
// if i comment these lines my code works fine
persons.forEach(pp -> {
System.out.println(pp.getName());
});

return person.get();
}

异常(exception):

Feb 05, 2016 10:36:17 PM org.glassfish.ejb.embedded.EJBContainerProviderImpl createEJBContainer
SEVERE: EJB6005:No EJB modules found
javax.naming.NamingException: Lookup failed for 'java:global/classes/ExampleBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: classes]
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:491)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:438)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at a.b.ExampleBeanTest.init(ExampleBeanTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
Caused by: javax.naming.NameNotFoundException: classes
at com.sun.enterprise.naming.impl.TransientContext.resolveContext(TransientContext.java:299)
at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:207)
at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:208)
at com.sun.enterprise.naming.impl.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:66)
at com.sun.enterprise.naming.impl.LocalSerialContextProviderImpl.lookup(LocalSerialContextProviderImpl.java:114)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:478)
... 18 more

编辑2:我可以在 Maven 日志文件中看到 NCLS-DEPLOYMENT-00009 警告消息。仅找到一个相关的论坛主题:Glassfish deploy error “Archive type of MyProject.jar was not recognized” 。这里建议删除 lambda 表达式。

maven日志

Feb 06, 2016 8:49:37 AM org.glassfish.deployment.common.GenericAnnotationDetector scanArchive
WARNING: NCLS-DEPLOYMENT-00009
Feb 06, 2016 8:49:37 AM org.glassfish.ejb.embedded.EJBContainerProviderImpl createEJBContainer
SEVERE: EJB6005:No EJB modules found
javax.naming.NamingException: Lookup failed for 'java:global/classes/ExampleBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: classes]
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:491)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:438)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at a.b.ExampleBeanTest.init(ExampleBeanTest.java:39)
...
Caused by: javax.naming.NameNotFoundException: classes
...

这是一个BUG吗?

是否有任何可用的嵌入式 EE 容器来使用 Java8 + EJB/Bean + Lambda 执行 JUnit/集成测试?

在哪里可以报告 glassfish-embedded-all 模块相关的错误?我只找到了 glassfish 服务器相关的 jira。

最佳答案

几天前 lambda 错误已修复: https://github.com/payara/Payara/issues/647

关于java - Glassfish 嵌入式 v4.1.1 和 lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35230667/

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