Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何正确地模仿积垢?_Java_Spring Boot_Mocking - Fatal编程技术网

Java 如何正确地模仿积垢?

Java 如何正确地模仿积垢?,java,spring-boot,mocking,Java,Spring Boot,Mocking,我是春天的新手。我的GCGood类与crudepository一起保存到MySQL数据库中。而且效果很好 现在我尝试编写JUnit测试。当然,我不希望数据库中有任何测试数据。所以我写了我的测试: @RunWith(SpringRunner.class) @SpringBootTest public class GCGoodTests { @MockBean private GCGoodRepository goodRepository; @Test publi

我是春天的新手。我的GCGood类与crudepository一起保存到MySQL数据库中。而且效果很好

现在我尝试编写JUnit测试。当然,我不希望数据库中有任何测试数据。所以我写了我的测试:

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

    @MockBean
    private GCGoodRepository goodRepository;

    @Test
    public void getterSetterTest()
    {
        GCGood testDummy = new GCGod();
        testDummy.setAttribute("Muhaha");
        testDummy.setColor(Color.BLACK);
        testDummy.setName("Maka");
        testDummy = goodRepository.save(testDummy);
        Iterable<GCGood> testDummy2 = goodRepository.findAll();
        assertEquals(testDummy.getAttribute(), "Muhaha");
        assertEquals(testDummy.getColor(), Color.BLACK);
        assertEquals(testDummy.getName(), "Maka");
        assertNotNull(testDummy.getId());
        assert(testDummy.getId()>0);
        assertNotNull(testDummy2);
    }
}
@RunWith(SpringRunner.class)
@春靴测试
公共类考试{
@蚕豆
私有GCGoodRepository goodRepository;
@试验
public void getterSetterTest()
{
GCGood testDummy=新的GCGod();
testDummy.setAttribute(“Muhaha”);
testDummy.setColor(Color.BLACK);
testDummy.setName(“Maka”);
testDummy=goodRepository.save(testDummy);
Iterable testDummy2=goodRepository.findAll();
assertEquals(testDummy.getAttribute(),“Muhaha”);
assertEquals(testDummy.getColor(),Color.BLACK);
assertEquals(testDummy.getName(),“Maka”);
assertNotNull(testDummy.getId());
断言(testDummy.getId()>0);
assertNotNull(testDummy2);
}
}
不幸的是,“testDummy=goodRepository.save(testDummy);”和“Iterable testDummy2=goodRepository.findAll();”返回null,测试甚至没有完成

如果我将@MockBean更改为@Autowire,一切都会正常工作。但是我的数据库中有不必要的数据


那么,我如何在没有数据shmodda的情况下实现corret测试呢?

虽然您已经注入了模拟bean,但实际上您没有提到必须模拟的方法,以及如果您确实调用模拟bean上的某个方法,您会期望得到什么。您可以做的是,只需像这样编写@Before方法:

@Before
public void setup() {
        List<GCGood> gcgoods = Collections.emptyList();
        given(goodRepository.findAll()).willAnswer(gcgoods);
}
@之前
公共作废设置(){
List gcgoods=Collections.emptyList();
给定(goodRepository.findAll()).willAnswer(gcgoods);
}

给定的()和willAnswer()位于BDDMockito中,您可以通过import static org.mockito.BDDMockito.*导入该文件

看起来您想进行集成测试。如果是这样,您应该使用
@DataJpaTest
注释,当测试可以使用内存数据库运行时,该注释为您的环境创建。更多信息,请参阅


这也取决于你想测试什么。如果repository(GCGoodRepository)是您的测试目标,您应该
@Autowired
它,并使用一些内存中的数据库在隔离的环境中测试它,正如我前面提到的。如果您测试某个依赖于某个存储库(例如GCGoodRepository)的服务,您应该将此存储库模拟为@karthi所提到的。

谢谢!“内存数据库”问题就是问题所在。有了@DataJpaTest,一切正常!非常感谢你的帮助。但是我不想只模拟特殊的函数,而是按照lucascode的建议将DB转移到“内存中”!