Bonjour j'ai voulu implémenter l'exemple de Patrice Secheresse et le faire tourner avec un serveur JBOSS . A l'exécution de mon test jUnit j'obtiens l'exception suivante :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
 
3 janv. 2011 12:54:40 demo.ejb3.calculatrice.CalculatriceFacadeTest testAdditionner
GRAVE: null
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
Voici mes beans (tous simples)

calculatrice :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package demo.ejb3.calculatrice;
 
import javax.ejb.Local;
 
@Local
public interface CalculatriceLocal {
 
    int additionner(int x, int y);
 
}
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package demo.ejb3.calculatrice;
 
import javax.ejb.Stateless;
 
 
@Stateless
public class Calculatrice implements CalculatriceLocal {
 
    public int additionner(int x, int y) {
        return x+y;
    }
 
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
 
}
Façade Calculatrice:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package demo.ejb3.calculatrice;
 
import javax.ejb.Remote;
 
 
@Remote
public interface CalculatriceFacadeRemote {
 
      int additionner(int x);
 
}
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package demo.ejb3.calculatrice;
 
import javax.ejb.EJB;
import javax.ejb.Stateful;
 
 
@Stateful
public class CalculatriceFacade implements CalculatriceFacadeRemote {
 
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @EJB
    private CalculatriceLocal calculatriceBean;
 
    int sousTotal=0;
 
    public int additionner(int x) {
        sousTotal = calculatriceBean.additionner(x, sousTotal);
        return sousTotal;
    }
 
 
 
}
Le test jUnit :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
 
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package demo.ejb3.calculatrice;
 
 
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
 
 
public class CalculatriceFacadeTest {
 
    public CalculatriceFacadeTest() {
    }
 
    @BeforeClass
    public static void setUpClass() throws Exception {
    }
 
    @AfterClass
    public static void tearDownClass() throws Exception {
    }
 
    @Before
    public void setUp() {
    }
 
    @After
    public void tearDown() {
    }
 
    /**
     * Test of additionner method, of class CalculatriceFacade.
     */
    @Test
    public void testAdditionner() {
        try {
            System.out.println("additionner");
 
            int x = 0;
            int expResult = 0;
 
            InitialContext ctx;
            System.out.println("additionner");
            ctx = new InitialContext();
            System.out.println("additionner");
 
            System.out.println("class name : "+CalculatriceFacadeRemote.class.getName());
            Object ref = ctx.lookup(CalculatriceFacadeRemote.class.getName());
            System.out.println("additionner");
            CalculatriceFacadeRemote calc = (CalculatriceFacadeRemote) PortableRemoteObject.narrow(ref,CalculatriceFacadeRemote.class);
            System.out.println("additionner");
 
            int result = calc.additionner(x);
            System.out.println("additionner");
            assertEquals(expResult, result);
 
            x=2;
            expResult = 2;
            result = calc.additionner(x);
 
 
            expResult = 4;
            result = calc.additionner(x);
            assertEquals(expResult, result);
 
            // TODO review the generated test code and remove the default call to fail.
            fail("The test case is a prototype.");
        } catch (NamingException ex) {
            Logger.getLogger(CalculatriceFacadeTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 
}
Pouvez-vous m'aider ? merci