cucumber test, jhipster and oauth: ClientRegistrationRepository bean not found - spring-security

I have a problem with running Cucumber tests in a khipster project (I can reproduce it with jhipster as well) when I configure it to use oauth2.
I create the project with the following configuration file (I call it mono.jdl):
application {
config {
applicationType monolith
authenticationType oauth2
baseName helloworld
buildTool maven
packageName com.example.helloworld
testFrameworks [cucumber]
}
entities *
}
I generate the project with a command: khipster import-jdl mono.jdl.
I create a very simple Cucumber test. I create a feature file (src/test/features/kuku/kuku.feature):
Feature: just a test
Scenario: my scenario
And one two three
and a file with steps (src/test/kotlin/com/example/helloworld/cucumber/stepdefs/KukuStepDefs.kt):
package com.example.helloworld.cucumber.stepdefs
import io.cucumber.java.en.When
class KukuStepDefs : StepDefs() {
#When("one two three")
fun magic() {
println("four five six")
}
}
I try to run the integration test with a command ./mvnw integration-test. However, it fails with a following error:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.example.helloworld.web.rest.LogoutResource required a bean of type 'org.springframework.security.oauth2.client.registration.ClientRegistrationRepository' that could not be found.
The following candidates were found but could not be injected:
- Bean method 'clientRegistrationRepository' in 'OAuth2ClientRegistrationRepositoryConfiguration' not loaded because OAuth2 Clients Configured Condition registered clients is not available
Action:
Consider revisiting the entries above or defining a bean of type 'org.springframework.security.oauth2.client.registration.ClientRegistrationRepository' in your configuration.
How can I fix this problem?

The solution is to find CucumberContextConfiguration class. It contains such annotation:
#ContextConfiguration(classes = [HelloworldApp::class])
We must change it to:
import com.example.helloworld.config.TestSecurityConfiguration
(...)
#ContextConfiguration(classes = [HelloworldApp::class, TestSecurityConfiguration::class])

Related

GORM can't work with spring-boot-devtools?

in spring boot application , build.gradle :
compile("org.springframework.boot:spring-boot-devtools")
compile('org.grails:gorm-hibernate5-spring-boot:6.1.2.RELEASE')
compile('org.grails:grails-datastore-gorm:6.1.2.RELEASE')
compile('org.grails:grails-datastore-gorm-support:6.1.2.RELEASE')
compile('org.grails:grails-datastore-gorm-validation:6.1.2.RELEASE')
application would throw exception at runtime.
Error performing load command : org.hibernate.HibernateException: Javassist Enhancement failed
why?help?
find Alternatives way.
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.springframework:springloaded:1.2.7")
}
After change code, click Build-》Build project .

Dependency injection does not work in spring boot test but works in normal development

I started to develop a classical Spring Boot MVC application. I use dependency injection (using #Service, #Autowired annotation) without any problem.
When I try to run some integration test using dependency injection the same way I get the following error message from Junit:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'hu.bookandwalk.RepositoryTests':
Unsatisfied dependency expressed through field 'userService'; nested
exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type 'hu.bookandwalk.services.UserService'
available: expected at least 1 bean which qualifies as autowire
candidate. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true)}
The relevant part of the test code:
package hu.bookandwalk;
...
import hu.bookandwalk.config.MyNeo4jConfig;
import hu.bookandwalk.domain.*;
import hu.bookandwalk.services.*;
#RunWith(SpringRunner.class)
#SpringBootTest
#Transactional
#ContextConfiguration(classes={MyNeo4jConfig.class})
public class RepositoryTests {
#Autowired
Session session;
#Autowired
UserService userService;
...
}
In the hu.bookandwalk.services package I have a UserService interface without annotation and a UserServiceImpl class annotated with #Service.
I don't understand that if DI works for running my application than why it doesn't work in the test. Somehow my annotated implementation class is not discovered by spring boot as the error messages says.
The test is located in the same package as my application class: hu.bookandwalk
All my services, repositories, domains are located under this: hu.bookandwalk.services, hu.bookandwalk.domain, ...
Any idea what kind of annotation I miss for the test class to make userServiceImpl discoverable?
Try to insert UserServiceImpl.class in #ContextConfiguration

Running parameterized Junit Test from Ant throws IllegalArgumentException

