2017-01-27 12 views
0

JavaEE 응용 프로그램을 Weblogic 10.3.6에서 Weblogic 12.2.1.2로 마이그레이션하는 중입니다. 이 마이그레이션의 일부로 표준 JSF 주석이 아닌 CDI 주석을 사용하도록 JSF Managed Bean을 변경하고 있습니다. @ManagedBean ~ @Namedjavax.faces.bean.ViewScoped ~ javax.faces.view.ViewScoped. 이것은 사소한 문제만으로도 성공적으로 입증되었습니다. 그러나 나는 우리의 테스트를 실행하려고 큰 문제가 있습니다. 테스트는 다음 오류와 함께 실패합니다.Arquillian CDI - CDI 범위 문제로 JSF 테스트

WebBeans context with scope type annotation @ViewScoped does not exist within current thread 

여러 다른 컨테이너 (포함 및 원격)를 시도했지만 여전히이 같은 오류가 발생합니다. 어떤 도움이라도 대단히 감사 할 것입니다.

나는 다음의 pom.xml 종속 Arquillian을 사용하고 있습니다 : TestBean

@RunWith(Arquillian.class) 
public class TestAgain { 

    @Deployment 
    public static JavaArchive createDeployment() { 
     return ShrinkWrap.create(JavaArchive.class, "test.jar") 
       .addClass(AnotherBean.class) 
       .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); 
    } 

    @Inject 
    AnotherBean anotherBean; 

    @Test 
    public void doTest() 
    { 
     Assert.assertEquals(anotherBean.doTest(), "test"); 
     anotherBean.doTest(); 
    } 
} 

UPDATE

내가 변경하는 경우 t

import javax.faces.view.ViewScoped; 
import javax.inject.Named; 
import java.io.Serializable; 

@Named 
@ViewScoped 
public class AnotherBean implements Serializable { 

    public String doTest() 
    { 
     System.out.println("test"); 
     return "test"; 
    } 
} 

:

<dependencyManagement> 
    <dependencies> 
     <dependency> 
      <groupId>org.jboss.arquillian</groupId> 
      <artifactId>arquillian-bom</artifactId> 
      <version>1.1.12.Final</version> 
      <scope>import</scope> 
      <type>pom</type> 
     </dependency> 
    </dependencies> 
</dependencyManagement> 


<dependencies> 
    <dependency> 
     <groupId>javax</groupId> 
     <artifactId>javaee-api</artifactId> 
     <version>7.0</version> 
     <scope>provided</scope> 
    </dependency> 

    <dependency> 
     <groupId>org.apache.tomee</groupId> 
     <artifactId>arquillian-openejb-embedded</artifactId> 
     <version>7.0.2</version> 
     <scope>test</scope> 
    </dependency> 

    <dependency> 
     <groupId>org.jboss.arquillian.junit</groupId> 
     <artifactId>arquillian-junit-container</artifactId> 
     <scope>test</scope> 
    </dependency> 

    <dependency> 
     <groupId>javax.faces</groupId> 
     <artifactId>javax.faces-api</artifactId> 
     <version>2.2</version> 
     <scope>provided</scope> 
    </dependency> 

    <dependency> 
     <groupId>org.primefaces</groupId> 
     <artifactId>primefaces</artifactId> 
     <version>6.0.13</version> 
    </dependency> 

    <dependency> 
     <groupId>org.primefaces.themes</groupId> 
     <artifactId>all-themes</artifactId> 
     <version>1.0.10</version> 
    </dependency> 

    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.12</version> 
     <scope>test</scope> 
    </dependency> 

</dependencies> 

BackingBean 내가 가져

@Deployment 
    public static WebArchive createDeployment() { 
     return ShrinkWrap.create(WebArchive.class, "test.jar") 
       .addClass(AnotherBean.class) 
       .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); 
    } 

: 테스트에서

javax.enterprise.inject.UnsatisfiedResolutionException: Api type [AnotherBean] is not found with the qualifiers 
Qualifiers: [@javax.enterprise.inject.Default()] 
for injection into Field Injection Point, field name : anotherBean, Bean Owner : [null] 

답변

0

우리 모자 비슷한 어려움 콩 @ViewScoped 그는에 @Deployment. 우리는 테스트를 통해 주입을 통해 콩을 생성함으로써이를 해결했습니다.

Bean 인스턴스 자체가 테스트에서 생성되고 모든 종속성이 리플렉션을 사용하여 삽입됩니다. 이것은, 콩과 함께 작동 TestHelper 내가 좋은 구식 해킹으로이 문제를 해결했다 결국이

public class TestHelper { 

public static void inject(Object bean, 
          String fieldName, 
          Object fieldValue) throws Exception { 
    if (null == bean) { 
    throw new IllegalArgumentException("Bean must not be null"); 
    } 
    Field field; 
    try { 
    field = bean.getClass().getDeclaredField(fieldName); 
    } catch (NoSuchFieldException e) { 
    log.log(Level.SEVERE, "Could not find field for injection: " + fieldName); 
    throw e; 
    } 
    field.setAccessible(true); 
    field.set(bean, fieldValue); 
} 
} 
0

처럼 보이는와 entitymanger 등

@RunWith(Arquillian.class) 
public class ViewControllerTest { 
@Inject private OtherBean otherBean; 

private ViewController viewController; 


@Deployment 
public static WebArchive createDeployment() { 
    return WebArchiveFactory.getDefaultWebarchArchive(); 
} 

@Before 
public void setup() throws Exception { 
    viewController = new ViewController(); 
    TestHelper.setFacesContext(); // provide FacesContextMock 
    TestHelper.inject(viewController, "otherBean", otherBean); 
} 
} 

. 나는 원래 WebBeans context with scope type annotation @ViewScoped does not exist within current thread에서 오는 org.apache.webbeans.container.BeanManagerImpl에 대한 소스를 발견했다. 테스트 소스에이 클래스를 만든 다음 문제를 해결하기 위해 몇 가지 변경 작업을 수행했습니다.

궁극적으로 내 테스트에서는 범위에 대해 신경 쓰지 않습니다. 메서드가 실행되고 올바른 논리/데이터를 반환하는지 테스트하고 있습니다. 따라서 클래스에서는 빈이 어떤 유형의 범위에 있는지 검사하고 예외를 throw합니다. 나는 그것이 그것이 Viewscoped에 있는지를 간단히 점검했고, 그렇다면 Dependent으로 바꿨다. 이것은 그 때 나의 시험이 작동하는 것을 허용했다.

최상의 해결책은 아니지만 작동합니다.