No qualifying repository test in Spring - java

I'm using Spring 2.4 and jUnit 5.7.
I'm trying to use a repository create for test but I'm getting "No qualifying bean of type".
I annotated the repository #TestComponent.
package com.cjgmj.dynamicQuery.persistence.repository;
import org.springframework.boot.test.context.TestComponent;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import com.cjgmj.dynamicQuery.persistence.entity.DummyEntity;
#TestComponent
public interface DummyRepository extends JpaSpecificationExecutor<DummyEntity> {
}
And I'm trying to get it on my Test class.
#SpringBootTest
public class TextLikeSpecificationTests {
#Autowired
private DummyRepository dummyRepository;
...
}
The project structure is:
- src/test/java
|- projectPackage
|- persistence
|- entity
|- DummyEntity
|- repository
|- DummyRepository
I tried place the repository and the entity on main package, and also without annotation in the repository, but I'd got the same error.What I'm doing wrong?
Thank you in advance.

I just solved this problem. The first step was change the repository to extends JpaRepositoryImplementation. Then it throws a UnsupportedOperationException. Diging a bit I reach this post and I changed all the Collections.emptyList and Arrays.asList() to instanciate it as new ArrayList<>(). Then the test run properly.

Related

Does Spring (Boot) have a special treatment for package-names .model and .repo?

I have a #SpringBootTest that autowires a #Repository which returns an #Entity. I have no specific ComponentScanning declared.
This works as long as the Repo is in a package my.repo and the entity and test are in package my.model
If I move all three to package my.repo I get "Not a managed type" for the model.
If I move all three to package my.model I get "No qualifying bean of type" for the repo.
I know about the recommended package structure. But is it more than a recommendation? Are there strict rules for the packages hardcoded?
As long as your #Repository is in the same package with other classes/entities, it doesnt need any scanning. But yes if you choose to use a different package, you must tell spring to scan your specific packages for your #Repositoy(ies). So somewhere in your #Configuration class, use this:
#ComponentScan(basePackages = "someuniq.packagename.goeshere").
For an example - like this.

NoClassDefFoundError when init a SpringBoot 2 application in IntelliJ IDEA

I have a SpringBoot 2 application, that when starts I got this error:
...
Caused by: java.lang.NoClassDefFoundError: Lcom/bonanza/BonanzaHelper;
I went to the service that uses this class:
public class BonanzaService {
#Autowired
private BonanzaHelper bonanzaHelper;
I use Ctrl and click on the class and I got this message: Cannot find declaration to go to
but if I use Shift + Ctrl + N I can find the class in my maven repositoy
the class is imported in a parent group in the pom.xml
it may look like one of the following:
1. You may have two dependencies in the pom that point to a different version of the artifact with different locations
The class is not declared as Bean, so you can autowired it, you can do it in the #configuration file :
#Bean
public ObjectMapper bonanzaHelper() { return new BonanzaHelper();}

Springboot #EntityScan not working

I have a Springboot application and my entity model is separated from my main application included as dependency.
my Application.java is located in this package com.a.b.c
#SpringBootApplication
#EntityScan("com.a.b")
public class Applciation
{
public static void main(String args[])
{
SpringApplication.run(Applciation.class, args);
}
}
and my entity model is located in another project inside this package com.a.b
But I'm getting an error: Caused by: java.lang.IllegalArgumentException: Not a managed type: class
I can relate to you. I have spent countless hours regarding this.
I'll divide your question to 3 parts (I will use "entity-project" and "spring-project" to refer to the project containing entity classes and main project trying to incorporate entity classes) :
Part 1 of 3: Making sure your entity classes are exposed in entity-project.
Create a Config at the root of entity-project
package com.a.b
#Configuration
#Import ({Entity1.class, Entity1Repo.class, Entity1Key.class,
Entity2.class, ... })
class EntityConfig {}
Part 2 of 3: Making sure your entity classes are in the classpath of spring-project.
Since you are using SpringBoot, I'm sure you are using maven (or Gradle).
Anyway, make sure you maven install the entity-project with entity classes:
cd /path/to/entity-project/
mvn install -DskipTests
In spring-project's maven file, include to the entity-project.
Part 3 of 3: Use the EntityScan annotation.
It is just my taste, but I prefer using basePackageClasses instead of basePackage.
#SpringBootApplication
#EntityScan(basePackageClasses = {Entity1.class})
// use basePackageClasses to avoid type errors
#Import({com.a.b.EntityConfig.class}) // this is what you are missing
public class Applciation
{
public static void main(String args[])
{
SpringApplication.run(Applciation.class, args);
}
}
Viola!

