2017-01-27 28 views
0

JavaEEアプリケーションをWeblogic 10.3.6からWeblogic 12.2.1.2に移行中です。この移行の一環として、標準のJSFアノテーションではなく、CDIアノテーションを使用するようにJSFマネージドBeanを変更しています。 @ManagedBean@Namedおよびjavax.faces.bean.ViewScopedjavax.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我々はテストで帽子同様の困難は、豆を@ViewScoped

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

を:私は取得

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

:彼はに@Deployment。私たちは、テスト自身の中に注入されたBeanを作成することでこれを解決しました。

テストで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

のように見えるで

@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); 
} 
} 

entitymangerなど、豆で動作します。私は元WebBeans context with scope type annotation @ViewScoped does not exist within current threadから来るorg.apache.webbeans.container.BeanManagerImplの源を見つけました。私はテストソースの中でこのクラスを作成し、問題を回避するためにいくつかの変更を加えました。

最終的に私のテストのために、私は範囲を気にしません。私はメソッドが実行し、正しいロジック/データを返すことをテストしています。したがって、クラスでは、Beanのスコープのタイプをチェックして例外をスローします。私はそれがViewscopedであるかどうかを単にチェックし、もしそうならばそれをDependentに変更しました。これにより、私のテストは機能しました。

最高の解決策ではありませんが、機能します。

関連する問題