I'd like to run the JUnit tests in our project from the command line using an Ant target. Up until now we ran the tests manually from Eclipse without any issues using the embedded JUnit in Eclipse.
After finally having figured out how the set the classpath I now get failed Tests for all classes that use the Parameterized Runner from JUnit 4.11.
While running the test target (ant test) the only output is "FAILED" after the name of the Testclass, even with option -v.
Generating testreports shows the following Exception:
java.lang.IllegalArgumentException: wrong number of arguments
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
What does this mean?
The version are:
- JDK 1.6
- ant 1.9.3
- JUnit 4.11
My build.xml is over 400 lines long so I'm not sure if it makes sense to post it here. Let me know if you need parts of the build.xml.
Update 13.05.2015: Here's a sample section from on of our JUnit Test classes that fail. The #Parameters section contains only one entry which is pretty useless in this case but this class still fails when run from ant.
#Parameters
public static Iterable<String[]> testData() {
return Arrays
.asList(new String[][] { { "a-rules-filename" } });
}
#Parameter
public String RULES_FILE_NAME;
Our Test class is annotated like this:
#FixMethodOrder(MethodSorters.NAME_ASCENDING)
#RunWith(Parameterized.class)
public class OurRulesTest {
The Exception is thrown due to a missing constructor in the test class.
It seems that when using the Parameterized Runner you need to specify a constructor with as many arguments as you have parameters.
For some reason this works in Eclipse without specifying a constructor.

MissingMethodException is a service: "No signature of method"

Having a really strange issue in a Grails service. I have a service thats setup like this:
package com.mydomain
import java.net.URLEncoder
import java.rmi.server.UID
import java.security.*
class EmailConfirmationService {
def sendConfirmation(Person person) {}
}
And I'm calling it from a controller with emailConfirmationService.sendConfirmation(person)
When I startup the Grails app, it throws the following error:
MissingMethodException occurred when processing request: [POST] /api/people/
No signature of method: mydomain.api.EmailConfirmationService.sendConfirmation() is applicable for argument types: (com.mydomain.Person) values: [name#mydomain.com]
All I have to do is save the EmailConfirmationService file, the watcher picks it up and recompiles, then it works great and never again throws that error.
Until I restart, then it errors until I save that file again.
I've tried the following but nothing has worked yet:
clean build
changed function name
made sure person instance existed
Any ideas?
Your service states another package (com.mydomain) than the one in the error message (mydomain.api). Please make sure, that the packages match and that the file is located in grails-app/services/com/mydomain/EmailConfirmationService.groovy (unless all of this is due to some obfuscation you did for SO)

Grails 2.3.4 War not correctly working in Tomcat 7.0.47

Running the application with grails run-app works fine but after deploying in Tomcat 7 I get following error.
groovy.lang.MissingMethodException:
No signature of method: static com.digithurst.hdspro.web.Responder.respond()
is applicable for argument types: (ResourceListCmd, QueryCmd, groovy.util.ConfigObject)
values: [ResourceListCmd#5c380e, ...]
Possible solutions: respond(HttpResource, java.lang.Object, java.lang.String)
As already said, this works outside of Tomcat. The way the method is called is exactly as it is implemented. The ResourceListCmd implements the interface HttpResource which makes it a perfect fit. This error also occurs if the first parameter is null.
groovy.lang.MissingMethodException:
No signature of method: static com.digithurst.hdspro.web.Responder.respond()
is applicable for argument types: (null, QueryCmd, groovy.util.ConfigObject)
values: [null, ...]
Possible solutions: respond(HttpResource, java.lang.Object, java.lang.String)
More on the environment:
Windows 7 64 Bit
Java 7 U45 x86
Grails 2.3.4
Tomcat 7.0.47
I have already cleaned the .grails and .m2 folders in the user directory and performed a grails clean berfore creating the war file.
[Edit after answer of H3rnst]
Controller:
def index() {
try {
ResourceListCmd configs = configService.search()
respond Responder.respond(configs, new QueryCmd(level: 'list'),
grailsApplication.config.grails.serverURL)
}
catch (Exception e) {
render status: INTERNAL_SERVER_ERROR
}
}
ResourceListCmd:
interface HttpResource {
...
}
abstract class AbstractHttpResource implements HttpResource {
...
}
class ResourceListCmd extends AbstractHttpResource {
...
}
Responder:
class Responder {
static def respond(HttpResource resource, def query, String serverURL) {
...
}
}
Your war (or tomcat server classpath) contain a duplicate or wrong version of jar that contains the class com.digithurst.hdspro.web.Responder. (The version of the class you are using developing an launching with run-app is different from the one tomcat load running your war)
You could try to unpack the war end verify the version of the problematic jar and/or use a tool like jarscan to scan for duplicate classes.
You could even try to use the command dependecy-report and search for duplicate injection of the same lib. Probably two different plugins your are using are incorporating to differente versions of the same lib causing the problem.
marko's suggestion doing run-war actually gave the final clue to solving this thing. It wasn't a problem with Tomcat but rather the environment the app was running in. run-app as well as run-war both use the "development" environment by default and therefore it worked. Only in "production" it did not, which is what is used when deployed to Tomcat.
The actual culprit was part of the configuration and the error message was right, although unexpected. I'm calling the Responder.respond() method with grailsApplication.config.grails.serverURL. The serverURL was only set in "development" mode but not in "production". That's why Groovy/Java complained about the method signature:
(ResourceListCmd, QueryCmd, groovy.util.ConfigObject) vs (HttpResource, java.lang.Object, java.lang.String)
The clue is the last parameter, the first two are correct. However, I would've expected null as the value instead of a completely different type.

Resources