unit-testing

Mocking SecureRandom::nextInt()

安稳与你 提交于 2021-02-08 01:57:37
问题 I have a class that uses a SecureRandom instance and grabs the next random number. Lets say the example is: public class ExampleClass() { public void method() { Random sr = new SecureRandom(); System.out.printf("%d %n", sr.nextInt(1)); System.out.printf("%d %n", sr.nextInt(1)); } } Test code @RunWith(PowerMockRunner.class) public class ExampleClassTest { ... @Test @PrepareOnlyThisForTest(SecureRandom.class) public void mockedTest() throws Exception { Random spy = PowerMockito.spy(new

Mocking SecureRandom::nextInt()

試著忘記壹切 提交于 2021-02-08 01:57:04
问题 I have a class that uses a SecureRandom instance and grabs the next random number. Lets say the example is: public class ExampleClass() { public void method() { Random sr = new SecureRandom(); System.out.printf("%d %n", sr.nextInt(1)); System.out.printf("%d %n", sr.nextInt(1)); } } Test code @RunWith(PowerMockRunner.class) public class ExampleClassTest { ... @Test @PrepareOnlyThisForTest(SecureRandom.class) public void mockedTest() throws Exception { Random spy = PowerMockito.spy(new

test_helper don't load when I make a test

送分小仙女□ 提交于 2021-02-08 01:54:41
问题 I need make a test of a model, but when I do this: ruby test/unit/user_test.rb I have also tried with other methods: ruby -I test test/unit/user_test.rb rake test:units TEST=test/unit/user_test.rb But anyone make the test and said: test/unit/user_test.rb:1:in `require': no such file to load -- test_helper (LoadError) Or like. This is part of the code (only the beginning): require 'test_helper' class UserTest < Test::Unit::TestCase self.use_instantiated_fixtures = true fixtures :users def test

test_helper don't load when I make a test

霸气de小男生 提交于 2021-02-08 01:53:14
问题 I need make a test of a model, but when I do this: ruby test/unit/user_test.rb I have also tried with other methods: ruby -I test test/unit/user_test.rb rake test:units TEST=test/unit/user_test.rb But anyone make the test and said: test/unit/user_test.rb:1:in `require': no such file to load -- test_helper (LoadError) Or like. This is part of the code (only the beginning): require 'test_helper' class UserTest < Test::Unit::TestCase self.use_instantiated_fixtures = true fixtures :users def test

ObjectMapper using TypeReference not working when passed type in generic method

≯℡__Kan透↙ 提交于 2021-02-07 20:53:14
问题 This is the method: protected <T> TestPageResult<T> getTestPageResutForRequest(MockHttpServletRequestBuilder request) throws Exception { String responseJson = mockMvc.perform(request).andReturn().getResponse() .getContentAsString(); TestPageResult<T> response = getObjectMapper().readValue(responseJson, new TypeReference<TestPageResult<T>>() { }); return response; } I call it like this: TestPageResult<SomeDto> pageResult = this.<SomeDto>getTestPageResutForRequest(getRequest()); TestPageResult

ObjectMapper using TypeReference not working when passed type in generic method

怎甘沉沦 提交于 2021-02-07 20:53:04
问题 This is the method: protected <T> TestPageResult<T> getTestPageResutForRequest(MockHttpServletRequestBuilder request) throws Exception { String responseJson = mockMvc.perform(request).andReturn().getResponse() .getContentAsString(); TestPageResult<T> response = getObjectMapper().readValue(responseJson, new TypeReference<TestPageResult<T>>() { }); return response; } I call it like this: TestPageResult<SomeDto> pageResult = this.<SomeDto>getTestPageResutForRequest(getRequest()); TestPageResult

ObjectMapper using TypeReference not working when passed type in generic method

雨燕双飞 提交于 2021-02-07 20:53:00
问题 This is the method: protected <T> TestPageResult<T> getTestPageResutForRequest(MockHttpServletRequestBuilder request) throws Exception { String responseJson = mockMvc.perform(request).andReturn().getResponse() .getContentAsString(); TestPageResult<T> response = getObjectMapper().readValue(responseJson, new TypeReference<TestPageResult<T>>() { }); return response; } I call it like this: TestPageResult<SomeDto> pageResult = this.<SomeDto>getTestPageResutForRequest(getRequest()); TestPageResult

'Toolkit not initialized' exception when unit-testing an JavaFX application

会有一股神秘感。 提交于 2021-02-07 20:36:19
问题 When I try to perform unit tests on components which contain JavaFX controls I get a java.lang.IllegalStateException: Toolkit not initialized . How can components be unit tested which operate with JavaFX controls? 回答1: Add the following dependency to your project <dependency> <groupId>de.saxsys</groupId> <artifactId>jfx-testrunner</artifactId> <version>1.2</version> </dependency> and the following annotation to your test classes @RunWith(JfxRunner.class) 回答2: Just declare and initialize a JFX

'Toolkit not initialized' exception when unit-testing an JavaFX application

浪尽此生 提交于 2021-02-07 20:35:04
问题 When I try to perform unit tests on components which contain JavaFX controls I get a java.lang.IllegalStateException: Toolkit not initialized . How can components be unit tested which operate with JavaFX controls? 回答1: Add the following dependency to your project <dependency> <groupId>de.saxsys</groupId> <artifactId>jfx-testrunner</artifactId> <version>1.2</version> </dependency> and the following annotation to your test classes @RunWith(JfxRunner.class) 回答2: Just declare and initialize a JFX

Mockito Spy calls the actual method of the spied class instead of returning the hardcoded mock

对着背影说爱祢 提交于 2021-02-07 20:01:32
问题 Here's my Spy statement OAuthService oAuthServiceMock = Mockito.mock(OAuthService.class); Mockito.doReturn(oAuthServiceMock).when(inviteServiceSpy.buildOAuthService(RESOURCE_URL, CONSUMER_KEY, CONSUMER_SECRET)); Here's the method in the actual class public OAuthService buildOAuthService(String RESOURCE_URL, String CONSUMER_KEY, String CONSUMER_SECRET) { return new ServiceBuilder() .provider(new DummyOAuth1ApiProvider(RESOURCE_URL)) .apiKey(CONSUMER_KEY) .apiSecret(CONSUMER_SECRET)