我读过几篇文章,看过一些视频,我想我已经掌握了Mockito的工作原理,但我仍然无法对我的控制器进行任何测试。
首先,我有以下服务类,它执行对外部API的请求:
@Service
public class MovieService {
RestTemplate restTemplate = new RestTemplate();
public MovieResponse getMovieResponse() {
ResponseEntity<MovieResponse> responseEntity = restTemplate.
我得到了 org.junit.jupiter.api.extension.ExtensionContext.getRequiredTestInstances()Lorg/junit/jupiter/api/extension/TestInstances;at org.mockito.junit.jupiter.MockitoExtension.beforeEach(MockitoExtension.java:143) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$null$0(TestMethod
在尝试使用测试类中的反射设置变量时,我获得了一个NullPointerException。
Cities.java使用@Value注释从application.properties初始化文件名,application.properties应该通过反射初始化文件名,而不是使用NPE。
我做错什么了?
Cities.java
@Component
public class Cities
{
private final List<City> cities;
@Value("${filename.Cities}")
private String fi
我正在学习一个关于服务单元测试的教程,并且我正在尝试使用JUnit5而不是JUnit4 (这就是本教程使用的)。当我完全按照教程使用JUnit4时,测试运行得很好,但是我使用JUnit5得到了一个NullPointerException。 存储库: public interface CourseRepo extends CrudRepository<Course, Long> {} 服务: @Service
public class CourseService {
private final CourseRepo courseRepo;
public Cour
我正在尝试测试服务类中的一个方法,该方法使用ModelMapper将dto转换为实体,但我在这一行中获得了NullPointerException。我该怎么解决呢?
异常
java.lang.NullPointerException: Cannot invoke "org.modelmapper.ModelMapper.map(Object, java.lang.Class)" because the return value of "com.where.where.mapper.ModelMapperService.forRequest()" is null
使用Spring Boot 2.0.0.RC2。
我已经编写了一个配置类:
@Configuration
@ConditionalOnProperty("launchdarkly.sdkKey")
public class LDClientConfiguration {
@Bean
public LDClientInterface ldClient(LDClientConfigurationProperties props) {
return new LDClient(props.getSdkKey(), props.getLDConfig(
对于Java14和mvn 5,如果我将mockito- JUnit -jupiter添加到我的构建中,即使在任何测试中没有使用junit,也会在尝试运行mvn test时抛出错误。没有mockito-junit-jupiter,一切都很好。除Maven外,所有依赖项都是最新版本。 抛出的错误是 # Created at 2020-04-15T16:34:33.816
org.junit.platform.commons.JUnitException: TestEngine with ID 'junit-jupiter' failed to discover tests
我正在尝试在Junit 5测试中注入一个spring数据存储库,但是我得到了
No qualifying bean of type 'com.xxx.core.security.datastore.AccountsRepository' available: expected at least 1 bean which qualifies as autowire candidate
下面是测试结果
package com.xxx.core.security.datastore;
import org.junit.jupiter.api.Assumptions;
import
下面是我的控制器:
@RestController
@RequestMapping("/Employee")
public class Employeecontroller {
@Autowired
Employeeservice empservice;
@PostMapping("/addEmployee") // adding emp details into the table
public Employee addem
我使用SpringBoot2.1.6.RELEASE在Spring中创建了一个简单的方面,它基本上记录了用于方法的总时间。
@Aspect
@Component
public class TimeLoggerAspect {
private static final Logger log = LoggerFactory.getLogger(TimeLoggerAspect.class);
@Around("@annotation(demo.TimeLogger)")
public Object methodTimeLogger(ProceedingJoinPoi
在集成测试中,我有一种奇怪的行为,在使用注释SpringBootTest和自动装配服务时捕获日志。我使用来捕获日志。
使用特定的设置,我无法捕获日志,我无法理解为什么会发生这种情况。我不确定我是否遗漏了弹簧引导测试配置中的某些内容,或者它是否是其他什么东西。
因此,让我们假设存在一个FooService:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@Service
public class FooService {
p
我刚开始测试用例,并尝试用JUnit 5和spring 2学习mokito 2。
No tests found in StudentServiceTest
Is the method annotated with @Test?
Is the method public?
我谷歌了很多。但没有找到任何可行的解决办法。
build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '2.2.6.RELEASE'
id 'io.spr
我使用Post请求添加3个类别,对于每个类别,3个相应的课程被添加到相同的类别中。最后,我需要得到一组包含课程列表的类别。但是,在使用Mockito执行测试驱动开发时,I‘会收到一个空指针异常,状态代码为400。同样也请帮助我。
Test.java
@RunWith(value = MockitoJUnitRunner.class)
@WebMvcTest(Controller.class)
class Test {
@Autowired
MockMvc mvc;
@MockBean
service s;
@MockBean
Control