当前位置: 首页 > 知识库问答 >
问题:

我如何模拟一个bean的一些方法调用,但在同一测试类中的其他方法调用中使用定义的bean?

宰父单弓
2023-03-14

我已经尝试在测试类中的DAO Bean上使用@spy注释。我尝试在DAO bean上使用@mockbean。我尝试使用Mockito的doReturn和when功能来覆盖DAO的默认实现,但是,我总是从TestConfiguration中定义的测试DAO实现中返回结果。

我确实更改了我正在做的事情的文本,因为它是公司代码,但这正是我要做的。

我的TestConfiguration是这样定义

@TestConfiguration
public class TestAppConfiguration {

    @Bean
    public PersonService personService() {
        return new PersonServiceImpl(personDao());
    }  

    @Bean
    public PersonDao personDao() {
        return new TestPersonDaoImpl();
    }
}

public class PersonServiceImpl implements PersonService {
    private Logger logger = LoggerFactory.getLogger(PersonServiceImpl.class);

    private PersonDao personDao;

    public PersonServiceImpl(PersonDao personDao){
        this.personDao = personDao;
    }

    @Override
    public List<PersonDto> getAllPersons() {
        return personDao.findAll().stream()
                .map(person -> PersonDtoFactory.getPersonDto(person))
                .collect(Collectors.toList());
    }

    @Override
    public PersonDto getPersonById(Long id) {
       return PersonDtoFactory.getPersonDto(personDao.findById(id));

    }
}
public class TestPersonDaoImpl implements PersonDao {

    @Override
    public List<PersonModel> findAll() {
        return getPersons();
    }

    @Override
    public List<PersonModel> findById(id) {
        return getPersons().stream()
                       .filter(person -> person.getId() == id)
                                   .collect(Collectors.toList());
    }

    private List<PersonModel> getPersons() {
        List<PersonModel> personList = new ArrayList<>();
        personList.add(new PersonModel(1L, "Susan");
        personList.add(new PersonModel(2L, "Jerry");
        personList.add(new PersonModel(3L, "Tom");
        return personList;
    }
}
@RunWith(SpringRunner.class)
@Import(TestAppConfiguration.class)
public class PersonServiceTests {
    //We won't test web socket functionality in this test class.
    @Autowired
    private PersonService personService;

    @MockBean //want to overwrite in the test only when specified in the test, otherwise, use default TestPersonDaoImpl bean.
    private PersonDao personDao;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void getAllPersons() {
        assert(personService.getAllTests().size() > 0);
    }

    @Test
    public void getPersonById() {
        assert(personService.getPersonById(1L).getName().equals("Susan"));
    }

    @Test
    public void getAllPersons_NoPersons(){
        //Mock the DAO call since it will have all of the test data by default
        doReturn(new ArrayList<Person>()).when(personDao).findAll();
        //when(personDao.findAll()).thenReturn(new ArrayList<>());  <-- this also doesn't work
        assert(personService.getAllPersons().size() == 0);
}

期望所有测试都通过,并且在服务实现中调用DAO调用时会被覆盖。实际结果是前两个测试通过,第三个测试失败,因为它没有覆盖dao调用。

共有1个答案

叶鹭洋
2023-03-14

使用@mockbean您将得到一个注入的模拟实例

使用@spy,您的dao将不会被注入到服务中

您需要@spybean..您将在默认情况下获得注入和所有方法的实现时调用。

 类似资料:
  • 问题内容: 在Bruce Eckel的“ Thinking In Java,第四版”的第428页(有关类型信息的章节)中,具有以下示例: 也许我有点累,但是我看不到add()方法中对add()的调用是如何工作的。我一直认为它应该有一个引用,或者是一个静态方法(并且我在ArrayList或List中找不到静态add())。我想念什么? 我只是为自己测试,发现这可行: 问题答案: Java为这样的方法

  • 问题内容: 所以我基本上想做的很简单 由于某种原因,它无法正常工作。在我的Javascript控制台(Chrome浏览器)中 编辑1:我已经添加了实际的代码,如您所见,我在构造函数中绑定了validateEmail 问题答案: 您的方法已正确定义,因此问题出在如何 调用上 。 您以一种设置为实例以外的方式调用它。这在事件侦听器中很常见。我想您的代码中有一些类似的代码: React 的推荐解决方案是

  • 我目前正在为一个groovy应用程序编写单元testcase 有人能告诉我这是不是嘲弄斯波克的两个电话的正确方法?如果没有,那么请引导我走向正确的解决方案。

  • 问题内容: 我正在为具有两个方法methodA,methodB的类编写JUnit测试用例。我想在测试用例中模拟对methodA的methodB调用,我正在对正在测试的类使用间谍,但仍然执行methodB。 这是课程 这是测试课 尽管我已对其进行了模拟,但方法B仍会得到调用。请向我建议一个使用正确方法模拟同一类methodA的methodB调用的解决方案。 问题答案: 我昨天碰到这个,因为间谍最好做

  • 问题内容: 我是python的新手。我试图在类中将值从一种方法传递给另一种方法。我搜索了该问题,但无法获得适当的解决方案。因为在我的代码中,“ if”正在调用类的方法“ on_any_event”,而该方法反过来应该调用我的另一个方法“ dropbox_fn”,该方法利用了“ on_any_event”中的值。如果“dropbox_fn”方法在类之外,它将起作用吗? 我将用代码说明。 这里的主要问

  • 实际上,它的测试如果eat方法对宠物有效,但我也需要检查feedPet方法对玩家也有效。 任何想法或建议都非常感谢。