SpringBoot - Can't resolve #RunWith - cannot find symbol - spring-boot

SpringBoot project.
In build.gradle:
dependencies {
implementation 'com.google.code.gson:gson:2.7'
implementation 'com.h2database:h2'
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml'
implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'
implementation('com.squareup.retrofit2:retrofit:2.4.0')
implementation('com.squareup.retrofit2:converter-gson:2.4.0')
implementation group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
Here my test class:
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
#RunWith(SpringRunner.class)
#DataJpaTest
public class CategoryRepositoryIntegrationTest {
#Autowired
private TestEntityManager entityManager;
#Autowired
private CategoryRepository productRepository;
But I get error:
error: cannot find symbol
#RunWith(SpringRunner.class)
^
symbol: class RunWith
1 error
FAILURE: Build failed with an exception

You mixed JUnit 4 and 5. You use the Test annotation from JUnit 5 and the RunWith annotation is from JUnit 4. I would recommend using JUnit 5. For this you only need to replace RunWith with the following line:
#ExtendWith(SpringExtension.class)
Or if you use SpringBoot 2.1 or older, you can remove the RunWith annotation and it should also work.

I found this from Spring boot doc, not sure could help with your question.
If you are using JUnit 4, don’t forget to also add #RunWith(SpringRunner.class) to your test, otherwise the annotations will be ignored. If you are using JUnit 5, there’s no need to add the equivalent #ExtendWith(SpringExtension.class) as #SpringBootTest and the other #…Test annotations are already annotated with it.
here is the link https://docs.spring.io/spring-boot/docs/2.1.5.RELEASE/reference/html/boot-features-testing.html
and check section 46.3

Related

Springboot Webflux endpoint not found by test - what is causing it?

I have the following code, and am consistently receiving 404 not found errors? Any advice would be much appreciated!
I've researched conflicting dependencies which does not seem to be the problem. I've also ensured that I am returning the correct content type.
One thing that I am not sure I'm doing correctly is annotating with the Bean and Autowired annotations. I have little understanding of what those do at the moment.
Router class below
#Configuration
#AllArgsConstructor
public class AgencyRouter {
#Bean
public RouterFunction<ServerResponse> agencyRoutes(AgencyController agencyController) {
return RouterFunctions
.route(RequestPredicates.POST("/agency").and(RequestPredicates.accept(MediaType.APPLICATION_JSON)), agencyController::createAgency);
}
}
Controller/Handler class below
#Component
public class AgencyController {
public Mono<ServerResponse> createAgency(ServerRequest request){
return ServerResponse.ok()
// .contentType(MediaType.APPLICATION_JSON)
.body(
Flux.just("Test", "message")
.delayElements(Duration.ofSeconds(1)).log(), String.class
);
}
}
Test class
#AutoConfigureWebTestClient
public class AgencyControllerTest {
#Autowired
private WebTestClient webClient;
#Test
void testCreateAgency() {
AgencyRequest request = new AgencyRequest(new AgencyRequestFields("TestName", true));
webClient.post()
.uri("/agency")
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(request))
.exchange()
.expectStatus().is2xxSuccessful();
}
}
build.gradle dependencies
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
//implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.flywaydb:flyway-core'
implementation 'org.springdoc:springdoc-openapi-webflux-ui:1.5.2'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
testImplementation 'org.testcontainers:junit-jupiter'
testImplementation 'org.testcontainers:postgresql'
}
Thanks in advance!!!
The easiest way is just to add #SpringBootTest on your AgencyControllerTest class (along with #AutoConfigureWebTestClient which you already have):
#SpringBootTest
#AutoConfigureWebTestClient
public class AgencyControllerTest {
...which sets up full auto-configuration along with your web test client. Then you don't need to do anything else - all your controllers, routes etc. will be available. This is probably easiest to remember if you're just getting started.
Alternatively, you can use #WebFluxTest and #ContextConfiguration to just instantiate the context you need for this particular test:
#WebFluxTest
#AutoConfigureWebTestClient
#ContextConfiguration(classes = {AgencyController.class, AgencyRouter.class})
public class AgencyControllerTest {
If you have other routes, controllers, beans, etc. set up and only need a small subset then this approach is more efficient, as you're not setting up and tearing down the entire context for each test (only what you need.)
One thing that I am not sure I'm doing correctly is annotating with the Bean and Autowired annotations. I have little understanding of what those do at the moment.
I'd recommend taking a good look at dependency injection & inversion of control (both theoretically and in a Spring context) - this is pretty much the foundation of Spring, and you'll almost certainly come unstuck at some point unless you have (at least) a base level understanding of this.

Using lombok dependency in my spring-boot project but the getter method yields error at runtime (built successfully though)

I am developing a spring-boot project using Gradle as the build tool on ItelliJ IDE.
I have the dependency of lombok declared in gradle.build:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web:2.5.3'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
compileOnly 'org.projectlombok:lombok:1.18.20'
}
I have a model class:
import lombok.Data;
import java.math.BigDecimal;
#Data
public class ProductModel {
private String name;
private BigDecimal price;
private Integer quantity;
}
As you can see I have annotated with #Data.
My controller has the method to handle a POST request, its payload is mapped to the ProductModel:
#PostMapping
public String createProduct(#RequestBody ProductModel productPayload) {
// Runtime error: error: cannot find symbol, 'getName' in 'ProductModel'
productPayload.getName();
}
I know I need to install the lombok plugin on my IntelliJ IDE in order to avoid compiler error on the getter method. So I did that. But when I run my application I get error:
error: cannot find symbol
symbol: method getName()
location: variable productPayload of type CreateProductRestModel
I also tried change the dependency from compileOnly to implementation:
implementation 'org.projectlombok:lombok:1.18.20'
It doesn't help. Why is that? What am I missing?
(I have enabled annotationProcessor on my IntelliJ too)
In order for Gradle to pick up on annotation processors, they have introduced a separate configuration that will generate all the new code ahead of the "normal" compilation.
For Lombok, it would look something like this:
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.20'
annotationProcessor 'org.projectlombok:lombok:1.18.20'
testCompileOnly 'org.projectlombok:lombok:1.18.20'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.20'
}

