#MicronautTest does not start the embedded server - testing

I am writing a Spock test for controllers in an app using Micronaut. When using #MicronautTest(application=Application), it throws exception with message #MicronautTest used on test but no bean definition for the test present..
On examining the code, I see the following 2 reasons why Micronaut throws this exception. From io.micronaut.test.extensions.spock.MicronautSpockExtension :
if (this.specDefinition == null) {
if (!this.isTestSuiteBeanPresent((Class)spec.getReflection())) {
throw new InvalidSpecException("#MicronautTest used on test but no bean definition for the test present. This error indicates a misconfigured build or IDE. Please add the 'micronaut-inject-java' annotation processor to your test processor path (for Java this is the testAnnotationProcessor scope, for Kotlin kaptTest and for Groovy testCompile). See the documentation for reference: https://micronaut-projects.github.io/micronaut-test/latest/guide/");
}
...
}
My POM configuration is:
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<goals>
<goal>addTestSources</goal>
<goal>addSources</goal>
<goal>compileTests</goal>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>UTF-8</encoding>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject-java</artifactId>
<version>${micronaut.version}</version>
</path>
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-validation</artifactId>
<version>${micronaut.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
<executions>
<execution>
<id>test-compile</id>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject-java</artifactId>
<version>${micronaut.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</execution>
</executions>
</plugin>
If I do not define the annotation the test #MicronautTest, it seems that the application doesn't even start.
Below is the spec code:
#MicronautTest(application= Application)
#PropertySource(value = [
#Property(name='spec.name', value = 'EndpointSpec'),
#Property(name = 'endpoints.health.details-visible', value = 'ANONYMOUS'),
#Property(name = MongoSettings.EMBEDDED, value = 'true'),
])
class EndpointSpec extends Specification {
#Inject
EmbeddedServer embeddedServer
#Inject
ApplicationContext applicationContext
#Unroll
def "test health endpoint is working"() {
given: 'a RxHttpClient'
URL server = embeddedServer.getURL()
RxHttpClient client = new DefaultHttpClient(server, new DefaultHttpClientConfiguration(), '/management')
when: '/health is called'
HttpResponse response = client.toBlocking().exchange('/health')
then: 'response is 200 OK and contains valid headers'
response.status == HttpStatus.OK
response.headers.size() == 5
response.headers.contains('uber-trace-id')
response.headers.contains('Date')
response.headers.contains('content-type') && response.headers.get('content-type') == MediaType.APPLICATION_JSON
response.headers.contains('content-length')
response.headers.contains('connection')
//and: 'response contains valid HealthResult'
//HealthResult healthResult = response.body()
// Want to validate the health result here but nothing is present in body
}
}
How I can either defined the specDefinition value or mark the test in such a way that it is present as a bean definition and what is the reason for such a behavior. Any help would be greatly appreciated.

Micronaut-test makes the tests themselves beans. In order for a Groovy test to be a bean, you need to have micronaut-inject-groovy on the compilation path for test.

Yes, adding the micronaut-inject-groovy to your maven build will resolve the issue. Add the following.
<dependency>
<groupId>io.micronaut.test</groupId>
<artifactId>micronaut-test-spock</artifactId>
<scope>test</scope>
</dependency>

Related

I have a problem with packing my maven selenium project in a standalone jar-file. There is no cp for testclasses despite being in jar file

I try to pack my maven selenium autotest project into a standalone jar. I use the next main class to start test classes:
public static void main(String[] args) {
InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile(new File("pom.xml"));
request.setGoals(Collections.singletonList("install"));
Invoker invoker = new DefaultInvoker();
invoker.setMavenHome(new File(System.getenv("MAVEN_HOME")));
try {
invoker.execute(request);
} catch (MavenInvocationException e) {
e.printStackTrace();
}
}
I have no problem with this, the tests are perfectly running from the mainclass. The next step is to pack the project into a jar-file. So I use maven-jar-plugin and maven-assembly-plugin for that purpose:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>common.MainClass</mainClass>
</manifest>
</archive>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/test/resources/assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
and my assembly:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>test-jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<!-- we're creating the test-jar as an attachement -->
<useProjectAttachments>true</useProjectAttachments>
<unpack>true</unpack>
<scope>test</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.directory}/test-classes</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.class</include>
</includes>
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
</fileSets>
</assembly>
I manage to pack both common and test classes in a jar-file. I manually put the pom file and the testing file in the same directory as the jar file, so I have no trouble with starting the project. But the tests don't run, because the test-classes are not found, despite being inside the project.
I will be very appreciative if anyone gives me a clue about what I do wrong.
P.S. I put here a refer to my project: https://bitbucket.org/Max1203/donatiton-script.git
Test class not found may be misconfiguration
Can you reimport maven dependencies it will solve
Maven cycle-Reload All maven projects
In case anybody needs the answer. Just change the code in MainClass to the next one:
TestNG testNG = new TestNG();
testNG.setTestSuites(Arrays.asList("testng.xml"));
testNG.setPreserveOrder(true);
testNG.run();
It is gonna solve the problem.

