junit

JUnit for void delete method

断了今生、忘了曾经 提交于 2021-01-29 13:49:45
问题 I am new to JUnit and trying to learn. How can we write a JUnit for a void method. I have a delete method which is void. I am writing the test like below but its not working. Please suggest. Method for which I have to write test case. public class DoseService { @Autowired private DoseRepository doseRepo; public void deleteDose(int id) { doseRepo.deleteById(id); } } My Test @Autowired private DoseService doseService; @MockBean public DoseRepository doseRepository; @Test public void

Unable to create JUnit Test for Spring Boot Controller

China☆狼群 提交于 2021-01-29 09:36:56
问题 I am trying to write a test for a simple Controller in SpringBoot application. However, I am receiving errors due to bean creations for my TopicRepository and TopicController. I had referenced a tutorial and am little new to Spring boot development so not sure exactly how it works. How can I make the test work? ControllerTest @RunWith(SpringRunner.class) @WebMvcTest(TopicController.class) public class TopicControllerTest { @Autowired private MockMvc mvc; @MockBean private TopicService

RestTemplate Mock throws NullPointerException

夙愿已清 提交于 2021-01-29 08:20:35
问题 I have a rest template that makes a call in a method in a service class like so: public CustomerResponse someMethod() { CustomerResponse response = restTemplate.exchange(url, HttpMethod.GET, null, CustomerRes.class).getBody(); return response; } When trying to mock the restTemplate in my test class, it keeps throwing a NullPointerException on the line where the mock restTemplate is called: public void checkResponseIsNotNull() { CustomerResponse customerResponseMock = mock(CustomerResponse

Actions click script - Selenium

别说谁变了你拦得住时间么 提交于 2021-01-29 08:01:10
问题 The code below is a selenium script I created with the Selenium IDE, I'm trying to get it to click 250 pixels from the technology button which should click the 'education' tab. You can see by running the script that the 'education' tab is highlighted as if its moused over but the console gives the error: org.openqa.selenium.WebDriverException: Element is not clickable at point (753.5, 107.51666259765625). Other element would receive the click Using: FireFox 45.0.0.1 Selenium Webdriver 2.53.1

JUnit: mock a method called inside another method

馋奶兔 提交于 2021-01-29 07:11:54
问题 Consider I have a class Tournament with methods register() and isAlreadyRegistered() . Below is the sample code. public class Tournament { private boolean register(String teamName) { if(!isAlreadyRegistered(teamName)) { // register team return True; } return False; } private boolean isAlreadyRegistered(String teamName) { // Check if team is already registered, involves DB calls } public static void main(String[] args) throws Exception { Tournament tournament = new Tournament(); tournament

How to pass a Input and Expected file name dynamically for BeforeAll Method | Junit 5

血红的双手。 提交于 2021-01-29 06:08:02
问题 I have Implemented a test cases to check the Input and expected Json files are same. @BeforeAll static void setUp() throws IOException { inputList = readInput(CommonTestConstants.FilePath + "/Input1.json"); expectedList = readExpected(CommonTestConstants.FilePath + "/Expected1.json"); assertEquals("Checking size of both list", inputList.size(), expectedList.size()); } static Stream<Arguments> Arguments() { return IntStream.range(0, inputList.size()) .mapToObj(i -> Arguments.of(inputList.get(i

@WebMvcTest creating more than one Controller for some reason

你离开我真会死。 提交于 2021-01-29 06:06:34
问题 I'm trying to create a controller test with @WebMvcTest , and as I understand, when I put @WebMvcTest(ClientController.class) annotation of the test class it should not create a whole lot of beans, but just ones that this controller requires. I'm mocking the bean this controller requires with @MockBean , but somehow it fails with an exception that there's 'No qualifying bean' of another service that does not required by this controller but by another. So this test is failing: @RunWith

一文让你快速上手 Mockito 单元测试框架

£可爱£侵袭症+ 提交于 2021-01-29 04:36:01
前言 在计算机编程中,单元测试是一种软件测试方法,通过该方法可以测试源代码的各个单元功能是否适合使用。为代码编写单元测试有很多好处,包括可以及早的发现代码错误,促进更改,简化集成,方便代码重构以及许多其它功能。使用 Java 语言的朋友应该用过或者听过 Junit 就是用来做单元测试的,那么为什么我们还需要 Mockito 测试框架呢?想象一下这样的一个常见的场景,当前要测试的类依赖于其它一些类对象时,如果用 Junit 来进行单元测试的话,我们就必须手动创建出这些依赖的对象,这其实是个比较麻烦的工作,此时就可以使用 Mockito 测试框架来模拟那些依赖的类,这些被模拟的对象在测试中充当真实对象的虚拟对象或克隆对象,而且 Mockito 同时也提供了方便的测试行为验证。这样就可以让我们更多地去关注当前测试类的逻辑,而不是它所依赖的对象。 1 生成 Mock 对象方式 要使用 Mockito,首先需要在我们的项目中引入 Mockito 测试框架依赖,基于 Maven 构建的项目引入如下依赖即可: <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>3.3.3</version> <scope>test</scope> </dependency>

Hibernate isolated integration tests

為{幸葍}努か 提交于 2021-01-29 02:39:01
问题 I'm a little bit new to hibernate, so I started with simple things. According to F.I.R.S.T test principles, unit tests must be I - isolated. I'm trying to apply it to integration tests for repository layer (Hibernate\JPA) using @Transactional annotation: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = RepositoryConfig.class) @Transactional public class EmployeeRepositoryTest extends AbstractRepositoryTest { @Autowired private IEmployeeRepository employeeRepository;

Get current Spring transaction in JUnit and commit it

梦想的初衷 提交于 2021-01-28 21:44:15
问题 is it possible to get current Spring transaction in JUnit? I want to test JPA in DAO. And DAO do not have transactions. In tests spring create transaction for every method. I want transaction to be commited somewhere in the middle of test. Need it to test DB status. Only solution I found is to create @Rule for every method. 回答1: You can use TestTransaction in order to manage spring test transactions: TestTransaction.flagForCommit(); TestTransaction.end(); This will flag the current