Testing an EJB with JUnit

The accepted answer requires mocking a lot of code, including the persistence layer. Use an embedded container to test the actual beans, instead; otherwise, mocking the persistence layer results in code that barely tests anything useful. Use a session bean with an entity manager that references a persistence unit: @Stateless public class CommentService { @PersistenceContext(unitName … Read more

Initialization of List in a JSF Managed bean

If it’s a managed bean as you say, you should do this in a method annotated with @PostConstruct public class Person { private List<String> friends; @PostConstruct public void init(){ friends = new ArrayList<String>(); } //getter and setter… } The practice of doing any initialization in the getter and setter is generally frowned upon within the … Read more

A message body writer for Java type, class myPackage.B, and MIME media type, application/octet-stream, was not found

In your client code you are not specifying the content type of the data you are sending – so Jersey is not able to locate the right MessageBodyWritter to serialize the b1 object. Modify the last line of your main method as follows: ClientResponse response = resource.type(MediaType.APPLICATION_XML).put(ClientResponse.class, b1); And add @XmlRootElement annotation to class B … Read more

Use Enum type as a value parameter for @RolesAllowed-Annotation

How about this? public enum RoleType { STUDENT(Names.STUDENT), TEACHER(Names.TEACHER), DEANERY(Names.DEANERY); public class Names{ public static final String STUDENT = “Student”; public static final String TEACHER = “Teacher”; public static final String DEANERY = “Deanery”; } private final String label; private RoleType(String label) { this.label = label; } public String toString() { return this.label; } } … Read more

what is the right path to refer a jar file in jpa persistence.xml in a web app?

Taking a look at jsr always works! 8.2.1.6.3 Jar Files One or more JAR files may be specified using the jar-file elements instead of, or in addition to the mapping files specified in the mapping-file elements. If specified, these JAR files will >be searched for managed persistence classes, and any mapping metadata annotations found on … Read more

Java EE 6 @javax.annotation.ManagedBean vs. @javax.inject.Named vs. @javax.faces.ManagedBean

First of all let me do some clarifications: Managed bean definition : generally a managed bean is an object that its life cycle (construction, destruction, etc) is managed by a container. In Java ee we have many containers that manage life cycle of their objects, like JSF container, EJB container, CDI container, Servlet container, etc. … Read more