Unable to resolve class - Groovy junit testing using maven

I am trying to create groovy tests where i use other methods from other Groovy scripts. Whenever i run mvn test i get unable to resolve class error, referring to the illegal import from another groovy script.
This is the testclass that I've written :
import jenkins_methods.gitMethods
import org.junit.Test
class GitMethodsTest extends GroovyTestCase{
GitMethodsTest(){
}
def gitMethods = new gitMethods()
#Test
void testGetDvhBranches(){
try{
gitMethods.getDVH_branches()
}
catch(Exception e){
println e
}
}
}
gitMethods.groovy
package jenkins_methods
// src/jobMethods
import dvh.util.AnsiColorDefinition
import jenkins_methods.fileMethods
String getGitDVH_BranchPath_flash() { return '/git-repository1/git/dvh/branches/' }
String dvhBranchNull() { return 'feature/DV-null-repo' }
String getDVH_branches() {
gitURL_DVH= "censored_url"
String response = sh returnStdout: true, script: "git ls-remote -h ${gitURL_DVH()}"
def branches = response.replaceAll(/[a-z0-9]*\trefs\/heads\//, ',')
branches = branches.substring(1, branches.length()).replaceAll("\n", "").replaceAll("\r", "")
return branches
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>jenkins-jobs</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<configuration>
<providerSelection>2.0</providerSelection>
</configuration>
<goals>
<goal>addSource</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
The jenkins_methods package contains various groovy scripts that are necessary for the Tests to become relevant.
Structure of the project is as following :
-src
-test
-groovy
-GitMethodsTest.groovy
-jenkins_methods
-gitMethods.groovy
This is the error that i get when i run mvn test
[ERROR] Failed to execute goal org.codehaus.gmaven:gmaven-plugin:1.5:testCompile (default) on project jenkins-jobs: startup failed:
[ERROR] /C:/Users/jenkins-jobs/src/test/groovy/GitMethodsTest.groovy: 3: unable to resolve class jenkins_methods.gitMethods
[ERROR] # line 3, column 1.
[ERROR] import jenkins_methods.gitMethods
[ERROR] ^
[ERROR]
[ERROR] 1 error
How do i solve this problem?

Has anyone used proguard to obfuscation of .war file and succeed?

Has anyone used proguard to obfuscation of .war file and succeed? if yes then please tell me the exact steps to obfuscation of war.
I am not finding any ideal document on web for my requirement.
I have created my web application in spring boot and jsp.
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<proguardVersion>5.3.3</proguardVersion>
<injar>${project.build.finalName}.jar</injar>
<outjar>${project.build.finalName}.jar</outjar>
<obfuscate>true</obfuscate>
<options>
<option>-dontshrink</option>
<option>-dontoptimize</option>
<option>-adaptclassstrings</option>
<option>-keepattributes
Exceptions,InnerClasses,Signature,Deprecated,
SourceFile,LineNumberTable,*Annotation*,EnclosingMethod</option>
<option>-keepnames interface **</option>
<option>-keepparameternames</option>
<option>-keep class
!com.slm.proguard.example.spring.boot.domain.**
{ *; }</option>
<option>-keep class com.slm.proguard.example.spring.boot.service {
*; }</option>
<option>-keep interface * extends * { *; }</option>
<option>-keepclassmembers class * {
#org.springframework.beans.factory.annotation.Autowired *;
#org.springframework.beans.factory.annotation.Value *;
}
</option>
</options>
<libs>
Include main JAVA library required.
<lib>${java.home}/lib/rt.jar</lib>
Include crypto JAVA library if necessary.
<lib>${java.home}/lib/jce.jar</lib>
</libs>
</configuration>
<dependencies>
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard-base</artifactId>
<version>5.3.3</version>
</dependency>
</dependencies>
</plugin>
I had problems with a JAR created by proguard, it seemed ok but tomcat didn't find anything inside. After a long search I found that unzipping and re-zipping with an ant-task the exact same file make a working one.
Here is the added configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>fixzip</id>
<phase>prepare-package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<!-- ******************** ********* ******************** -->
<!-- ******************** IMPORTANT ******************** -->
<!-- ******************** ********* ******************** -->
<!-- It may seems strange to unzip and re-jar the file -->
<!-- but the JAR made by Proguard is somehow not readable -->
<!-- by Tomcat and Spring beans aren't recognized -->
<!-- *************************************************** -->
<!-- Fixing JAR. -->
<move file="target/${project.build.finalName}/WEB-INF/lib/${project.artifactId}-${project.version}.jar"
tofile="target/${project.artifactId}-${project.version}_proguard_base.jar" />
<jar destfile="target/${project.build.finalName}/WEB-INF/lib/${project.artifactId}-${project.version}.jar">
<zipfileset
src="target/${project.artifactId}-${project.version}_proguard_base.jar"
includes="**/*.*" />
</jar>
</tasks>
</configuration>
</execution>
</executions>
</plugin>

Generate classes with gradle and jaxb for tests with different configuration

I'm using
id "com.bmuschko.docker-java-application" version "3.0.7"
https://github.com/rackerlabs/gradle-jaxb-plugin
gradle plugin and this configuration:
jaxb {
xsdDir = "${project.projectDir}/src/main/xsd/"
bindingsDir = "${project.projectDir}/src/main/xsd/"
xjc {
taskClassname = "org.jvnet.jaxb2_commons.xjc.XJC2Task"
args = [
'-Xfluent-api'
]
}
}
sourceSets.main.java.srcDirs += "${generatedSources}"
compileJava.dependsOn xjc
for tests I want to add more arguments to xjc. And run this new task with regular gradle build (depending the test task on this one). Maven solves this with help of executions
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.13.1</version>
<executions>
<execution>
<id>prod</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
<execution>
<id>test</id>
<phase>process-test-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generateDirectory>target/generated-test-sources/xjc</generateDirectory>
<addCompileSourceRoot>false</addCompileSourceRoot>
<addTestCompileSourceRoot>true</addTestCompileSourceRoot>
<args>
<arg>-Xfluent-api</arg>
<arg>-Xinheritance</arg>
<arg>-Xannotate</arg>
<arg>-Xvalue-constructor</arg>
<arg>-Xequals</arg>
<arg>-XhashCode</arg>
<arg>-XtoString</arg>
</args>
</configuration>
</execution>
</executions>
<configuration>
<schemaDirectory>src/main/xsd</schemaDirectory>
<bindingDirectory>src/main/xsd</bindingDirectory>
<removeOldOutput>true</removeOldOutput>
<extension>true</extension>
<verbose>true</verbose>
<readOnly>true</readOnly>
<args>
<arg>-Xfluent-api</arg>
</args>
</configuration>
</plugin>
But how to solve it in gradle? I want as result 2 sets of classes in different folders - one for production code and one for tests (with additional jaxb plugins enabled).
Simple
task tst(type: org.openrepose.gradle.plugins.jaxb.task.JaxbXjc) {
}
Requires separate configuration
> No value has been specified for property 'bindings'.
> No value has been specified for property 'episodeDirectory'.
> No value has been specified for property 'generatedFilesDirectory'.
> No value has been specified for property 'schemasDirectory'.
> No value has been specified for property 'xsds'.
different from dsl. How to reuse the dsl-based configuration?

swagger codegen jaxrs validation

Is there a way to get swagger codegen to add jaxrs validation annotations for the validation of path parameters and output model.
I mean the #Valid and #NotNull annotations in the following code:
#Path("/person/{personId}")
#GET
#Valid
public Response getPerson(#NotNull #PathParam Integer personId) {
// TO Do
}
Thanks!
You can also use a maven plugin to generate code and add the javax.validation as dependency
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>${openapi-generator-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
...
</execution>
</executions>
<dependencies>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>${validation-api.version}</version>
</dependency>
</dependencies>
</plugin>