Merge pull request #11857 from eugenp/BAEL-5336-v2
remove duplicate code from old module BAEL-5336
This commit is contained in:
commit
c591c1d448
|
@ -1,3 +0,0 @@
|
|||
### Relevant Articles:
|
||||
|
||||
- [Spring @Autowired Field Null – Common Causes and Solutions](https://www.baeldung.com/spring-autowired-field-null)
|
|
@ -1,24 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-5-autowiring-beans</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-5-autowiring-beans</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -1,14 +0,0 @@
|
|||
package com.baeldung.autowiring;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@SpringBootApplication
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(App.class, args);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
package com.baeldung.autowiring.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
import com.baeldung.autowiring.service.MyService;
|
||||
|
||||
@Controller
|
||||
public class CorrectController {
|
||||
|
||||
@Autowired
|
||||
MyService myService;
|
||||
|
||||
public String control() {
|
||||
return myService.serve();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package com.baeldung.autowiring.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
import com.baeldung.autowiring.service.MyService;
|
||||
|
||||
@Controller
|
||||
public class FlawedController {
|
||||
|
||||
public String control() {
|
||||
MyService userService = new MyService();
|
||||
return userService.serve();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
package com.baeldung.autowiring.service;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class MyComponent {
|
||||
|
||||
public void doWork() {}
|
||||
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
package com.baeldung.autowiring.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
/**
|
||||
* The bean corresponding to this class is defined in MyServiceConfiguration
|
||||
* Alternatively, you could choose to decorate this class with @Component or @Service
|
||||
*/
|
||||
public class MyService {
|
||||
|
||||
@Autowired
|
||||
MyComponent myComponent;
|
||||
|
||||
public String serve() {
|
||||
myComponent.doWork();
|
||||
return "success";
|
||||
}
|
||||
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
package com.baeldung.autowiring.service;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class MyServiceConfiguration {
|
||||
|
||||
@Bean
|
||||
MyService myService() {
|
||||
return new MyService();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
package com.baeldung.autowiring.controller;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest
|
||||
public class CorrectControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
CorrectController controller;
|
||||
|
||||
@Test
|
||||
void whenControl_ThenRunSuccessfully() {
|
||||
assertDoesNotThrow(() -> controller.control());
|
||||
}
|
||||
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
package com.baeldung.autowiring.controller;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest
|
||||
public class FlawedControllerIntegrationTest {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(FlawedControllerIntegrationTest.class);
|
||||
|
||||
@Autowired
|
||||
FlawedController myController;
|
||||
|
||||
@Test
|
||||
void whenControl_ThenThrowNullPointerException() {
|
||||
NullPointerException npe = assertThrows(NullPointerException.class, () -> myController.control());
|
||||
LOGGER.error("Got a NullPointerException", npe);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue