Method on class [com.secure.Role] was used outside of a Grails application - spring

I'm completing doing everything same as described in tutorial at Grail website . However I'm receiving this strange error message :
Method on class [com.secure.Role] was used outside of a Grails application
I've done it in my business computer and personal notebook. I've tried to it with Spring IDE and different grails versions. What's the problem can not understand.

If you are trying to run a unit test without first applying mocking to a domain class then you will get this error.
Also integration tests cannot curerntly be run in an IDE and require use of the command line (some IDEs integrate the command line to run these tests). If you run an integration test from the IDE you will likely get an error like this.

Related

Property-based Testing support for Quarkus / Tech doku about the Quarkus test-engine

We have a Quarkus project in production. It runs fine but testing is a nightmare because of high complexity of the data structure and business logic.
We would like to add property-based testing to our project. I have so far failed to find a Java PBT testing library that supports Quarkus though.
I have tried *jqwik *and junit-quickcheck. They both run technically but they cannot access our project code, i.e. access the injected beans (they remain NULL during the test run), due to different test engines.
See also: Inject returns NULL when activating jqwik in Quarkus test
I have tried to write an own quarkus-extension for jqwik. I got along with the jqwik part but failed for the lack of understanding the Quarkus Test Engine.
My question:
Is there working support property-based testing support in Quarkus?
If not, can anyone provide an in-depth guide about the Quarkus test engine? Writing an own extension will take a while but (given the referring PBT library owner approves) I could probably make the resulting extension public.
You may want to look at quicktheories: https://github.com/quicktheories/QuickTheories.
What differentiates it from both jqwik and junit-quickcheck is its independence from any test framework or test engine. The obvious advantage is that you can just add it to whatever you're already using for example-based testing your Quarkus app. The disadvantage is that you get no additional lifecycle support like instantiating objects provided by DI for each try of a property. Moreover, quicktheories seems to be orphaned. So it could be a short-term solution to get started with PBT within Quarkus, but you probably also want to continue the development of integrated support for the Quarkus test engine.

Running jacoco report where integration tests are in one code base and source code is in another code base

I recently started working on creating jacoco reports for maven projects including unit and integration tests and they seem to work out correctly.
Now I have encountered a different scenario which I am not sure how to approach.
I have one workspace which consists of integration test cases - application A, but the source code does not exist in the same workspace/code base. The source code which actually runs on invoking these integration test scripts are in a different workspace/code base - application B(they are invoked using rest api calls with the localhost urls. The jboss server is started for application B so that the localhost context is up) from the integration tests.
The aim is to invoke these integration tests from application A, which in turn calls the source code of these tests in application B generating the jacoco report of the code coverage for application B.
I am not actually sure how to achieve this.
Can someone provide some input.
Thanks.
If I understand you correctly, you actually have 2 different processes in your scenario:
The "client" process that runs the integration tests and for which jacoco can be easily applied, but it's not what you need
The "server" process that runs the actual JBoss server and executes the actual code.
Client process contacts the server via HTTP.
In this case, I'm afraid jacoco won't be able to provide a coverage for you if you're running the tests from maven/gradle, because jacoco instruments only bytecode on the running JVM. So you have to be "creative" here :)
I'll list here some possible approaches
Disclaimer: I haven't tried them though (didn't work with jboss/java ee), but maybe you'll be able to at least borrow some ideas
The first approach would be running the tests together with the application somehow, like its done for example in spring tests (I'm not sure whether JBoss provides similar capabilities).
The idea is simple:
You run the integration test, it runs the jboss "embedded in the same jvm" and you can inject beans / EJB session beans into the test (like autowiring with spring).
The advantage of such a method is that you'll be able just to use jacoco maven plugin and it will instrument everything for you
I don't know how easy will be achieving this architecture technically, I know that recent jboss versions support embedded mode, So maybe you'll find This link to be a useful foundation
Another direction is to take a look at Arquillian project. They have some jacoco extension that probably will help, but I've never tried it.
And the last approach I can think of is running the jboss server with jacoco agent directly instead of relying on the build system that runs jacoco for you.
The idea here is to stream the results of covered server code into some file / tcp endpoint. So you run the jboss with -javaagent:[yourpath/]jacocoagent.jar and it starts streaming the results wherever you need it to stream. After the tests you should gather these results and prepare a report. You can find Here more information about this approach

Can you get Spring Boot JUnit tests to use the same server?

I have some Spring Boot JUnit tests that require a somewhat lengthy server start up (I'm loading a complex domain in JPA). I've put them into a test suite, but each test kicks off a new server start up.
Is it possible to set them up in such a way that the server is only started once and each test is loaded onto it and run as if the server were started by the test itself?
Okay, so the solution here is actually built in to Spring testing. That is, it caches ApplicationContexts for tests, as described here, as long as the various things like properties are the same.
Ironically, I screwed this up by trying to speed up the tests by using test properties to limit what was loaded.

How to run a project containg drools in Tomcat7?

I have created a Dynamic web project which also uses drools for providing some functionality. When i put the WAR file in Tomcat7 and the server, the drools part does not work.
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
After this line which is first line relating to drools, nothing happens.
Is some configuration required to run my project containing drools 5.5.0 Final in the Tomcat7.
Please help me. I am badly stuck and I am new to drools.
You'll have to add some facts to the working memory and execute(fire) the rules. Check out these examples on GitHub
P.S. Probably not related to Tomcat in any way. Might be worth while to try getting the rules executed from command line app first.
You need to check all the dependencies that are added to your web application (WEB-INF/lib) make sure that drools has all the required deps there, because if not it will not be able to create the knowledge builder. Most of the time if it is failing is because that you forgot to add the deps in the web app.
The following project in GitHub is a web application, containing some REST-style endpoints for validating IBANs. It uses Drools 5.5 to perform that evaluation.
https://github.com/gratiartis/sctrcd-payment-validation-web/
It generates a .war which can be loaded into Tomcat, and could be a useful starting point. The knowledge base is wrapped within a Spring service:
https://github.com/gratiartis/sctrcd-payment-validation-web/blob/master/src/main/java/com/sctrcd/payments/validation/RuleBasedIbanValidator.java
Following through how that creates a knowledge base and session might help you see where your code is going wrong.
As a bonus, you can run it up in Tomcat using "mvn tomcat7:run" to test it out immediately.

Why does inheriting from GroovyTestCase cause Spring errors in Grails 2.2?

The integration test classes generated for me by Grails when I created my domain classes do not extend the class GroovyTestCase. However, I have seen it recommended by many authors (here is an example in order to use the shouldFail method, which indeed seems to work).
However, extending my test class from GroovyTestCase has resulted in the following error message when I run test-app in the interactive Grails console:
Spring Loaded: Cannot reload new version of foo.barTests
Reason: Supertype changed from java/lang/Object to groovy/util/GroovyTestCase
Is this something I should be concerned about? I have searched online and cannot find other people complaining about this error, so it might be something new with Grails 2.2. Please advise.
I am running my tests in the grails interactive console (what you get when you run grails without arguments). I've left my tests inheriting from Object for a while, but when I run test-app I still occasionally get those messages output to the HTML test report (of the "changed from GroovyTestCase to Object" variety).
I am quite sure it's a Grails bug related to
AST Transformation annotations
spring-loaded module
As I've encountered this strange behaviour as well.
The steps that caused this problem seem to be that a Groovy class is compiled once without transformation, so that it's a subclass of java.lang.Object.
When the AST transformaton kicks it, the class is recompiled again. This time, it becomes a subclass of another class. Then Spring-loaded fails to re-load them into the memory (as JVM does not allow the same class to redefine its super class).

Resources