Spring Controller Testing. Can't seem to find MockMvcRequestBuilders - unit-testing

I'm trying to write some Unit and Integration tests for my Spring Controllers following this guide and Spring's documentation for testing MVC controllers
The problem is that I'm unable to find the appropriate includes in mvnrepository for the following piece of code
this.mockMvc.perform(get("/foo").accept("application/json"))
.andExpect(status().isOk())
.andExpect(content().mimeType("application/json"));
I'm unable to find the jar for get("/foo) method and .mimeType(....).
Upon googling, I was however able to find out the source for the above get and mimeType at here.
So, should I just copy paste these helper classes from this Spring Test showcase project? or am I missing something here?

Looks like the package name changed from test.web.server to test.web.servlet in spring-test and the blog articles/docs are out of date for Spring 4.

I'm assuming you're using Eclipse IDE. Unfortunately it doesn't automatically import static imports.
You have to add these through: Window > Preferences > Java > Editor > Content Assist > Favorites.
Here's a good post with more information:
http://piotrnowicki.com/2012/04/content-assist-with-static-imports-in-eclipse/

do you have
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
somewhere in your pom.xml?

Related

How to skip resolving dependencies of a specific configuration in Ivy?

I have the following problem:
My project (C++) has a dependency to a system library. For that purpose I define in the ivy.xml file a new section within dependencies like this:
<ivy-module version="2.0">
<info organisation="org.acme" module="mymodule">
<configurations>
<conf name="system"/>
</configurations>
[...]
<dependencies>
<dependency name="pthread" rev="*" conf="system"/>
</dependencies>
</ivy>
The problem I am facing is that Ivy tries to resolve the dependency to pthread before publishing mymodule.
The command use for publishing is the following:
java -jar ivy.jar -debug -ivy ivy.xml -publish publish -publishpattern "<ivy-pattern>" -status integration -revision 0.0.1-SNAPSHOT -settings ivysettings.xml -overwrite
I checked the Ivy documentation but cannot find any hint on how to tell Ivy to skip resolving dependencies for a specific configuration.
Has one of you guys maybe a hint or an idea how to configure that?
Thanks a lot in advance.
UPDATE:
I managed to exclude the dependency for a specific configuration by using the following dependencies tag:
<dependencies>
<dependency name="pthread" rev="*" conf="system"/>
<exclude conf="system"/>
</dependencies>
In order to do this you must provide ogranisation, module and revision which is needed for publish task. There reason is that if this is not provided publish will look for it in last resolved module (check the publish task for the attributes).
So when you call publish task, it will require either providing all of these attributes, or call to resolve task beforehand. The attributes that are needed are:
pubrevision
artifactspattern
organisation
module
revision
srcivypattern
You can check the link to see which one is what, even thought it's pretty self explanatory.
Now I don't know how(or if at all) you cann pass all these paramaters when calling ivy as a jar. Maybe use ivyfile as listed here. That's why I gave the answer on how to do it with ant task - well that and because it's usually cleaner and easier to use ivy with ant :)

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>

How can I use different JARs for compiling and testing in maven?

I compile my programm against javaee-api. But for Junit testing I must use a specific implementation like glassfish's javaee.jar to avoid errors like java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/persistence/Persistence (see also 1).
So avoid using methods, that are only available in glassfish implementation, I want to compile my artifact with the general api, but run junit with the implementation jar. But both provide equal named classes and inferfaces, so the class loader gets in trouble.
What is the best way to solve this problem? Can I solve this problem with maven?
Thanks a lot
I think that this is possible. Actually, starting with version 2.0.9, Maven uses the POM order to build the classpath, so you can manipulate it now. And if you combine this with Dependency Scope, it should be possible to achieve what you want. In practical terms, if you place GlassFish's javaee dependency (with a test scope) before the javaee-api dependency, the former should be placed before the later in the test classpath and thus used by unit tests while the later will be used during compile. In theory, this should work but it is kinda fragile so it needs to be carefully documented.
Something like that (with a fictional GFv3 jar):
<dependencies>
<dependency><!-- this one will be first on the test classpath -->
<groupId>org.glassfish</groupId>
<artifactId>javaee</artifactId>
<version>6.0</version>
<scope>test</scope>
<dependency>
<dependency><!-- this one will be used during compile -->
<groupId>javax.javaee-api</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
<dependency>
...
</dependencies>

OC4J Problem: global-web-application.xml Works, orion-web.xml Doesn't

