`spring-test`是Spring Framework的测试模块,为JUnit和TestNG提供单元和集成测试支持,包括用于单元测试的各种模拟和Spring TestContext Framework以及用于集成测试的Spring MVC测试框架基于Spring的应用程序。
需要帮助为服务层方法编写 JUnit 测试用例,该方法有 rest 调用
我的服务层代码: public String fileUpload(Multipart file, String payload, HttpHeaders headers) throws Exception{ MultiValueMap\ map = new LinkedMultiValueMap\<\>()...
我一直在做一个演示 Spring Boot 项目,它提供了一个 REST API 来对一些实体类执行 CRUD 操作。在开发这个 REST API 的过程中,我还创建了 unit 和
Spring 测试:@Sql 直接使用 DataSource 而无需事务
对于 Spring Framework 6 - 出于学术目的,@Repository 直接与数据源一起工作。因此,既不涉及 JdbcTemplate 也不涉及 DataSourceTransactionManager。 @Reposi ...
为什么我的测试没有通过并且我得到 org.opentest4j.AssertionFailedError
我正在参加 spring 学院的构建 RESTFUL CashCard 系列 API 的 spring boot 课程。 但是我在编写测试时得到了意想不到的结果。 这是我的 CashCard 实体类 包com.
我想为此代码创建一个 JUnit 测试: 私人服务列表服务; @RequestMapping(method = {RequestMethod.GET}, path = {"id/{id}"}) 公共响应实体 我想为此代码创建一个 JUnit 测试: private Service listsService; @RequestMapping(method = {RequestMethod.GET}, path = {"id/{id}"}) public ResponseEntity<Object> find(@PathVariable String id) { LookupResponse lookupResponse = listsService.findById(id); return new ResponseEntity<>(lookupResponse, HttpStatus.OK); } ................. @Override public LookupResponse findById(String id) { Optional<Lists> list = ListsRepository.findById(id); if(list.isPresent()) { Lists lists = binList.get(); LookupResponse response = LookupResponse.builder() .countryCode(lists.getCountry()) .category(lists.getType()) .build()) .build(); return response; } return null; } 我试过这个: public class ControllerTest { @MockBean private ListsService listsService; @Autowired private MockMvc mockMvc; @Test void justAnExample() { LookupResponse lookupResponse = LookupResponse.builder() .type("123456") .build(); Mockito.when(listsService.findById(Mockito.anyString())).thenReturn(lookupResponse); } } 当我运行代码时出现错误: Cannot invoke "com.ListsService.findById(String)" because "this.listsService" is null java.lang.NullPointerException: Cannot invoke "com.ListsService.findById(String)" because "this.listsService" is null 你知道我如何正确实施这个模拟测试吗?
我做了一些更改以激活斜杠编码的 URL,有关详细信息 它工作正常,我想在运行 IT 测试时激活这些更改。 我如何激活 UserserviceApplic 上的这些更改...
doCallRealMethod 不适用于在抽象类中自动装配的对象
我正在尝试为具有超类的子类编写测试基础。我尝试了以下方法,但流程并未进入 enrichEvent 方法。 公共抽象类 StudentProcessor 实现
Spring Boot项目升级到3.01,Java升级到17后出现的问题,pom.xml也改了很多。我不确定手动设置所有版本号是否是个好主意,但是...
在AnnotatinConfigApplicationContext中注入模拟Bean失败。
我的Springboot应用程序没有主类,因为它有一个AWS Lambda handler。这是我的classtobetested的样子。@Slf4j @SpringBootApplication @Configuration @ComponentScan(...)
我想用多个消息来测试Spring Cloud Stream Sink。比方说,我有一个这样的处理器类。@Component @EnableBinding(Processor.class) public class TestProcessor { @Autowired ...
创建名称为'sessionFactory'的Bean时出错。无法实例化默认的tuplizer (SpringJUnit4ClassRunner)
我正在更新一个10多年前的旧webapp 我不得不更新一些Spring和Hibernate的依赖关系,以适应新的JDK和应用服务器。现在,我正试图让其中一个...
java.lang.AssertionError: 内容类型未设置 - Spring Test 和 Mockito
我的测试返回失败,出现错误:java.lang.AssertionError: Content type not set 我读到这个错误并不意味着那么多,因为如果有一个NPE或者不匹配的处理程序,返回的结果是一样的......。
在Assertj 3.16.1中,"hasOnlyElementsOfType "方法是否被废弃?
我的这段代码在更新到Assertj 3.16.1后不再工作了 Throwable thrown = catchThrowable(()-> myObject.methodThrowsException()); assertThat(thrown).isInstanceOf(MyCustomException......)。
我定义了一个类来自动加载我的sprintboot applicatoin中的application-*.properties的属性。@Component @ConfigurationProperties("my-app") @EnableConfigurationProperties @Data ...
我正在考虑在我的集成测试中使用@Sql来清理测试用例前的数据。注释解决了我的用例。唯一需要注意的是,我无法弄清楚在传递......。
我是Junit 5的新手。被测类中有两个函数,第一个函数调用第二个函数,第二个函数返回一个值,该值在第一个函数中用于...
Spring Boot DataJpaTest失败,并出现java.lang.IllegalStateException:原因:给定类型必须是接口
确切地说,取决于我遇到的错误。如果我使用Intellij Maven Install,则会收到此异常(这很奇怪,因为我具有此依赖关系,默认情况下,它应该在spring-starter -...]中出现)>
SpringBootTest,content()函数已随sprint-test 5.2.6一起使用
[我正在尝试执行类似的操作:嘲笑mvc.perform(post(“ / forums / {forumId} / register”,42L).contentType(“ application / json”).param(“ sendWelcomeMail”,“ true” ).content(objectMapper ....
apache httpclient clientprotocol class notfoundexception with gradle with program args
我正在尝试运行gradlew测试-Penv =“ envLocal”,这将引发ClassNotFound:ClientProtocolException,但如果没有-Penv则无法正常工作。有人可以解决这个问题吗?运行弹簧...
由于不满意的依赖关系,SpringBoot应用程序上下文未启动
我已经在如下配置类中配置了一个bean,如下所示:@Configuration public class WebServiceConfiguration {@Bean public Jaxb2Marshaller jaxb2Marshaller(){Jaxb2Marshaller ...