You can use it immediately if you use Mockito, but I wrote it because I wanted to make a note.
Java9 Spring-boot 2.0.0.RELEASE
Rewrite the Config set in the Service class from Junit instead of reading the test Config file.
pom.xml It is written in other articles, but I will always check it, so I will write it in the meantime.
<?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>
<groupId>com.tasogarei</groupId>
<artifactId>junit-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>junit-test</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>9</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.0.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Service There is a mysterious private method, but I just want to try something else after this. please do not worry.
package com.tasogarei.app.service;
import java.util.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tasogarei.app.util.TestConfig;
@Service
public class TestService {
@Autowired
private TestConfig config;
public String getString() {
return getStr();
}
private String getStr() {
return config.getTest();
}
}
config This time, we will do it based on the Configuration annotation.
package com.tasogarei.app.util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestConfig {
@Value("${app.test}")
private String test;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
}
application.properties
app.test=configtest
Put an appropriate value in ʻapplication.properties`.
If you convert it to Mock with @ MockBean
and do the content you want to rewrite withMockito.when (). Then ()
, the one that was converted to Mock in the test will be used, so this is okay.
Even if the return value of the method specified by when
is a class, if you put the class you want to rewrite with then
, that class will be used, so you can test with it.
Of course the types must be the same.
package com.tasogarei.app.service;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Properties;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import com.tasogarei.app.util.TestConfig;
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class TestServiceTest {
@Autowired
private TestService testService;
@MockBean
private TestConfig testConfig;
@Test
public void test() {
Mockito.when(testConfig.getTest()).thenReturn("hogehoge123");
assertEquals(testService.getString(), "hogehoge123");
}
}
I feel that rewriting with the test method is subtle, so I think that it is better to do what can be done in advance processing there.
Recommended Posts