I recently "solved" a somewhat common problem in OC4J regarding the use of Xerces rather than OC4J's built-in parser. The problem was solved by adding this line to global-web-application.xml:
<web-app-class-loader search-local-classes-first="true"/>
Unfortunately, this was too much of a big-hammer approach that could cause problems on the app server, so I tried to resolve it via creating the following orion-web.xml file in the WEB-INF directory of the app:
<?xml version="1.0"?>
<orion-web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://xmlns.oracle.com/oracleas/schema/orion-web-10_0.xsd">
<web-app-class-loader search-local-classes-first="true" include-war-manifest-class-path="true"/>
<web-app/>
</orion-web-app>
Unfortunately, it turns out that using global-web-application.xml worked, using orion-web.xml didn't
OC4J version is 10.1.3.5.
Can anyone advise?
Another way is specify at Deploy Time. Take a look:
Specifying search-local-classes-first at Deployment Time
The following example illustrates how to set the search-local-classes-first attribute in the orion-web.xml file generated for the Web module at deployment time, with Application Server Control.
Select Applications>Deploy to launch the Application Server Control deployment wizard.
Supply the path to the application in the first page of the wizard.
Specify the application name and supply any context URI mappings in the second page.
Click Configure Class Loading in the third page of the wizard (Deploy: Deployment Settings).
Under Configure Web Module Class Loaders, check the Search Local Classes First checkbox next to the name of the Web module containing the local JAR file to use.
Optionally click the Save Deployment Plan button, and save the plan for reuse.
I think you could solve this in a different manner. Using properties, and removing libraries.
There are two properties:
xml.driver.property
xml.driver.impl
One define the parser interface and the other the implementation. You can switch this from one implementation from another.
for example we have:
xml.driver.property=org.xml.sax.driver
xml.driver.impl=org.apache.xerces.parsers.SAXParser
As this is a system properties you can load it in many different ways. We use a special servlet installed in all OC4J instances (containers) that loads this an other properties at runtime.
A "little" bit late but hope it helps.
Finally I'm abble run JAXB2 (used by Spring WS 2.1.4) on my OC4J 10.13 (and 10.13.50). JAXB needs xalan lib.
orion-web.xml
<?xml version="1.0"?>
<!DOCTYPE orion-web-app PUBLIC "-//Evermind//DTD Orion Web Application 2.3//EN"
"http://xmlns.oracle.com/ias/dtds/orion-web.dtd">
<orion-web-app
persistence-path=""
jsp-cache-directory="./persistence"
jsp-cache-tlds="standard"
simple-jsp-mapping="false"
temporary-directory="./temp"
servlet-webdir="/servlet/"
>
<web-app-class-loader search-local-classes-first="true"/>
</orion-web-app>
maven pom.xml
<!-- JAXB implemetation by EclipseLink MOXy-->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.1</version>
<scope>compile</scope>
</dependency>
<!-- Specific dependencies for OC4J v1013 -->
<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
<version>2.7.1</version>
<scope>runtime</scope>
<exclusions>
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
</exclusions>
</dependency>

What jetty jar should I use?

I'd like to create an application using the embedded version of Jetty. Unfortunately, I can't find any information on what jar files I would need to do that. There are several in the maven repository (http://repo2.maven.org/maven2/org/eclipse/jetty/aggregate/). But what's the difference between jetty-server, jetty-server-all, and jetty-webapp? Are any of these what I want for the embedded use case?
I stopped getting compile errors against the Eclipse embedded code minimal example combining SimplestServer and HelloWorldHandler...
http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty
To achieve this I had to include the following from the lib directory in the unzipped distribution from eclipse's jetty mirror...
jetty-server-7.1.4xxxx.jar
jetty-util-7.1.4xxxx.jar
servlet-api.2.5.jar
This document lists the JAR files required for embedding and is pretty complete.
http://docs.codehaus.org/display/JETTY/Embedding+Jetty
I believe you won't need the Ant jar file unless you're invoking Jetty from Ant, even though it says you need it.
Some of the JSP jar files are named differently in the binary bundle than that document calls for, but this document helps figure out which Jetty JSP jars to use:
http://docs.codehaus.org/display/JETTY/JSP+2.0+v+JSP+2.1
I used jetty-webapp.
All the dependencies are very best explained in this diagram : http://wiki.eclipse.org/Jetty/Reference/Dependencies
Based on the diagram,for embedded use case, a minimum of 6 jars are required.
E.g for Jetty 8, try:
jetty-continuation-8..jar
jetty-http-8..jar
jetty-io-8..jar
jetty-server-8..jar
jetty-util-8.*.jar
servlet-api-3.0.jar
For completeness, the xml for jetty-webapp is;
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>8.1.2.v20120308</version>
</dependency>