When I run my Grails project in my local environment everything works fine however when I use grails package and deploy it to our test server it isn't able to load the page because it throws a NoClassDefFoundError
This is the code for the loading of the specific page:
package com.x.scheduledReport
import com.x.Account
import com.x.XUtil
import com.x.ResponseList
import com.x.ScheduledReport
import com.x.UserService
import com.x.scheduledReports.ScheduledReportDeleteAllCommand
import com.x.scheduledReports.ScheduledReportDeleteCommand
import com.x.scheduledReports.ScheduledReportResponse
import com.x.scheduledReports.ScheduledReportUpdateCommand
import com.x.scheduledReports.ScheduledReportsCommand
import grails.transaction.Transactional
import com.x.scheduledreport.ScheduledReportUtil
#Transactional
class ScheduledReportBusinessService {
ScheduledReportService scheduledReportService
UserService userService
def list(Map params) {
if (params.start) {
params.offset = params.start
}
if (params.length) {
params.max = Math.min(params.length ? params.length as Integer : 100, 1000)
}
params.columns = XUtil.getSearchMap(params)
Account account = userService.getAccountForCurrentUser()
def scheduledReports = scheduledReportService.findAllByAccount(params, account)
params.total = scheduledReports.count
List<ScheduledReportResponse> scheduledReportResponseList = ScheduledReportUtil.toScheduledReportResponseList(scheduledReports.list)
new ResponseList(recordsTotal: scheduledReports.count, recordsFiltered: scheduledReports.count,
data: scheduledReportResponseList, draw: params.draw ? params.draw as int : 0)
}
}
This is the error thrown in the logs
ERROR 2018-04-05 14:18:42,242
org.springframework.boot.context.web.ErrorPageFilter - Forwarding to
error page from request [/rest/scheduledreport/list/true] due to
exception [com/x/scheduledreport/ScheduledReportUtil]
java.lang.NoClassDefFoundError:
com/x/scheduledreport/ScheduledReportUtil at
com.x.scheduledReport.ScheduledReportBusinessService.$tt__list(ScheduledReportBusinessService.groovy:33)
at
com.x.scheduledReport.ScheduledReportBusinessService$_list_closure1.doCall(ScheduledReportBusinessService.groovy)
There were inconsistensies with the folder names for ScheduledReport making all the names the same fixed the issue.
Related
serenity-appium is working fine on using serenity version 1.7.4 and serenity cucumber version 1.6.3. However getting below error on using serenity and cucumber version 3.0.5 for the page
[main] WARN net.thucydides.core.pages.PageFactory - Failed to instantiate page of type class pageObjects.LoginPageMobile (net.thucydides.core.webdriver.DriverConfigurationError: Could not instantiate class io.appium.java_client.AppiumDriver)
Given User launches "Mobile" application # starter.stepdefinitions.LoginSteps.userLaunchesApplication(java.lang.String)
net.thucydides.core.pages.WrongPageError: The page object class pageObjects.LoginPageMobile could not be instantiated:
Failed to instantiate page (net.thucydides.core.webdriver.DriverConfigurationError: Could not instantiate class io.appium.java_client.AppiumDriver)
Tried with below pages
Without MobilePageObject
package pageObjects;
import io.appium.java_client.pagefactory.AndroidFindBy;
import net.serenitybdd.core.pages.PageObject;
import org.openqa.selenium.WebElement;
public class LoginPageMobile extends PageObject {
#AndroidFindBy(xpath="//android.widget.Button[#text='Log In']")
private WebElement WPLogInButton;
public void doLogin(){
typeInto(WPLogInButton,"test#test.com");
}
}
With MobilePageObject
package pageObjects;
import com.google.common.base.Predicate;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import net.serenitybdd.core.pages.PageObject;
import io.appium.java_client.android.AndroidDriver;
import net.thucydides.core.webdriver.WebDriverFacade;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
public class MobilePageObject extends PageObject {
public MobilePageObject(final WebDriver driver) {
super(driver, new Predicate<PageObject>() {
#Override
public boolean apply(PageObject page) {
PageFactory.initElements(new AppiumFieldDecorator(((WebDriverFacade) page.getDriver()).getProxiedDriver()), page);
return true;
}
});
}
public MobilePageObject() {
}
}
Serenity.properties
webdriver.driver= appium
appium.hub = http://localhost:4723/wd/hub
######## android CAPS ######
appium.automationName = Appium
appium.platformName= Android
appium.platformVersion = 11.0
appium.deviceName = emulator-5554
appium.app = serenity-cucumber-starter/src/test/resources/Calculator.apk
Extending PageObject rather than MobilePageObject for the page resolved the issue for serenity and cucumber version 3.0.5.
I've written a feign client and I would like to test that it works using a unit test.
For my case, integration tests is not the right approach for the current development stage.
The feign client is null, I receive a NullPointerException while running the test.
How can I autowire it?
Feign client
package com.myapp.clients;
import com.myapp.model.StatusResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#FeignClient(name="myClient", url="${feign.clients.my.url}")
public interface myClient {
#RequestMapping(method= RequestMethod.GET, value="/v1/users/{userId}")
StatusResponse getStatus(
#RequestHeader(value = "Auth", required = true) String authorizationHeader,
#RequestHeader(value = "my_tid", required = true) String tid,
#PathVariable("userId") String userId);
}
Tests:
package com.myapp.clients;
import com.intuit.secfraudshared.step.broker.model.StatusResponse;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
public class MyClientTest {
#Autowired
MyClient myClient;
#Test
public void testmyClient_status200() {
StatusResponse myResponse = myClient.getStatus("", "tidTestSample", "4626745161770145");
Assert.assertNotNull(iusResponse);
}
}
How can autowire MyClient?
The method that has worked for me so far while trying to test Feign Clients is stubbing the response via wiremock. You would need to add dependency for wiremock.
testImplementation 'org.springframework.cloud:spring-cloud-contract-wiremock'
Then you would need to annotate as
#RunWith(SpringRunner.class)
#SpringBootTest(properties = "feign.clients.my.url=http://localhost:${wiremock.server.port}")
#AutoConfigureWireMock(port = 0)
And then stub using wiremock.
stubFor(post(urlPathMatching("/v1/users/([a-zA-Z0-9-]*)")).willReturn(aResponse().withStatus(200).withHeader("content-type", "application/json").withBody("{\"code\":200,\"status\":\"success\"})));
where ([a-zA-Z0-9-]*) is regex for {userId} assuming it is alphanumeric.
And then, of course, assert.
StatusResponse myResponse = myClient.getStatus("", "tidTestSample", "4626745161770145");
Assert.assertNotNull(myResponse);
I'm trying to run sql-code from inside an ordinary Groovy class (no service).
In Grails 2 I could access a datasource by doing this:
public GroovyClass() {
def ctx = ServletContextHolder.servletContext.getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT)
def dataSource = ctx.getBean('dataSource')
sql = new Sql(dataSource)
}
After migrating to Grails 3.3.8 the code no longer works. What is the correct method in Grails 3.3.8?
The "Hollywood Principle" says "Don't call us, we will call you" and that is what dependency injection is about. In your case, don't go get the dataSource, have the dataSource given to you.
There are a number of ways to do that. See the project at https://github.com/jeffbrown/asdudemo.
https://github.com/jeffbrown/asdudemo/blob/master/src/main/groovy/ascudemo/helpers/FirstGroovyClass.groovy
// src/main/groovy/ascudemo/FirstGroovyClass.groovy
package ascudemo.helpers
import groovy.sql.Sql
import groovy.util.logging.Slf4j
import org.springframework.beans.factory.InitializingBean
import javax.sql.DataSource
#Slf4j
class FirstGroovyClass implements InitializingBean {
DataSource dataSource
Sql sql
void logSomeInfo() {
// both have been initialized
log.debug "dataSource: ${dataSource}"
log.debug "sql: ${sql}"
}
#Override
void afterPropertiesSet() throws Exception {
sql = new Sql(dataSource)
}
}
https://github.com/jeffbrown/asdudemo/blob/master/src/main/groovy/ascudemo/helpers/SecondGroovyClass.groovy
// src/main/groovy/ascudemo/SecondGroovyClass.groovy
package ascudemo.helpers
import groovy.sql.Sql
import groovy.util.logging.Slf4j
import javax.annotation.PostConstruct
import javax.sql.DataSource
#Slf4j
class SecondGroovyClass {
DataSource dataSource
Sql sql
void logSomeInfo() {
// both have been initialized
log.debug "dataSource: ${dataSource}"
log.debug "sql: ${sql}"
}
#PostConstruct
void initSql() throws Exception {
sql = new Sql(dataSource)
}
}
https://github.com/jeffbrown/asdudemo/blob/master/src/main/groovy/ascudemo/helpers/ThirdGroovyClass.groovy
// src/main/groovy/ascudemo/SecondGroovyClass.groovy
package ascudemo.helpers
import groovy.sql.Sql
import groovy.util.logging.Slf4j
import org.springframework.beans.BeansException
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
import javax.sql.DataSource
#Slf4j
class ThirdGroovyClass implements ApplicationContextAware {
Sql sql
void logSomeInfo() {
// sql been initialized
log.debug "sql: ${sql}"
}
#Override
void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
DataSource dataSource = applicationContext.getBean('dataSource', DataSource)
sql = new Sql(dataSource)
}
}
https://github.com/jeffbrown/asdudemo/blob/master/grails-app/controllers/ascudemo/DemoController.groovy
// grails-app/controllers/ascudemo/DemoController.groovy
package ascudemo
class DemoController {
SomeService someService
def index() {
someService.logSomeInfo()
render 'Success'
}
}
https://github.com/jeffbrown/asdudemo/blob/master/grails-app/services/ascudemo/SomeService.groovy
// grails-app/services/ascudemo/SomeService.groovy
package ascudemo
import ascudemo.helpers.FirstGroovyClass
import ascudemo.helpers.SecondGroovyClass
import ascudemo.helpers.ThirdGroovyClass
class SomeService {
FirstGroovyClass firstGroovyBean
SecondGroovyClass secondGroovyBean
ThirdGroovyClass thirdGroovyBean
def logSomeInfo() {
firstGroovyBean.logSomeInfo()
secondGroovyBean.logSomeInfo()
thirdGroovyBean.logSomeInfo()
}
}
https://github.com/jeffbrown/asdudemo/blob/master/grails-app/conf/spring/resources.groovy
// grails-app/conf/spring/resources.groovy
import ascudemo.helpers.FirstGroovyClass
import ascudemo.helpers.SecondGroovyClass
import ascudemo.helpers.ThirdGroovyClass
beans = {
// demonstrates one approach
firstGroovyBean(FirstGroovyClass) { bean ->
bean.autowire = 'byName'
}
// demonstrates another approach
secondGroovyBean(SecondGroovyClass) { bean ->
bean.autowire = 'byName'
}
// demonstrates a third approach
thirdGroovyBean ThirdGroovyClass
}
Run the app and send a request to http://localhost:8080/demo/ and that will verify that all 3 approaches worked.
There are other ways to do this but I hope that one of the above will be helpful to you.
Best of luck!
I just tested this and it seems to give the datasource
def ds = Holders.grailsApplication.mainContext.getBean('dataSource')
println "DataSource: ${ds}" ---> DataSource: org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy#5e91ade8
Didn't try any operations on it, but that looks right.
I've recently upgraded to grails 3.3.1 and realised that grails.test.mixin.Mock has been pulled to separate project which has been build just for backward compatibility according to my understanding org.grails:grails-test-mixins:3.3.0.
I've been using #Mock annotation to mock Grails service injected into groovy/src class under test. What is the tactic to mock collaborating services in this case? Is there anything from Spock what I can use or should I fallback to grails-test-mixins plugin?
Class under test:
import gra
ils.util.Holders
import grails.util.Holders
class SomeUtilClass {
static MyService myService = Holders.grailsApplication.mainContext.getBean("myService")
static String myMethod() {
// here is some code
return myService.myServiceMethod()
}
}
My test spec (Grails 3.2.1):
import grails.test.mixin.Mock
import spock.lang.Specification
#Mock([MyService])
class ValidatorUtilsTest extends Specification {
def 'my test'() {
when:
def result = SomeUtilClass.myMethod()
then:
result == "result"
}
}
Due to you use Holders.grailsApplication in your SomeUtilClass, you can try to add #Integration annotation:
import grails.testing.mixin.integration.Integration
import spock.lang.Specification
#Integration
class ValidatorUtilsTest extends Specification {
def 'my test'() {
when:
def result = SomeUtilClass.myMethod()
then:
result == "result"
}
}
Not sure, but hope it work for you.
Try with this code
#TestMixin(GrailsUnitTestMixin)
#Mock([your domains here])
class ValidatorUtilsTest extends Specification {
static doWithSpring = {
myService(MyService)
}
def 'my test'() {
when:
def result = SomeUtilClass.myMethod()
then:
result == "result"
}
}
Remove the #Mock annotation and implement ServiceUnitTest<MyService> in your test class.
I've been starting to use GraphAware timetree for neo4j, and so far its working out pretty well. Now I'm trying to work out how I can unit / integration test my code that uses neo4j timetree.
I've put together some code as below... but still I'm getting the message:
org.neo4j.ogm.exception.CypherException: Error executing Cypher "Neo.ClientError.Procedure.ProcedureNotFound"; Code: Neo.ClientError.Procedure.ProcedureNotFound; Description: There is no procedure with the name `ga.timetree.events.attach` registered for this database instance. Please ensure you've spelled the procedure name correctly and that the procedure is properly deployed.
Am I sort of on the right track?
package myproject.core;
import java.util.ArrayList;
import java.util.HashMap;
import javax.inject.Inject;
import org.junit.After;
import org.junit.runner.RunWith;
import org.neo4j.graphdb.DynamicLabel;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.ogm.session.Session;
import org.neo4j.ogm.session.SessionFactory;
import org.neo4j.ogm.testutil.MultiDriverTestClass;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.neo4j.template.Neo4jOperations;
import org.springframework.test.context.junit4.SpringRunner;
import com.graphaware.common.policy.NodeInclusionPolicy;
import com.graphaware.module.timetree.module.TimeTreeConfiguration;
import com.graphaware.module.timetree.module.TimeTreeModule;
import com.graphaware.runtime.GraphAwareRuntime;
import com.graphaware.runtime.GraphAwareRuntimeFactory;
import myproject.core.context.TestPersistenceContext;
#RunWith(SpringRunner.class)
#SpringBootTest(classes = TestPersistenceContext.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AbstractTest extends MultiDriverTestClass {
#Inject
private Neo4jOperations neo4jOperations;
public AbstractTest() {
new SessionFactory("myproject.model.pojos").openSession();
TimeTreeConfiguration timeTreeConfiguration = TimeTreeConfiguration.defaultConfiguration();
TimeTreeModule timeTreeModule = new TimeTreeModule("TT.1", timeTreeConfiguration, super.getGraphDatabaseService());
GraphAwareRuntime runtime = GraphAwareRuntimeFactory.createRuntime(super.getGraphDatabaseService());
runtime.registerModule(timeTreeModule);
runtime.start();
}
#After
public void clearDatabase() {
neo4jOperations.query("match (n) detach delete n;", new HashMap<>());
neo4jOperations.clear();
}
}
Please change your AbstractTest() constructor to read as follows:
public AbstractTest() {
new SessionFactory("myproject.model.pojos").openSession();
TimeTreeConfiguration timeTreeConfiguration = TimeTreeConfiguration.defaultConfiguration();
TimeTreeModule timeTreeModule = new TimeTreeModule("TT.1", timeTreeConfiguration, super.getGraphDatabaseService());
TimeTreeProcedures.register(super.getGraphDatabaseService());
GraphAwareRuntime runtime = GraphAwareRuntimeFactory.createRuntime(super.getGraphDatabaseService());
runtime.registerModule(timeTreeModule);
runtime.start();
}
Note the added line: TimeTreeProcedures.register(super.getGraphDatabaseService());