I am getting error " cant resolve symbol SpringockitoContextLoader
" on this following line in test package
#ContextConfiguration(loader = SpringockitoContextLoader.class ....
I have migrated to spring 2.0.3 , I am suspecting SpringockitoContextLoader is not used anymore
Assuming you mean "Spring Boot" when you say "spring 2.0.3", you should use Spring Boot Test's built-in support for mocks and spies via #MockBean and #SpyBean instead of Springockito (which is a separate project that resides outside the official Spring portfolio).
Related
Good day,
I am doing a Spring Boot Application in my Eclipse IDE. When I right click on my SpringBoot Application file and run as Java application, I hitting error as follow:
APPLICATION FAILED TO START
Description:
Field tutorialRepository in com.utility.tool.ToolApplication required a bean of type 'com.utility.tool.repository.TutorialRepository' that could not be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.utility.tool.repository.TutorialRepository' in your configuration.
Then I found that I forget to include the spring boot starter data jar. Hence, I add the following code in my build.gradle and it finally run correctly:
implementation 'org.springframework.boot:spring-boot-starter-data-jpa:2.7.5'
Then I right click my project and export jar as runnable jar, and then try to run it by java -jar my.jar, and it hit back the error.
I open the jar in JdGui, and found that the spring-boot-starter-data-jpa-2.7.5.jar is inside. May I know what is my mistake? My jar structure is something as follow:
The jar is in the list but is at bottom, thus not in my screen shot.
Check your SpringBoot annotations. You may be missing some #Service, #Repository, #Component annotations.
I have this (https://github.com/Danix43/HerculesAPI) REST application made by Java with Spring Boot.
All good when for some reason the #SpringBootApplication annotation doesn't find anymore the interfaces TermometruService annothed with #Service.
I have tried everything from using #ComponentScan with the service package but then any Rest operation returns a RESOURCE_NOT_FOUND to changing the jdk version and a flush of maven repositories.
Project structure if useful
The log after running the application
2020-01-27 19:33:39.635 DEBUG 6248 --- [main] o.s.c.a.ClassPathBeanDefinitionScanner : Ignored because not a concrete top-level class: file [D:\Programe\Programare\Java\HerculesAPI\target\classes\com\herculesapi\services\TermometruService.class]
The problem was solved by annotating the TermometruServiceImple class with #Service. Also regarding the Github repository, it doesn't always have all the changes right away pushed but yes the TermometruService interface had the annotation.
Now that Alfresco integration tests of custom modules are run using Docker, I wonder how to make additional Spring beans available in this context and how to access existing Spring beans in test classes.
Until Alfresco 5.x, I used to annotate the test class with
#ContextConfiguration("classpath:alfresco/application-context.xml")
This made the Spring context available. To make Spring beans from this context available in the test class, I annotated members like this:
#Autowired
#Qualifier("authenticationComponent")
private AuthenticationComponent authenticationComponent;
In addition I was able to define additional Spring beans in src/test/resources/alfresco/extension/test-context.xml.
Is this the approach to use when writing integration tests for 6.x and Docker?
At least the annotation org.springframework.test.context.ContextConfiguration is no longer included in a module build using the Maven 4.0.0 SDK archetype.
This blog post talks about the above mentioned annotations. But the dependencies pulled in by the pom.xml created from the SDK 4 archetype don't include these annotations.
A different approach seems to be to only use
#RunWith(value = AlfrescoTestRunner.class)
on the integration test class. But how do I get the Spring beans like nodeService injected into it? And how do I declare and make available additional Spring beans which are part of my custom module and required by the integration test to succeed?
You can get the Spring context via AlfrescoTestRunner as follows:
#Before
public void setUp() {
this.nodeService = (NodeService) super.getApplicationContext().getBean("nodeService");
}
I do the same with custom beans:
super.getApplicationContext().getBean(MyType.class);
Since the integration tests run in the repository, all of the Spring context is automatically available.
Note that your test class needs to extend AbstractAlfrescoIT for this to work.
An example class may look like this:
package nl.open.mystuff;
import org.alfresco.rad.test.AbstractAlfrescoIT;
import org.alfresco.rad.test.AlfrescoTestRunner;
import org.alfresco.service.cmr.repository.NodeService;
#RunWith(value = AlfrescoTestRunner.class)
public class MyCustomIT extends AbstractAlfrescoIT {
private NodeService nodeService;
private MyType myType;
#Before
public void setUp() {
this.nodeService = (NodeService) super.getApplicationContext().getBean("NodeService");
this.myType = super.getApplicationContext().getBean(MyType.class);
}
}
In Alfresco SDK 3, you can even add your own Spring XML files under src/test/resources/alfresco/extension/*-context.xml. I imagine this still works, but I haven't tried it with SDK 4 myself.
I am currently running into some problems with spring boot and multi maven project structure. I am using Spring Boot 4.3.1.
My project structure looks as follows:
parent
-- pom.xml
-- application
-- pom.xml
-- src
-- main
-- java
-- Application.java (annotated with #SpringBootApplication)
-- test
-- java
-- MyApplicationTest.java (annotated with #SpringBootTest)
-- library
-- pom.xml
-- src
-- main
-- java (...)
-- test
-- java
-- MyLibraryTest.java (annotated with #SpringBootTest)
application module has a dependency on library.
MyApplicationTest works perfectly fine, but running MyLibraryTest instead, I fail with the following error:
java.lang.IllegalStateException: Unable to find a #SpringBootConfiguration, you need to use #ContextConfiguration or #SpringBootTest(classes=...) with your test at org.springframework.util.Assert.state(Assert.java:392)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOr FindConfigurationClasses(SpringBootTestContextBootstrapper.java:173)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:133)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:409)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:305)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:112)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:78)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:120)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:105)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:152)
My first guess is that library needs a dependency on application, but this causes a cycle.
Is there any solution to that problem?
How can I structure my application correctly?
thanks a lot for suggestions.
MyLibraryTest looks as follow:
#RunWith(SpringRunner.class)
#SpringBootTest
#Transactional
public class MyLibraryTest {
#Autowired
private MyService service;
#Test
public void testMyService_Save() {...}
}
You need to make sure that the library module's pom.xml includes -
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
which is required for the module to use the #SpringBootTest annotation. You might already be using the same in your app module but is not included in the library module.
Well post the Edit, found the question to be possibly a duplicate of Unable to find a #SpringBootConfiguration when doing a JpaTest
Also quoting from Thomas's answer from the same thread, here
The thing about #DataJpaTest and a few other annotations is that they
look for a #SpringBootConfiguration annotation in the current package,
and if they cannot find it there, they traverse the package hierarchy
until they find it.
For example, if the fully qualified name for your test class was
com.example.test.JpaTest and the one for your application was
com.example.Application, then your test class would be able to find
the #SpringBootApplication (and therein, the
#SpringBootConfiguration).
If the application resided in a different branch of the package
hierarchy, however, like com.example.application.Application, it would
not find it.
which seems to be the case for you, where you are trying to test an application in a different module itself. Hence the error that you see.
I have an application with one Spring #Controller annotated with #RequestMapping.
Using Spring (core and webmvc) 4.1.0.RELEASE or higher I'm getting exception when starting the app server.
java.lang.NoSuchMethodError: org.springframework.web.bind.annotation.RequestMapping.name()Ljava/lang/String;
at method RequestMappingHandlerMapping.createRequestMappingInfo(RequestMapping annotation, RequestCondition<?> customCondition)
Debugging, using Eclipse inspect, the method variable annotation does not contains name attribute and fails at action annotation.name(). All the other attributes values are correct (they have the values defined in the Controller method annotated with #RequestMapping).
I'm using Tomcat 7 and Maven 2.3.
mvn dependency:tree does not shows other Spring version than 4.1.0.RELEASE.
The error does not occurs when using Spring 4.0.7.RELEASE or lower.
Does anyone have any clue?
Seems like a classpath issue. An old version of RequestMapping was being loaded.