JPA + EJB testing with embedded glassfish v3 - jpa-2.0

Trying to set up a JPA+EJB testing after these instructions:
http://ctpjava.blogspot.fi/2009/10/unit-testing-ejbs-and-jpa-with.html
There seems to be few problems that I can't seem to get quite right.
First I get this error (which I am able to work around, but it still needs to be fixed):
SEVERE: EJB6004:Specified application server installation location [C:\Users\<userName>\.m2\repository\org\glassfish\extras\glassfish-embedded-all\domains\domain1] does not exist.
Found this site, which gives the (more correct?) properties to set:
http://docs.oracle.com/cd/E18930_01/html/821-2424/gjlde.html
And modified my testing setup to this:
import javax.ejb.embeddable.EJBContainer;
import javax.naming.Context;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
...
#BeforeClass
public static void createContainer() {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(EJBContainer.MODULES, new File("target/classes"));
properties.put("installation.root", "./src/test/glassfish");
properties.put("configuration.file", "./src/test/glassfish/domains/domain1/config/domain.xml");
container = EJBContainer.createEJBContainer(properties);
ctx = container.getContext();
}
And in pom.xml I have the following that seems to be required for this:
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.9.1.0</version>
<scope>test</scope>
</dependency>
<!-- Must be before java-ee -->
<dependency>
<groupId>org.glassfish.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
The error pointed into user directory and I was able to get pass this by setting up the required stuff there (and got past this), but that is not a proper place, since everything must be available via SVN.
According to the latter link I think I have the correct properties set, but those seem to be ignored. Maybe I am missing something obvious?

Missed this text in the properties link:
Properties that can be passed to the
EJBContainer#createEJBContainer(Properties) method are summarized in
the following table. All properties are in the
org.glassfish.ejb.embedded.glassfish package. For example, the full
name of the installation.root property is
org.glassfish.ejb.embedded.glassfish.installation.root.
So the answer was to have:
org.glassfish.ejb.embedded.glassfish.
Before each property
Thus:
org.glassfish.ejb.embedded.glassfish.installation.root
intead of:
installation.root

Related

Quarkus ignore dependencies for test

