Drools integration with Spring, issue loading DRL files in my test case - maven

I am trying to use drools with Spring in a Maven Eclipse project.
1.My POM has the following
<properties>
<droolsVersion>5.5.0.Final</droolsVersion>
</properties>
<dependencies>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>${droolsVersion}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>${droolsVersion}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.sample.app.Test2</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>jboss</id>
<name>jboss</name>
<url>https://repository.jboss.org/nexus/content/groups/public/</url>
</repository>
</repositories>
2.My sample class is here
package com.services.dms.model;
public class Account {
private Integer balance;
public Account() {}
public Integer getBalance() {
return balance;
}
public void setBalance(Integer balance) {
this.balance = balance;
}
public Account(Integer balance) {
super();
this.balance = balance;
}
public void withdraw(int money) {
balance -= money;
}
}
My test main class
package com.services.dms.service;
import org.drools.KnowledgeBase;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatelessKnowledgeSession;
import com.services.dms.model.Account;
public class Test2 {
public static final void main(String[] args) {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("testrule.drl"), ResourceType.DRL);
KnowledgeBase kbase = kbuilder.newKnowledgeBase();
StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession();
Account account = new Account(200);
account.withdraw(150);
ksession.execute(account);
}
}
And my testrule.drl is
package com.services.dms.service
import com.services.model.account;
rule "accountBalanceAtLeast"
when
$account : Account( balance < 100 )
then
System.out.println("Warning! money running out!");
end
So when I run this project i get a file not found exception and it is not able to find the .drl file.
DRL file stored in src->resources->rules
Even When I put the rule file in the same package as my test main class I get the same error.

A classpath resource is expected in one of the directories that are collected in it. Most likely src/resources/rules is not part of the classpath. I don't know what you mean by "put the file in the same package as my test main class", but chances are that you just change the package statement of the DRL file.
Think how com.services.dms.model.Account is found by the JVM: the directory where com.services.dms.model.Account is rooted must be part of the classpath. So: put your DRL file there and expect it to be found. Conversely, prefix some directory levels to the filename in your Test2.main.
Always, always, always call the KnowledgeBuilder methods hasErrors and getErrors after adding a resource, e.g.:
if( kbuilder.hasErrors() ){
System.err.println( "### compilation errors ###" );
KnowledgeBuilderErrors errors = kbuilder.getErrors();
for( KnowledgeBuilderError err: errors ){
System.err.println( err.toString() );
}
throw new IllegalStateException( "compile errors" );
}
This will show you clearly where the problem in your DRL file is. (A double fault ;-) )

Related

Unable to set directory as resource/source in Maven Custom Plugin

