1. Requirements
1. Certain IP addresses and ports are restricted in the project, and only the content (ip) written in the configuration file can access the project.
2. When running the test case, ensure that the class (CbeConfig) that reads the ip and port in the configuration file must be run in advance.
Second, work
1, the following test will definitely not work
@Test
public void getCbeTest(){
CbeConfig cbeConfig = new CbeConfig();
System.out.println("IP is" + cbeConfig.getIp());
System.out.println("Port is" + cbeConfig.port);
}
2. Make sure that the CbeConfig class exists at the moment the program runs, first write a class that reads the configuration, and read it after the program runs After the configuration, then set the read parameters to another class (CbeConfigAfter), and extract the parameters later. Both use CbeConfigAfter.
CbeConfigBefore class implements the run method of ApplicationRunner interface
package com.example.demo;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class CbeConfigBefore implements ApplicationRunner {
@Value("${cbe.ip}")
public String ip;
@Value("${cbe.port}")
public Integer port;
@Override
public void run(ApplicationArguments applicationArguments) throws Exception {
System.out.println("My first startup");
System.out.println("haha ip" + ip);
System.out.println("haha port" + port);
CbeConfigAfter cbeConfigAfter = CbeConfigAfter.getInstance();
cbeConfigAfter.setIp(ip);
cbeConfigAfter.setPort(port);
System.out.println("Set up");
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
CbeConfigAfter class is written in singleton mode
package com.example.demo;
import lombok.Getter;
import lombok.Setter;
public class CbeConfigAfter {
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
String ip;
int port;
private static CbeConfigAfter cbeConfigAfter;
private CbeConfigAfter (){}
public static synchronized CbeConfigAfter getInstance() {
if (cbeConfigAfter == null) {
cbeConfigAfter = new CbeConfigAfter();
}
return cbeConfigAfter;
}
}
Test class
package com.example.demo.controllerTest;
import com.example.demo.CbeConfigAfter;
import com.example.demo.CbeConfigBefore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class CbeTest {
@Test
public void getCbeByAfterTest(){
CbeConfigAfter instance = CbeConfigAfter.getInstance();
System.out.println("IP is" + instance.getIp());
System.out.println("The port is" + instance.getPort());
}
@Test
public void getCbeBeforeTest(){
CbeConfigBefore cbeConfig = new CbeConfigBefore();
System.out.println("IP is" + cbeConfig.getIp());
System.out.println("Port is" + cbeConfig.port);
}
}
At this time, run the getCbeByAfterTest test class again, OK, done.
Although it is done, I still have doubts. Please comrades who are fortunate enough to find faults after reading. thanks.
@Test
public void getCbeTest(){
CbeConfig cbeConfig = new CbeConfig();
System.out.println("IP is" + cbeConfig.getIp());
System.out.println("Port is" + cbeConfig.port);
}
package com.example.demo;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class CbeConfigBefore implements ApplicationRunner {
@Value("${cbe.ip}")
public String ip;
@Value("${cbe.port}")
public Integer port;
@Override
public void run(ApplicationArguments applicationArguments) throws Exception {
System.out.println("My first startup");
System.out.println("haha ip" + ip);
System.out.println("haha port" + port);
CbeConfigAfter cbeConfigAfter = CbeConfigAfter.getInstance();
cbeConfigAfter.setIp(ip);
cbeConfigAfter.setPort(port);
System.out.println("Set up");
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
package com.example.demo;
import lombok.Getter;
import lombok.Setter;
public class CbeConfigAfter {
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
String ip;
int port;
private static CbeConfigAfter cbeConfigAfter;
private CbeConfigAfter (){}
public static synchronized CbeConfigAfter getInstance() {
if (cbeConfigAfter == null) {
cbeConfigAfter = new CbeConfigAfter();
}
return cbeConfigAfter;
}
}
package com.example.demo.controllerTest;
import com.example.demo.CbeConfigAfter;
import com.example.demo.CbeConfigBefore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class CbeTest {
@Test
public void getCbeByAfterTest(){
CbeConfigAfter instance = CbeConfigAfter.getInstance();
System.out.println("IP is" + instance.getIp());
System.out.println("The port is" + instance.getPort());
}
@Test
public void getCbeBeforeTest(){
CbeConfigBefore cbeConfig = new CbeConfigBefore();
System.out.println("IP is" + cbeConfig.getIp());
System.out.println("Port is" + cbeConfig.port);
}
}