我正在使用springboot版本2.2.2.RELEASE。
我正在尝试使用junit5添加测试,这是在build.gradle中设置它的方式:
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'junit', module: 'junit'
}
testImplementation "org.junit.jupiter:junit-jupiter-api:5.5.2"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.5.2"
这是我的测试课:
//@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT, classes = UserServiceApplication.class)
@ActiveProfiles("it")
public class UserControllerIntegrationTest {
@Autowired
private TestRestTemplate testRestTemplate;
}
问题是restRestTemplate为null。它唯一有效的方法是在使用时:
@RunWith(SpringRunner.class)
但是,以我的理解,这是对junit4的支持,对吗?
我正在使用不推荐使用的TestRestTemplate(org.springframework.boot.test.web.client.TestRestTemplate)。
我还尝试添加以下内容,但也无法正常工作:
@ExtendWith(SpringExtension.class)
我想念什么?
谢谢。
确保整个测试使用的是与JUnit 5相关的注释。也许您的@Test
注释仍在使用JUnit 4。
The following example works for JUnit 5 and Spring Boot:
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.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = YourApplication.class)
public class TestOne {
@Autowired
private TestRestTemplate testRestTemplate;
@Test
public void test() {
assertNotNull(testRestTemplate);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句