Run JUnit 5 Test Suite loading Spring context - java

I'm facing problems when running a Test Suite of JUnit 5 Jupiter with Spring.
I'm using latest version of Spring (5.2.8-RELEASE) (not spring-boot) and latest version of JUnit Jupiter (5.7.0).
Here is my Test Suite code:
package mypackage.test;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.suite.api.SelectPackages;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.web.AnnotationConfigWebContextLoader;
import org.springframework.test.context.web.WebAppConfiguration;
#WebAppConfiguration
#ExtendWith(SpringExtension.class)
#ContextConfiguration(loader = AnnotationConfigWebContextLoader.class, classes = {
MyRootSpringConfiguration.class })
#SelectPackages("mypackage.test.units")
class MyTestSuite {
}
This way, I'm loading the context of the application, and then I want to execute all the corresponding tests.
But it seems not working, as Eclipse doesn't recognize this Class as JUnit 5 Test Class.
I have a similar configuration for the individual Test Classes, and they work fine. But each file loads the context, and that's not a good solution. I prefer to have a Suite that loads the context once, and then all the Tests uses this context to be executed.
Any idea about how to configure it?

Related

Redundant declaration: #SpringBootApplication already applies given #ComponentScan

I created a spring boot application(3.0.1). Then I connected the Postgres database and executed it. The application is up on server 8080 smoothly. then I added below-mentioned annotations.
#EnableAutoConfiguration
#ComponentScan
whole file,
package com.example.SpringRecap;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
#SpringBootApplication
#EnableAutoConfiguration
#ComponentScan
public class SpringRecapApplication {
public static void main(String[] args) {
SpringApplication.run(SpringRecapApplication.class, args);
}
#GetMapping("/")
public void Employee() {
}
}
Now, below mentioned error are appeared,
Redundant declaration: #SpringBootApplication already applies #EnableAutoConfiguration
Redundant declaration: #SpringBootApplication already applies given #ComponentScan
Image of InteliJ:
If anyone knows the reason, please help me. If further information is needed to solve the problem please put a comment here.
Thank You.
below mentioned error are appeared: This means that you can remove #ComponentScan and #EnableAutoConfiguration.
As the documentation on spring mentioned, it is included in #SpringBootApplication:
Many Spring Boot developers like their apps to use auto-configuration, component scan and be able to define extra configuration on their "application class". A single #SpringBootApplication annotation can be used to enable those three features, that is:
#EnableAutoConfiguration: enable Spring Boot’s auto-configuration mechanism
#ComponentScan: enable #Component scan on the package where the application is located (see the best practices)
#Configuration: allow to register extra beans in the context or import additional configuration classes
It is a check in the plugin of Intellij. Also see https://youtrack.jetbrains.com/issue/IDEA-177632

CommandLineRunner functionality in Spring MVC application

We have a Spring MVC project with multiple Maven modules. We package it into an EAR and deploy it to a WildFly server.
I am trying to do a single time job on project start up. Thus, I thought about the CommandLineRunner interface, the project would compile and run but the commandLineRunner run method wouldn't run.
I guess it's because we are using an MVC Spring project and not a SpringBoot one with its own embedded server.
Can you suggest any ways to implement such a concept in Spring MVC ?
Thanks.
You can do something like that:
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
#Component
public class StartupExec implements {
#EventListener(ContextRefreshedEvent.class)
public void contextRefreshedEvent() {
// do whatever you need here
}
}
This is from this answer.

Spring boot 2. Java 10. JUnit. The package org.slf4j is accessible from more than one module: <unnamed>, slf4j.api

I use Maven, Java 10, Spring boot 2 and Junit 5 for create my application with unit tests.
Main application class:
package mypackage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class MyApplication {
private final static Logger LOGGER = LoggerFactory.getLogger(MyApplication.class);
public static void main(String[] args) {
LOGGER.info("I'am running...");
SpringApplication.run(MyApplication.class, args);
}
}
My JUnit test class:
package mypackage;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
#ExtendWith(SpringExtension.class)
#SpringBootTest(classes=MyTest.class)
public class MyTest {
private static final Logger LOGGER = LoggerFactory.getLogger(MyTest.class);
#Test
public void myTest() {
LOGGER.info("Message from test");
}
}
When i import my maven project in Eclipse 4.10, i get error on my test class:
The package org.slf4j is accessible from more than one module: <unnamed>, slf4j.api
But mvn install work fine and maven-surefire-plugin run correct. What i'm doing wrong? Or it's eclipse bug? Maybe Java 10, Junit and SLF4J not working together? in my module-info.java: requires slf4j.api;
Please help me.
I believe you've added the jar for slf4j to the ModulePath:
Go to Project -> BuildPath -> Config BuildPath -> Remove Jars from Modulepath
This happens when you have added the external jars in the ModulePath.
Remove those added external jars and add dependency using pom.xml
Check under Dependency Hierarchy, slf4j library should be under one dependency
Need remove module-info.class from your spring-boot project. spring-boot/#16031

Mocking Spring Boot

I want to implements tests in Spring Boot (Spring Boot 1.4.2.RELEASE), so I am checking this example
https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4
but not able to find the given method anywhere in this package
import static org.mockito.Mockito.*
On the bottom of the article there is a link to the github repo with the full source of the test. There you can see the following line:
import static org.mockito.BDDMockito.given;

Cannot Compile Spring Boot Project after Update to New Version

I updated my Spring Boot Application from 1.3.4 to 1.4.3 and refreshed the project using the Gradle Refresh. I now have an error in my main application class:
The project was not built since its build path is incomplete. Cannot
find the class file for java.lang.Object. Fix the build path then try
building this project BeverageDataServices Unknown Java Problem
The type java.lang.Object cannot be resolved. It is indirectly
referenced from required .class
files
BeverageDataServicesApplication.java /BeverageDataServices/src/main/java/com/boelter/beverage
Here is the main application class:
package com.xxx.beverage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.EnableScheduling;
#SpringBootApplication (exclude = {DataSourceAutoConfiguration.class, MailSenderAutoConfiguration.class})
#ComponentScan(basePackages = "com.xxx.xxx")
#EntityScan("com.xxx.xxx.model")
#EnableScheduling
public class BeverageDataServicesApplication {
public static void main(String[] args) {
SpringApplication.run(BeverageDataServicesApplication.class, args);
}
}
It looks like the JRE was removed or not present in the Java Build Path ... thank you both for your quick insight!! I added the Java 8 JRE to the Build Path and it works... thanks

Categories