#DataJpaTest updating actual data in case of MySQL but working fine with H2

I am learning #DataJpaTest, my test case is as below
import com.demo.mockito.entity.StudentEntity;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
#DataJpaTest
#AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class StudentRepositoryTest {
#Autowired
private StudentRepository studentRepository;
#Test
public void findAll() {
StudentEntity student1 = new StudentEntity("shrikant", new Date());
studentRepository.save(student1);
List<StudentEntity> entityList = studentRepository.findAll();
assertEquals(1, entityList.size());
}
}
it's giving me the error
expected: <1> but was: <33>
Expected :1
Actual :33
<Click to see difference>
because right now there are 33 records in DB, and with every save test case, it increases.
src/main/test/application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/student_db?jdbcCompliantTruncation=false&sessionVariables=sql_mode='NO_ENGINE_SUBSTITUTION'&useSSL=false&useServerPrepStmts=false&rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.generate-ddl=true
spring.jpa.database.schema=student_db
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '2.2.6.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
}
group 'com.demo.mockito'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testRuntime('org.junit.jupiter:junit-jupiter-engine:5.2.0')
runtime 'mysql:mysql-connector-java'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
if i use h2 instead, it gives correct result beucase every time it recreates a new instance with no data.
is this intended behavior? or am I doing something wrong,
is h2 standard in case of database testing.
but I don't want to configure another database in my application when my intention is to test MySQL only.
if i use h2 instead, it gives correct result because every time it
recreates a new instance with no data.
You answered yourself to your question.
Bu default, at each Spring Boot container starts (which happens when you define a test with #SpringBootTest or the test slicing #DataJpaTest annotation), a new database instance is created when you use an in-memory DB such as H2 (that is the default behavior of H2 that you may change) while when you use MySQL, Spring Boot doesn't use that strategy by default. It does not change the DB content.
The official doc states indeed :
Spring Boot chooses a default value for you based on whether it thinks
your database is embedded. It defaults to create-drop if no schema
manager has been detected or none in all other cases.
About :
but I don't want to configure another database in my application when
my intention is to test MySQL only.
For unit tests you want to use in-memory DB as H2 because that is straight and doesn't require a long/complex setup (that is populating/cleaning DB state).
For integration tests you want to use the target DB (here MySQL) because you want to write tests that are the closest possible of your application behavior.
To achieve that, you have to use a specific DB (a test database), you also have to populate fixture data for the tests and at last you have to clean data to make tests to be reproducible.
Both kinds of tests are complementary.

Could not autowire JobLauncherTestUtils

I am attempting to test a simple spring batch application.
Using the Spring Batch documentation as a guide (found here), I have created the following test class:
import org.junit.runner.RunWith;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.batch.test.context.SpringBatchTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.jupiter.api.Assertions.assertNotNull;
#SpringBatchTest
#RunWith(SpringRunner.class)
#ContextConfiguration(classes = BatchConfig.class)
class BatchConfigTest {
#Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
#Test
void userStep() {
assertNotNull(jobLauncherTestUtils, "jobLauncherTestUtils should not be null");
}
}
According to docs #SpringBatchTest should inject the JobLaucherTestUtils bean. However, when I run the test, the assertion fails. I have also tried defining the bean in an inner configuration class and had the same result:
static class TestConfiguration {
#Autowired
#Qualifier("userJob")
private Job userJob;
#Bean
public JobLauncherTestUtils jobLauncherTestUtils() {
JobLauncherTestUtils utils = new JobLauncherTestUtils();
utils.setJob(userJob);
return utils;
}
}
Is there something I'm missing? The full source code can be found here.
I am using Spring Batch v4.2.0 and JUnit 5
You are using #RunWith(SpringRunner.class) which is for JUnit 4. You need to use #ExtendWith(SpringExtension.class) for JUnit 5 tests:
#SpringBatchTest
#ExtendWith(SpringExtension.class)
#ContextConfiguration(classes = BatchConfig.class)
class BatchConfigTest {
// ...
}
I had a same problem with Spring Batch 4.1.3 and JUnit 4.12.
Replacing #SpringBatchTest with #SpringBootTest solved the problem.
I was seeing the same error in IntelliJ and spent ages looking into why before I ran the test. It was being injected just fine, IntelliJ was incorrectly telling me the bean wasn't there.
:facepalm:
Posting this in case someone faces the same issue.

Spring #DataJpaTest with JUnit 5

There doesn't seem to be a specific standard way I can find online that makes #DataJpaTest to run correctly.
Is it true that #DataJpaTest is not being used nowadays and all tests are run at the service or controller level using #SpringBootTest?
#Repository
public interface MyBeanRepository extends JpaRepository<MyBean, Long> {
}
#Configuration
#EnableJpaRepositories("com.app.repository.*")
#ComponentScan(basePackages = { "com.app.repository.*" })
public class ConfigurationRepository {
}
#Entity
public class MyBean {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "id")
private Long id;
#Version
#Column(name = "version")
private Integer version;
#NotNull
#Size(min = 2)
private String name;
}
#DataJpaTest
public class MyBeanIntegrationTest {
#Autowired
MyBeanRepository myBeanRepository;
#Test
public void testMarkerMethod() {
}
#Test
public void testCount() {
Assertions.assertNotNull(myBeanRepository , "Data on demand for 'MyBean' failed to initialize correctly");
}
}
Running using the Eclipse->Run Junit shows these logs.
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:73)
Running using gradle test shows the error that init failed.
FAILURE: Build failed with an exception.
* What went wrong:
Test failed.
Failed tests:
Test com.app.repository.MyBeanIntegrationTest#initializationError (Task: :test)
Here is the gradle script.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin")
}
}
plugins {
id 'org.springframework.boot' version '2.1.5.RELEASE'
id 'java'
id 'eclipse'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.app'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenLocal()
jcenter()
mavenCentral()
}
test {
useJUnitPlatform()
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
runtimeOnly 'org.hsqldb:hsqldb'
testImplementation ('org.springframework.boot:spring-boot-starter-test') {
// exlcuding junit 4
exclude group: 'junit', module: 'junit'
}
/**
Test Dependencies Follows
**/
// junit 5 api
testCompile "org.junit.jupiter:junit-jupiter-api:5.2.0"
// For junit5 parameterised test support
testCompile "org.junit.jupiter:junit-jupiter-params:5.2.0"
// junit 5 implementation
testRuntime "org.junit.jupiter:junit-jupiter-engine:5.2.0"
// Only required to run junit5 test from IDE
testRuntime "org.junit.platform:junit-platform-launcher"
}
EDIT:
This has been solved and committed at the same repository.
https://github.com/john77eipe/spring-demo-1-test
It was a good idea to add a Github link. I can see following issues there:
1) If you can't have a main class annotated with #SpringBootApplication, you can use:
#SpringBootConfiguration
#EnableAutoConfiguration
#ComponentScan(basePackages = "com.app.repository")
public class MySampleApplication {
}
2) Change annotations over your ConfigurationRepository class to:
#EnableJpaRepositories("com.app.repository")
#ComponentScan(basePackages = { "com.app.repository" })
public class ConfigurationRepository {
That should let us proceed to the next point:
3) Your MyBeanIntegrationTest should be annotated as:
#SpringBootTest(classes = MyAppApplication.class)
public class MyBeanIntegrationTest {
4) In application.yml you have a small issue with indentation in the last line. Convert tab so spaces and it should be fine.
5) Next thing is MyBeanRepository interface. You can't use a method named findOne there. Thing is, that in interfaces marked as JpaRepository or CrudRepository and so on, methods names need to follow certain rules. If you mark that it will be a repository containing type MyBean your method name should be changed to findById, because Spring will look for a property named id in your bean. Naming it by findOne will cause test to fail with:
No property findOne found for type MyBean!
After fixing these things, your tests pass on my env.
I hope this helps!

Resources