#Repository is not autowired

I have a projected checked into GitHub here
https://github.com/romeoopk/demo
Please note that this is not a "complete" working project but in progress!
I have two data sources (h2 mem DB and Cassandra)
The aim of the project is to hide the implementation behind the Service.
there are two profiles I am looking against
dev - goes against h2
test - goes against Cassandra
when I run against test, it runs fine as expected but when I run against dev, I get the following message
Parameter 0 of constructor in com.example.demo.service.H2HotelServiceImpl required a bean of type 'com.example.demo.repository.HotelRepository' that could not be found.
Action:
Consider defining a bean of type 'com.example.demo.repository.HotelRepository' in your configuration.
I am unsure, how to have a proper injection so that the H2HotelRepository and H2HotelByLetterRepository get used for querying towards H2
any help is highly appreciated!!!
Your repository classes under cassandra folder is like this
#Repository
#Profile("test")
public class CassandraHotelRepository implements HotelRepository<Hotel> {
....
}
#Repository
#Profile("test")
public class CassandraHotelByLetterRepository implements HotelByLetterRepository<HotelByLetter, HotelByLetterKey> {
....
}
But in your h2 folder you have declared as
#Repository
#Profile("dev")
public abstract class H2HotelRepository implements CrudRepository<Hotel, String>, HotelRepository<Hotel> {
...
}
#Repository
#Profile("dev")
public abstract class H2HotelByLetterRepository implements CrudRepository<HotelByLetter, HotelByLetterKey>, HotelByLetterRepository<HotelByLetter, HotelByLetterKey> {
.....
}
As you can clearly see in h2 folder i.e. for profile dev, there is no concrete class. Both of your repositories under h2 are abstract.
Remove abstract and it should work fine.

Scala+Android+Roboguice: ClassNotFoundException in loader

I'm evaluating Scala on Android by starting with the NotesList demo. I was able to replace the NotesLiveFolder.java file with its Scala equivalent without problem.
Next, I introduced Roboguice, creating a simple NotesListApplication.java that sets up the Guice modules, and successfully injected a resource into the NoteEditor.java activity.
Finally, I when I tried to replace NotesListApplication.java with its Scala equivalent, I get the following runtime error before the application finishes booting:
java.lang.ClassNotFoundException: com.example.android.notepad.NotesListApplication in loader dalvik.system.PathClassLoader[/data/app/com.example.android.notepad-1.apk]
I created a Google Code project containing the complete Eclipse project and source. The original functioning NotesListApplication.java is:
package com.example.android.notepad;
import java.util.List;
import roboguice.application.RoboApplication;
import com.google.inject.Module;
public class NotesListApplication extends RoboApplication {
private Module module = new BindEverything();
public void setModule(Module module) {
this.module = module;
}
#Override
protected void addApplicationModules(List<Module> modules) {
modules.add(module);
}
}
and the Scala equivalent that causes the error is:
package com.example.android.notepad
import roboguice.application.RoboApplication
class NotesListApplication extends RoboApplication {
val module : Module = new BindEverything()
override protected def addApplicationModules(modules:java.util.List[Module] ) {
modules.add(module)
}
}
I'm building in Eclipse with the ScalaIDE plugin. I'm not running any treeshaker/proguard/etc.
The disassembly shows the Scala classes as expected:
Class descriptor : 'Lcom/example/android/notepad/NotesLiveFolder;'
...
Class descriptor : 'Lcom/example/android/notepad/NotesListApplication;'
Any ideas what could cause this?
Upgrade to 2.0-SNAPSHOT of RoboGuice and then you dont have to use RoboApplication and it all binds automatically. For more how to bind check out the slides from Mike Burtons presentations about RoboGuice at AnDevCon 2 and check out the 2.0 section on the wiki.
Like I posted on the mailing list maybe check out the apk with dedexer and see if the class was actually removed e.g. by Proguard or renamed so it cant be found as a next step.

Categories

Resources