In my quarkus app, I have repository implementations in a separate gradle project/module, partly because I want to be able to ensure unit tests don't use the database etc.
The trouble is, if I want to use any injected dependencies in test, I need to use #QuarkusTest, and then the build or startup ensures that all dependencies are satisfied.
Is there any way around this that doesn't involve mocking every single external dependency, e.g.?
Don't enforce dependencies during build or startup when running tests, and instead leave it to error during runtime if accessed.
Some way of mocking all unavailable dependencies (or a subset of them) with a mock that errors on access.
I am using weld-junit to run unit tests on CDI components. In the past I have been using cdiunit too which is great, but it does not support JUnit 5. The idea of both is to launch the tests inside a real CDI container, in a minimal configuration. The important part is that you control explicitly what beans are available for injection, so the system skips the bean discovery part. This makes the tests fairly fast: the CDI overhead is only a few seconds. Both systems allow you to provide mock instances for any dependencies you want - I have been using Mockito for managing the mocks.
The minimal Maven setup (see here is to use a version of the Maven surefire plugin that enables JUnit 5, e.g.:
<pluginManagement>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<plugin>
</pluginManagement>
And the other dependencies:
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-junit5</artifactId>
<version>${version.weld-junit5}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${version.junit.jupiter}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${version.junit.jupiter}</version>
<scope>test</scope>
</dependency>
<!-- If you want Mockito: -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${version.mockito}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${version.mockito}</version>
<scope>test</scope>
</dependency>
Having these in place, you can write & run a test (an example) for your CDI beans (example):
// weld-junit5 annotation - there are other ways to activate weld-junit5
// in your tests, and more features for controlling the container,
// read the full docs!
#EnableAutoWeld
// add real bean implementations in the DI container
#AddBeanClasses(RealBeanDependency.class)
// add extensions if you need
#AddExtensions({Extension1.class, Extension2.class})
// activate scopes, if you need
#ActivateScopes(RequestScoped.class)
// add automatic creation of mocks with Mockito (#Mock)
#ExtendWith(MockitoExtension.class)
public class MyClassTest {
// an example of introducing mocks to the DI container
#Produces #Mock
private ArticleService articleService;
// injected the "System Under Test"
// weld-junit will automatically add it to the available beans
#Inject
private MyClass sut;
#Test
void test() {
// this runs inside the minimal CDI container
// read the docs about details, e.g. it starts
// a fresh container for each test (I think)
}
}

MessageReceiver class missing

I could not find the class ProjectSubscriptionName/MessageReceiver and eclipse reporting ProjectSubscriptionName/MessageReceiver class can not resolved error tough the related google-cloud-pubsub jar imported to build path by defining in pom.xml file.
pom.xml:
<dependency>
<groupId>com.google.cloud.dataflow</groupId>
<artifactId>google-cloud-dataflow-java-sdk-all</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
<version>0.8.0</version>
</dependency>
Need help here to find the correct jar for missing classes. Appreciate your help. Jar should be stable one not alpha/beta version.
Hi Kamal,
Google released pubsub 1.31.0 version yesterday and pom entry below:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
<version>1.31.0</version>
</dependency>
Link:
https://mvnrepository.com/artifact/com.google.cloud/google-cloud-pubsub/1.31.0
The ProjectSubscriptionName and MessageReceiver are only defined in later versions of the client library that were in beta until version 1.31.0 was released. The newer client libraries are designed to make it easier to handle high throughput and low latency. The MessageReceiver interface first appeared in version 0.9.0-alpha. The ProjectSubscriptionName class first appeared in 0.36.0-beta.

Where can I find the HttpTester class in Jetty 9.3.1?

In the Jetty 9.3.1 documentation the Class HttpTester is referenced, however it doesn't appear to be in any of the jar files provided in the download package.
HttpTester.Request request = HttpTester.newRequest();
request.setURI("/some/resource");
HttpTester.Response response =
HttpTester.parseResponse(HttpTester.from(localConnector.getResponse(request.generate())));
I want to use HttpTester.Response and HttpTester.Request in my unit test cases.
Was this class removed in 9.3.1? If so, is there a recommended alternative?
It was moved to the tests classified artifacts.
Try this (in maven-speak):
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http</artifactId>
<version>9.3.11.v20160721</version>
<classifier>tests</classifier>
</dependency>

Webservice issue in cq5 - [A WebService annotation is not present on class]

I was working on SOAP JAX-WS with CQ5. I was trying the example available on link "_http://cqblog.inside-solutions.ch/2013/11/01/consuming-soap-web-service-with-jax-ws-in-cq5-6-1/". I used the command "wsimport -keep -verbose http://www.w3schools.com/webservices/tempconvert.asmx?wsdl" to generate the stubs. When i am calling this below line in my WebServiceClient class, i am getting error as "A WebService annotation is not present on class: com.mercer.jaxws.TempConvertSoap". TempConvertSoap is a proxy class.
*line :-
return super.getPort(new QName("_http://www.w3schools.com/webservices/", "TempConvertSoap"), TempConvertSoap.class);
i am adding the below dependencies to resolve the required jars in maven pom.xml :-
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-ws-metadata_2.0_spec</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<version>2.2.8</version>
</dependency>
Please provide pointers on the same.
Thanks,
Kiran Parab

entity manager doesn't have method getCriteriaBuilder()

I am introducing JPA2.0 in my application working on Oracle9i database and I added the libraries EclipseLink(JPA2.0) and created the entity classes but when I use
javax.persistence.criteria.CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
I get the following error
cannot find symbol
symbol : method getCriteriaBuilder()
location: interface javax.persistence.EntityManager
my web.xml is version 2.4 and here's my persistence.xml
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="MiraclinPU" transaction-type="JTA">
<jta-data-source>jdbc/Miraclin</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
It looks like the app is using JPA1.0 as I read on the forums...Can anyone help?
Use:
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
And not:
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
Search in all libraries that are included in your project, and remove the ones containing persistence.xml except the one you need. Then reinclude them again.
In eclipse:
Project -> Properties -> Java Build Path -> Configure Build Path -> Libraries
remove references to javax/persistente
insert a reference to hibernate-jpa-2.0-api-/version/.jar
how to verify:
in your sourceDAO.java press F3 in EntityManagerFactory and look what .jar eclipse open.
Missing
import javax.persistence.criteria.*;
possibly?
Due to org.hibernate.orm artifact was moved to hibernate-core;
I removed this from pom.xml:
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.9.Final</version>
</dependency>
and added this:
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.9.Final</version>
</dependency>
That was solved the cannot find symbol getCriteriaBuilder() problem.