我有一个void方法"functionVoid"通知参数.
public class MyMotherClass {
@Inject
MyClass2 myClass2
public String motherFunction(){
....
String test = "";
myClass2.functionVoid(test);
if (test.equals("")) {
IllegalArgumentException ile = new IllegalArgumentException(
"Argument is not valid");
logger.throwing(ile);
throw ile;
}
....
}
}
public class MyClass2 {
public void functionVoid(String output_value)
{ ....
output_value = "test";
....
}
}
Run Code Online (Sandbox Code Playgroud)
如何在JUnit方法中模拟我的方法"motherFunction"?在我的例子中,"test"变量仍然是空的.
@RunWith(MockitoJUnitRunner.class)
public class MyMotherClassTest {
@Mock
private MyClass2 myClass2 ;
@InjectMock
private final MyMotherClass myMotherClass = new MyMotherClass ();
@Test
public void test(){
myMotherClass.motherFunction();
}
}
Run Code Online (Sandbox Code Playgroud) 假设我有以下服务对象
public class UserService {
@Autowired
private UserDao dao;
public void addUser(String username, String password) {
if (username.length() < 8 ) {
username = username + "random" ; // add some random string
}
User user = new User(username, password);
dao.save(user);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望在用户名长度小于8且用户名大于8个字符时测试方法"addUser"的行为.如何在单元测试UserService.addUser(...)中进行验证并验证它?我知道使用assert(),但值"password"在addUser(...)方法之外是不可用的.
我使用JUnit和Mockito.
我使用mockito来模拟单元测试用例并得到以下异常
org.mockito.exceptions.misusing.NotAMockException:
Argument passed to verify() is of type ConsumerImpl and is not a mock!
Make sure you place the parenthesis correctly!
See the examples of correct verifications:
verify(mock).someMethod();
verify(mock, times(10)).someMethod();
verify(mock, atLeastOnce()).someMetenter code herehod();
Run Code Online (Sandbox Code Playgroud)
我的代码是
MessageConsumer mConsumer = Mockito.mock(MessageConsumer.class);
String data = "new Message for Testing";
Message message = new Message(data.getBytes());
Mockito.when(mConsumer.next(10, TimeUnit.SECONDS)).thenReturn(message);
StringParserTest parserTest = new StringParserTest();
ConsumerImpl<String> consumer = new ConsumerImpl<String>(mConsumer, parserTest);
String mes=Mockito.verify(consumer,VerificationModeFactory.times(3)).consumeMessage(10,TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
请有人帮我解决这个问题
提前致谢
SRN
我有这种制作方法:
public boolean onShouldOverrideUrlLoading(String url) {
boolean isConsumed = false;
if (url.contains("code=")) {
Uri uri = Uri.parse(url);
String authCode = uri.getQueryParameter("code");
mView.authCodeObtained(authCode);
isConsumed = true;
}
return isConsumed;
}
Run Code Online (Sandbox Code Playgroud)
我有这个Mockito测试方法:
@Test
public void onShouldOverrideUrlLoadingOnAuthCodeObtained(){
String code = "someCode";
boolean isConsumed = mPresenter.onShouldOverrideUrlLoading("http://localhost/?code=" + code);
verify(mView, times(1)).authCodeObtained(code);
assertEquals(isConsumed, true);
}
Run Code Online (Sandbox Code Playgroud)
但似乎一旦代码运行并且它到达Uri.parse(url),我得到一个空指针.我错过了什么?在生产中,这非常有效.只有在测试时,Uri.parse()才会返回null.
谢谢!
我正在使用 Spring Boot 1.4.3 @AutoConfiguration,根据用户指定的属性自动创建 bean。用户可以指定一系列服务,其中名称和版本是必填字段:
service[0].name=myServiceA
service[0].version=1.0
service[1].name=myServiceB
service[1].version=1.2
...
Run Code Online (Sandbox Code Playgroud)
如果用户忘记在一项服务上指定必填字段,我想退缩并且不创建任何 bean。我可以用 来完成这个任务@ConditionalOnProperty吗?我想要这样的东西:
@Configuration
@ConditionalOnProperty({"service[i].name", "service[i].version"})
class AutoConfigureServices {
....
}
Run Code Online (Sandbox Code Playgroud) 在以下示例中:
Execution execution = mock(Execution.class);
when(execution.getLastQty()).thenReturn(1000.0);
when(execution.getLastPrice()).thenReturn(75.0);
order.onFillReceived(execution);
assertEquals(0, order.getLeavesQty(), 0);
Run Code Online (Sandbox Code Playgroud)
执行还有许多其他不应该被调用的方法.在此测试中只应使用已经模拟的两种方法,并且应该调用它们.如果调用任何其他方法,则测试应该失败.
如果调用任何其他方法,如何告诉Mockito失败?
我正在使用mockito-all-1.9.5-rc1.jar和powermock-mockito-1.4.12-full.jar.当我在非final类中运行这个简单的单元测试来模拟final方法时.
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest(ABC.class)
public class ABCTest {
@Test
public void finalCouldBeMock() {
final ABC abc = PowerMockito.mock(ABC.class);
PowerMockito.when(abc.myMethod()).thenReturn("toto");
assertEquals("toto", abc.myMethod());
}
}
Run Code Online (Sandbox Code Playgroud)
当我跑的时候,我得到了
java.lang.NoClassDefFoundError: org/mockito/internal/MockitoInvocationHandler
Caused by: java.lang.ClassNotFoundException: org.mockito.internal.MockitoInvocationHandler
当我搜索FO类MockitoInvocationHandler中mockito-all-1.9.5-rc1.jar和powermock-mockito-1.4.12-full.jar.我找不到任何东西.需要帮助解决这个问题!谢谢
我正在尝试通过Jenkinsfile为Jenkins配置HTML Publisher插件,以发布一些这样的html文件:
publishHTML(
target: [
allowMissing : false,
alwaysLinkToLastBuild: false,
keepAll : true,
reportDir : 'my-project-grails/build/reports/codenarc',
reportFiles : 'test.html',
reportName : "Codenarc Report"
]
)
Run Code Online (Sandbox Code Playgroud)
这里的reportFiles参数说明说我应该能够指定多个文件。但是语法是什么?
EasyMock或Unitils Mock(非Unitils支持的EasyMock)中是否有任何技术可以将模拟直接注入到被测试类中?
例如.在Mockito中,可以将mocks直接注入类的成员变量中,
public class TimeTrackerTest {
@InjectMocks // Used to create an instance the CUT
private TimeTrackerBean cut;
@Mock // Used to create a Mock instance
EntityManager em;
@Before
public void injectMockEntityManager() {
MockitoAnnotations.initMocks(this); // Injects Mocks into CUT
}
@Test
...
}
Run Code Online (Sandbox Code Playgroud)
这样的事情可以用EasyMock或Unitils Mock完成吗?在easymock中,我们需要在CUT中使用单独的setter方法来支持测试中的注入.我是对的还是方向注射在某种程度上是可能的?
-谢谢
我会假设以下测试应该通过,但是从不抛出异常.有线索吗?
@RunWith(PowerMockRunner.class)
@PrepareForTest(StaticService.class)
public class TestStuff {
@Test(expected = IllegalArgumentException.class)
public void testMockStatic() throws Exception {
mockStatic(StaticService.class);
doThrow(new IllegalArgumentException("Mockerror")).when(StaticService.say("hello"));
verifyStatic();
StaticService.say("hello");
}
Run Code Online (Sandbox Code Playgroud)
}