我研究了StackOverflow的类似问题,但在我的案例中找不到根本原因。
上下文:
我在SpringBoot2
中有@RestController
依赖于BookSearcherService
。我想模拟booksearcherservice
以便单元测试Controller。
代码:
BookSearcherController.java
:
@RestController
@RequestMapping("/book")
@RequiredArgsConstructor
public class BookSearcherController {
private BookSearcherService bookSearcherService;
@GetMapping(path = "/list", produces = { "application/json" })
@ResponseBody
public List<BookDto> listAll() {
return bookSearcherService.listAll();
}
}
BookSearcherService.java
:
@Service
public class BookSearcherService {
private BookSearcherCrudService bookSearcherCrudService;
@Autowired
void setBookSearcherRepository(BookSearcherCrudService bookSearcherCrudService) {
this.bookSearcherCrudService = bookSearcherCrudService;
}
private BookSearcherRepository bookSearcherRepository;
@Autowired
void setBookSearcherRepository(BookSearcherRepository bookSearcherRepository) {
this.bookSearcherRepository = bookSearcherRepository;
}
public List<BookDto> listAll() {
List<Book> books = bookSearcherRepository.findAll();
return books.stream()
.map(bookSearcherCrudService::toDto)
.collect(Collectors.toList());
}
}
@WebMvcTest(controllers = BookSearcherController.class)
public class BookSearcherControllerMockMvcTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private BookSearcherService bookSearcherService;
@Autowired
private ObjectMapper objectMapper;
@Test
public void canListAll() throws Exception {
assertThat(bookSearcherService).isNotNull();
assertThat(mockMvc).isNotNull();
List<BookDto> books = asList(new BookDto("Title 1"), new BookDto("Title 2"), new BookDto("Title 3"));
given(bookSearcherService.listAll()).willReturn(books);
ResultActions perform = mockMvc.perform(get("/book/list"));
}
}
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.search.book</groupId>
<artifactId>book-searcher</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>book-searcher</name>
<description>Project for books search</description>
<properties>
<java.version>1.8</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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<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>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
问题是booksearcherservice
没有被注入到booksearchcontroller
中,因为它使用了@requiredargsconstructor
,在本例中,它没有创建正确的构造函数。
@requiredargsconstructor
文档指出,将使用未初始化的final
或@nonnull
字段的参数创建构造函数。但是,booksearcherservice
两者都不是,因此它不作为构造函数参数添加。
因此,尽管模拟本身不是空的,但它从未被注入到被测试的控制器中。
controller.java UserServiceImpl.java 我得到了这个错误 应用程序启动失败 描述: 我使用的SpringBoot版本:2.1.0.发行版
我正在跑步。带有vue的net core应用程序。js作为SPA 突然间,我在从cetain客户端访问API时遇到了问题。在现成的示例API上执行GEThttp://localhost:63861/api/SampleData/WeatherForecasts 在Postman中,我收到了带有html和此消息的200OK 很抱歉,没有启用JavaScript,ClientApp无法正常工作。请使其
问题内容: 我正在随处使用,并且可以在Firefox上正常使用。它不再与IE9一起工作,也不再在IE8中工作。我能做什么? 问题答案: 为什么要依赖具有对象的浏览器,而不是只包含Douglas Crockford的脚本文件。您可以在这里找到缩略文件:http : //www.json.org/js.html 导入后,您不必担心浏览器中现有的方法。
使用objectClass“inet orgPerson”和“person”在people组中创建一个person。并命名为“Joe” 创建一个名为“group of Names”的组,名为“Agent”。成员属性的值为'uid=joe,ou=people,dc=company,dc=com' 现在,当我查看person'Joe'时,我找不到Apache ldap Studio中启用了'Shoin