gpt4 book ai didi

Spring 注入(inject) : @MockBean @Repository is not injected

转载 作者:行者123 更新时间:2023-12-04 21:09:12 35 4
gpt4 key购买 nike

我正在尝试 @MockBean 一个 @Repository 注释类:

@Repository
public interface ApplicationDao extends MongoRepository<Application, String> {}

我将它注入(inject)到一个 @Service 注释类中:

@Service
public class AuthorizationService {

private ApplicationDao appsDao;
private List<Application> allowedApplications;

@Autowired
public AuthorizationService(ApplicationDao appsDao) {
this.appsDao = appsDao; //<<MOCKED INJECTED BEAN>>
this.fillApplications();
}

private void fillApplications() {
this.appsDao.findAll() //<<MOCKED method>>
.forEach(entry -> {
this.allowedApplications.put(entry.getName(), entry);
});
}

public bool isAuthorized(Application application) {
return this.allowedApplications
.stream()
.anyMatch(app -> app.getId().equals(application.getId()));
}
}

我的测试模拟配置如下:

@RunWith(SpringRunner.class)
@SpringBootTest()
public class GroupReferencesTest {

private @Autowired AuthorizationService;
private @MockBean ApplicationDao applicationDao;

@Before
public void setUp() {

Application testApplication = new Application();
testApplication.setName("test-application");
List<Application> allowedApplications = new ArrayList<Application>();
allowedApplications.add(testApplication);

Mockito
.when(this.applicationDao.findAll())
.thenReturn(allowedApplications);
}

@Test
public void test() {
Application app = new Application();
app.getId("test-application");
assertTrue(this.authorizationService.isAuthorized(app)); //<<FAILS>>
}
}

然而,我的模拟对象没有被注入(inject)。我的意思是,当我的 AuthorizationService 调用其注入(inject)的 ApplicationDao 时,返回一个空列表而不是我的模拟列表。

我也尝试过使用 @MockBean(name="applicationDao")。行为相同。

我还尝试使用此代码配置我的模拟 bean:

@TestConfiguration
public class RestTemplateTestConfiguration {

@Bean("applicationDao")
@Primary
public static ApplicationDao mockApplicationDao() {
ApplicationDao mock = Mockito.mock(ApplicationDao.class);

Application testApplication = new Application();
testApplication.setName("test-application");
List<Application> allowedApplications = new ArrayList<Application>();
allowedApplications.add(testApplication);

Mockito
.when(mock.findAll())
.thenReturn(allowedApplications);

return mock;
}
}

但是,它并不能正常工作。

Application 类是:

public class Application {
private String id;

//setters & getters
}

有什么想法吗?

最佳答案

首先要做的是测试的类型。答:单元测试。

您正在启动管理 AuthorizationService 生命周期的 Spring 上下文,然后您正在尝试注入(inject)模拟。真正发生的是 Spring IoC 容器正在将一个真正的 ApplicationDao(由 Spring IoC 容器管理的那个)注入(inject)到 AuthorizationService 中。

解决方案:

通过您的测试运行器管理 AuthorizationService 的生命周期(例如 MockitoJUnitRunner 并将 ApplicationDao 模拟注入(inject)其中):

@RunWith(MockitoJUnitRunner.class)
public class GroupReferencesTest {

private @InjectMocks AuthorizationService authorizationService;
private @Mock ApplicationDao applicationDao;

@Before
public void setUp() {

Application testApplication = new Application();
testApplication.setName("test-application");
List<Application> allowedApplications = new ArrayList<Application>();
allowedApplications.add(testApplication);

Mockito
.when(this.applicationDao.findAll())
.thenReturn(allowedApplications);
}

@Test
public void test() {
Application app = new Application();
app.getId("test-application");
assertTrue(this.authorizationService.isAuthorized(app));
}
}

关于 Spring 注入(inject) : @MockBean @Repository is not injected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52992222/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com