I have been unable to set a folder as source/resource using the most common code, copied and pasted from Apache repositories into a custom Maven Plugin:
Main/Mojo:
package com.example;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.*;
import org.apache.maven.project.MavenProject;
import java.io.File;
import static org.twdata.maven.mojoexecutor.MojoExecutor.*;
/**
* Goal that sets directory as source/resource.
*/
#Mojo(name = "set-some-dir-as-source", requiresDependencyResolution = ResolutionScope.TEST, defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
public class SetDirectoryAsSourceMojo extends AbstractMojo {
#Component
private MavenSession mavenSession;
#Component
private BuildPluginManager pluginManager;
#Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;
/**
* Output location.
*/
#Parameter(property = "outputDirectory", defaultValue = "${project.build.directory}/some-folder/generated")
protected File outputDirectory;
/**
* Main entry into mojo.
*
* #throws MojoExecutionException
* #throws MojoFailureException
*/
#Override
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info("Executing Mark Directory as Source");
executeMojo(
plugin(
groupId("org.codehaus.mojo"),
artifactId("build-helper-maven-plugin"),
version("3.3.0")
),
goal("add-source"),
configuration(
element(name("sources"),
element(name("source"), getOutputDirectory().getPath())
)
),
executionEnvironment(
project,
mavenSession,
pluginManager
)
);
getLog().info("Executing Mark Directory as Resource");
executeMojo(
plugin(
groupId("org.codehaus.mojo"),
artifactId("build-helper-maven-plugin"),
version("3.3.0")
),
goal("add-resource"),
configuration(
element(name("resources"),
element(name("resource"),
element(name("directory"), getOutputDirectory().getPath())
)
)
),
executionEnvironment(
project,
mavenSession,
pluginManager
)
);
project.addCompileSourceRoot(getOutputDirectory().getPath());
project.addCompileSourceRoot(getOutputDirectory().getAbsolutePath());
if (getLog().isInfoEnabled()) {
getLog().info("Source directory: " + getOutputDirectory().getPath() + " added.");
getLog().info("Source directory: " + getOutputDirectory().getAbsolutePath() + " added.");
}
Resource resource = new Resource();
resource.setTargetPath(getOutputDirectory().getPath());
resource.setTargetPath(getOutputDirectory().getAbsolutePath());
getProject().addCompileSourceRoot(getOutputDirectory().getAbsolutePath());
if (getLog().isInfoEnabled()) {
getLog().info("Resource directory: " + getOutputDirectory().getPath() + " added.");
getLog().info("Resource directory: " + getOutputDirectory().getAbsolutePath() + " added.");
}
}
/**
* #return Returns the Maven project.
*/
public MavenProject getProject() {
return project;
}
/**
* #return Returns the outputDirectory.
*/
public File getOutputDirectory() {
return this.outputDirectory;
}
/**
* #param theOutputDirectory The outputDirectory to set.
*/
public void setOutputDirectory(File theOutputDirectory) {
this.outputDirectory = theOutputDirectory;
}
}
In the example above, I am using executeMojo from:
https://github.com/mojo-executor/mojo-executor
And also doing it programatically in the last few lines.
Project 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>custom-set-source-maven-plugin</artifactId>
<version>0.0.1</version>
<packaging>maven-plugin</packaging>
<name>custom-set-source-maven-plugin</name>
<url>http://maven.apache.org</url>
<organization>
<name>The Example</name>
<url>https://www.example.com/</url>
</organization>
<properties>
<java.version>1.8</java.version>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven-plugin-api.version>3.8.4</maven-plugin-api.version>
<maven-plugin-annotations.version>3.6.4</maven-plugin-annotations.version>
<maven-project.version>2.2.1</maven-project.version>
<maven-plugin-plugin.version>3.6.4</maven-plugin-plugin.version>
<maven-site-plugin.version>3.11.0</maven-site-plugin.version>
<maven-dependency-plugin.version>3.2.0</maven-dependency-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>${maven-plugin-api.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>${maven-plugin-annotations.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>${maven-project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin.version}</version>
</dependency>
<dependency>
<groupId>org.twdata.maven</groupId>
<artifactId>mojo-executor</artifactId>
<version>2.3.3</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>${maven-plugin-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>${maven-site-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<reportSets>
<reportSet>
<reports>
<report>report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
</project>
If I simply declare the plugin in my project, from org.codehaus.mojo:build-helper-maven-plugin, it works:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>add-element-templates-as-source</id>
<phase>generate-resources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}/src/main/resources/processes/some_folder/generated</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Am I missing any descriptor in my custom plugin? A way to tell the IDE that the directory has been set that I am missing? I can't seem to find anything in the Mojohaus source codes at:
https://github.com/mojohaus/build-helper-maven-plugin/blob/master/src/main/java/org/codehaus/mojo/buildhelper/AddSourceMojo.java
or
https://github.com/mojohaus/build-helper-maven-plugin/blob/master/src/main/java/org/codehaus/mojo/buildhelper/AddResourceMojo.java
While building I was seeing following error:
[ERROR]
Some dependencies of Maven Plugins are expected to be in provided scope.
Please make sure that dependencies listed below declared in POM
have set '<scope>provided</scope>' as well.
The following dependencies are in wrong scope:
* org.apache.maven:maven-plugin-api:jar:3.8.4:compile
* org.apache.maven:maven-model:jar:3.8.4:compile
* org.apache.maven:maven-artifact:jar:3.8.4:compile
* org.apache.maven:maven-project:jar:2.2.1:compile
* org.apache.maven:maven-settings:jar:2.2.1:compile
* org.apache.maven:maven-profile:jar:2.2.1:compile
* org.apache.maven:maven-artifact-manager:jar:2.2.1:compile
* org.apache.maven:maven-plugin-registry:jar:2.2.1:compile
* org.apache.maven:maven-core:jar:3.1.1:compile
* org.apache.maven:maven-settings-builder:jar:3.1.1:compile
* org.apache.maven:maven-model-builder:jar:3.1.1:compile
* org.apache.maven:maven-repository-metadata:jar:3.1.1:compile
* org.apache.maven:maven-aether-provider:jar:3.1.1:compile
Some changes needed in the POM file to get it working.
There was warning with Component annotation above Maven Session. Changed it as below:
#Parameter( defaultValue = "${session}", readonly = true )
private MavenSession mavenSession;
After above changes able to compile and execute the code using the command below:
mvn install com.example:custom-set-source-maven-plugin:0.0.1:set-some-dir-as-source install
For some reason the code was complaining about the outputDirectory.
I have the fixed code at https://github.com/gopinnath/custom-set-source-maven-plugin
Can be executed with below maven command:
install com.example:custom-set-source-maven-plugin:0.0.1:set-some-dir-as-source install exec:java -Dexec.mainClass="Example"

mvn spring-boot:run "No primary or single public constructor found...TemplateLineItemInput" but only from commandline?

I've got an up-the-middle Spring Boot + Lombok project that works like a champ from the IDE but errors strangely when I run it from the command line through mvn spring-boot:run. Its a pretty recent version Spring Boot...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
Lombok is unremarkable and came from the Spring Initializr thing (https://start.spring.io/).
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
The JavaBean its complaining about is equally boring...
import lombok.Builder;
import lombok.Data;
import java.math.BigDecimal;
#Data
#Builder(toBuilder = true)
public class TemplateLineItemInput {
private final String partMasterId;
private final String partMasterIdPath;
private final String actionType;
private final BigDecimal actionQuantity;
}
The API of this Boot project is GraphQL but when I execute the following mutation from a mvn spring-boot:run invocation it always comes back as an error (nothing on the console...the framework is kicking it out somehow).
Request...
mutation createTemplateLineItem($tbomId: ID!) {
createTemplateLineItem(
tbomId: $tbomId
input: {
partMasterId: "2"
partMasterIdPath: "808863036.1"
actionType: "ADD"
actionQuantity: "2"
}) {
...TBomFrag
}
}
...
{
"partMasterId": "5025489768",
"tbomId": "a4688d22-9d99-41a2-9777-6acc75b2aab9",
"lineItemId": "9e460199-34fb-432c-b971-8cd8321d3283"
}
Response...
{
"errors": [
{
"message": "No primary or single public constructor found for class aero.blue.ems.boa.domain.TemplateLineItemInput - and no default constructor found either",
"locations": [
{
"line": 20,
"column": 3
}
],
"path": [
"createTemplateLineItem"
],
"extensions": {
"classification": "INTERNAL_ERROR"
}
}
],
"data": {
"createTemplateLineItem": null
}
}
My spring-boot-maven-plugin is configured like...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.4</version>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>repackage</id>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
...also from Spring Initializr
When I run the Application from the IDE directly, no issue. The mutation works fine.
Is there something missing from my spring-boot:run config in the pom.xml or something? Did I have to clue the plugin into annotation processing? This is really confusing.
Please,
Check the following config on section plugins at you pom.xml project, as following here:
<project>
<modelVersion>4.0.0</modelVersion>
<artifactId>getting-started</artifactId>
<!-- ... -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
For more information about, check this link: https://docs.spring.io/spring-boot/docs/2.5.4/maven-plugin/reference/htmlsingle/
Ultimately this comes down to Lombok misconfiguration of my beans. The fix is to add the #AllArgsConstructor to the bean definition
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import java.math.BigDecimal;
#Data
#Builder(toBuilder = true)
#AllArgsConstructor
public class TemplateLineItemInput {
private final String partMasterId;
private final String partMasterIdPath;
private final String actionType;
private final BigDecimal actionQuantity;
}
How we figured this out was to "Delombok" the Bean and look at the resulting code. This observation matched the error message; there was no public constructor.
...
TemplateLineItemInput(String partMasterId, String partMasterIdPath, String actionType, BigDecimal actionQuantity) {
this.partMasterId = partMasterId;
this.partMasterIdPath = partMasterIdPath;
this.actionType = actionType;
this.actionQuantity = actionQuantity;
}
Somehow (I still don't fully get why), the #Builder(toBuilder=true) annotation had Lombok producing a package private constructor. Jackson needed something public.
Adding the #AllArgsConstructor annotation made that constructor public and all is well.
public TemplateLineItemInput(String partMasterId, String partMasterIdPath, String actionType, BigDecimal actionQuantity) {
this.partMasterId = partMasterId;
this.partMasterIdPath = partMasterIdPath;
this.actionType = actionType;
this.actionQuantity = actionQuantity;
}
Delombok was the key.

maven-pmd-plugin not identifying errors

I am trying to configue the maven-pmd-plugin.
I have added the following to my POM.xml:
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<rulesets>
<ruleset>category/java/codestyle.xml/ControlStatementBraces</ruleset>
<ruleset>category/java/braces.xml</ruleset>
<ruleset>category/java/naming.xml</ruleset>
</rulesets>
</configuration>
</plugin>
</plugins>
</reporting>
and am testing against the following class:
package org.example;
import java.beans.BeanInfo;
public class Ct {
public int d(int a, int b) {
if (b==0)
return Integer.MAX_VALUE;
else
return a / b;
}
public void loop(){
while(1==1)
System.out.println("sdfj");
}
}
I'm running the command with mvn pmd:pmd and the only error that is being reported is the unused import. Can anyone tell me what I'm doing wrong as it should pick up more errors.

What is the appropriate driver to load for embedded DynamoDB in a Spring app?

I followed various tutorials to end up with the configuration below. There may also be incorrect configuration causing the problem. When I run the tests, I get:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (the profiles "test" are currently active).
Research has lead me to believe that the appropriate driver is not being loaded and I have to add it to .properties. Is that something included in DynamoDBLocal library? I can't find it in the docs and it seems my only option is to get a 3rd party driver from the web.
Here's are the important parts:
pom.xml:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.11.34</version>
</dependency>
<dependency>
<groupId>com.github.derjust</groupId>
<artifactId>spring-data-dynamodb</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>DynamoDBLocal</artifactId>
<version>1.10.5.1</version>
</dependency>
DataSourceConfigLocal:
#Configuration
#EnableWebMvc
#EnableDynamoDBRepositories(basePackages="com.cfa.dao")
#Profile({"local", "test"})
public class DataSourceConfigLocal {
#Value("${amazon.dynamodb.endpoint}")
private String amazonDynamoDBEndpoint;
#Value("${amazon.aws.accesskey}")
private String amazonAWSAccessKey;
#Value("${amazon.aws.secretkey}")
private String amazonAWSSecretKey;
#Bean
public AmazonDynamoDB amazonDynamoDB() {
AmazonDynamoDB amazonDynamoDB
= new AmazonDynamoDBClient(amazonAWSCredentials());
if (!StringUtils.isEmpty(amazonDynamoDBEndpoint)) {
amazonDynamoDB.setEndpoint(amazonDynamoDBEndpoint);
}
return amazonDynamoDB;
}
#Bean
public AWSCredentials amazonAWSCredentials() {
return new BasicAWSCredentials(
amazonAWSAccessKey, amazonAWSSecretKey);
}
}
IntegrationTest:
#RunWith(SpringJUnit4ClassRunner.class)
#WebAppConfiguration
#Profile("test")
#TestPropertySource(properties = {
"amazon.dynamodb.endpoint=http://localhost:8000/",
"amazon.aws.accesskey=x",
"amazon.aws.secretkey=x" })
public class OrderRequestRepositoryIntegrationTest {
private DynamoDBMapper dynamoDBMapper;
#Autowired
private AmazonDynamoDB amazonDynamoDB;
#Autowired
OrderRequestDao orderRequestDao;
private static final String STORE_NUMBER = "100";
#Before
public void setup() throws Exception {
dynamoDBMapper = new DynamoDBMapper(amazonDynamoDB);
CreateTableRequest tableRequest = dynamoDBMapper
.generateCreateTableRequest(OrderRequest.class);
tableRequest.setProvisionedThroughput(
new ProvisionedThroughput(1L, 1L));
amazonDynamoDB.createTable(tableRequest);
dynamoDBMapper.batchDelete(
(List<OrderRequest>)orderRequestDao.findAll());
}
#Test
public void sampleTestCase() {
OrderRequest orderRequest = new OrderRequest(STORE_NUMBER);
orderRequestDao.save(orderRequest);
List<OrderRequest> result
= (List<OrderRequest>) orderRequestDao.findAll();
assertTrue("Not empty", result.size() > 0);
assertTrue("Contains item with expected cost",
result.get(0).getStoreNumber().equals(STORE_NUMBER));
}
}
I am not sure whether you have already referred this. I am adding this as it may help you.
Test using HTTP and without using HTTP
pom file which has the server runner and sql lite
1) Also, use the latest version 1.11.0.1 of the JAR.
2) SQL Lite lib in classpath
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>DynamoDBLocal</artifactId>
<version>${aws.dynamodblocal.version}</version>
</dependency>
<profile>
<id>start-dynamodb-local</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${exec.maven.plugin.version}</version>
<executions>
<execution>
<phase>initialize</phase>
<configuration>
<executable>java</executable>
<arguments>
<argument>-cp</argument>
<classpath/>
<argument>-Dsqlite4java.library.path=${basedir}/target/dependencies</argument>
<argument>com.amazonaws.services.dynamodbv2.local.main.ServerRunner</argument>
<argument>-inMemory</argument>
<argument>-port</argument>
<argument>${dynamodb-local.port}</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>

ContextConfiguration does not seem to load my beans for TestNg testcase

I am new to TestNg. I am trying to test one sample hello world program.
Following is my project structure
Source Package
com.mycompany.mavenproject1 =>HelloWorld.java
Test Package
com.test => NewTestConfig.java & NewTestNGTest.java
Helloworld.java
package com.mycompany.mavenproject1;
public class HelloWorld {
String msg = "test";
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
NewTestConfig.java
package com.test;
import com.mycompany.mavenproject1.HelloWorld;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
#Configuration
public class NewTestConfig {
#Bean
public HelloWorld helloWorldBean() {
HelloWorld hw = new HelloWorld();
return hw;
}
}
NewTestNGTest.java
package com.test;
import com.mycompany.mavenproject1.HelloWorld;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.springframework.test.context.ContextConfiguration;
#Test
#ContextConfiguration(classes={ NewTestConfig.class })
public class NewTestNGTest {
#Autowired
HelloWorld hw ;
#Test
public void exampleOfTestNgMaven() {
String s = hw.getMsg();
System.out.println("This is TestNG-Maven Example" + s);
}
#BeforeClass
public static void setUpClass() throws Exception {
}
#AfterClass
public static void tearDownClass() throws Exception {
}
#BeforeMethod
public void setUpMethod() throws Exception {
}
#AfterMethod
public void tearDownMethod() throws Exception {
}
}
My pom.xml looks like below:
<?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>com.mycompany</groupId>
<artifactId>mavenproject1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>mavenproject1</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
When I try to run the test I get NullpointerException (at hw.getMsg();)
Error stack:
Test set: com.test.NewTestNGTest
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.209 sec <<< FAILURE!
exampleOfTestNgMaven(com.test.NewTestNGTest) Time elapsed: 0.003 sec <<< FAILURE!
java.lang.NullPointerException
at com.test.NewTestNGTest.exampleOfTestNgMaven(NewTestNGTest.java:25)
I tried to run test in debug mode, I always get HelloWorld bean as null. So eventually test fails due to NullPointerException. I tried many other variations such as instead of config class tried using applicationContext.xml but my bean never gets injected.
For last 2 days I am struggling to make simple test pass but unable to figure out what's going wrong. Why my context is not getting loaded for test?
Any help is greatly appreciated.
Thanks
Manisha

Resources