是空的Arraylist(以null为其项)被视为null?所以,基本上下面的陈述是正确的:
if (arrayList != null)
Run Code Online (Sandbox Code Playgroud)
谢谢
我正在尝试模拟Apache HttpClient接口,以便模拟下面提到的一个方法来返回一个存根的JSON对象作为响应.
HttpResponse response = defaultHttpClient.execute(postRequest);
Run Code Online (Sandbox Code Playgroud)
有人可以通过一些示例代码建议如何实现这一目标吗?非常感谢您的帮助.
谢谢
我正在使用jdbcTemplate.queryForList以下列方式执行命名查询:
List<Conversation> conversations = jdbcTemplate.queryForList(
SELECT_ALL_CONVERSATIONS_SQL_FULL,
new Object[] {userId, dateFrom, dateTo});
Run Code Online (Sandbox Code Playgroud)
SQL查询是:
private final String SELECT_ALL_CONVERSATIONS_SQL_FULL =
"select conversation.conversationID, conversation.room, " +
"conversation.isExternal, conversation.startDate, " +
"conversation.lastActivity, conversation.messageCount " +
"from openfire.ofconversation conversation " +
"WHERE conversation.conversationid IN " +
"(SELECT conversation.conversationID " +
"FROM openfire.ofconversation conversation, " +
"openfire.ofconparticipant participant " +
"WHERE conversation.conversationID = participant.conversationID " +
"AND participant.bareJID LIKE ? " +
"AND conversation.startDate between ? AND ?)";
Run Code Online (Sandbox Code Playgroud)
但是,当以下列方式提取列表的内容时:
for (Conversation conversation : conversations) {
builder.append(conversation.getId());
builder.append(","); …Run Code Online (Sandbox Code Playgroud) 在我的java代码中,我需要向具有3个标头的特定URL发送http post请求:
URL: http://localhost/something
Referer: http://localhost/something
Authorization: Basic (with a username and password)
Content-type: application/json
Run Code Online (Sandbox Code Playgroud)
这将返回一个响应,其中包含一个JSON"key":"value"对,然后我需要以某种方式解析以将键/值(Alan/72)存储在MAP中.响应是(当使用SOAPUI或Postman Rest时):
{
"analyzedNames": [
{
"alternate": false
}
],
"nameResults": [
{
"alternate": false,
"givenName": "John",
"nameCategory": "PERSONAL",
"originalGivenName": "",
"originalSurname": "",
"score": 72,
"scriptType": "NOSCRIPT",
}
]
}
Run Code Online (Sandbox Code Playgroud)
我可以使用SOAPUI或Postman Rest来做到这一点,但是如何在Java中执行此操作,因为我收到错误:
****DEBUG main org.apache.http.impl.conn.DefaultClientConnection - Receiving response: HTTP/1.1 500 Internal Server Error****
Run Code Online (Sandbox Code Playgroud)
我的代码是:
public class NameSearch {
/**
* @param args
* @throws IOException
* @throws ClientProtocolException
*/
public static void main(String[] args) throws …Run Code Online (Sandbox Code Playgroud) 我希望单独记录在同一个类中生成的特定消息.那么,如何在同一个类中创建2种不同类型的记录器.目前,Properties文件看起来像
log4j.rootCategory=DEBUG, O
# Stdout
log4j.appender.O=org.apache.log4j.ConsoleAppender
log4j.appender.O.layout=org.apache.log4j.PatternLayout
log4j.appender.O.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n
# File
log4j.appender.MESSAGE=org.apache.log4j.RollingFileAppender
log4j.appender.MESSAGE.File=target/logs/messages.log
# Control the maximum log file size
log4j.appender.MESSAGE.MaxFileSize=1000KB
# Archive log files (one backup file here)
log4j.appender.MESSAGE.MaxBackupIndex=100
log4j.appender.MESSAGE.layout=org.apache.log4j.PatternLayout
log4j.appender.MESSAGE.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M (% F:% L) - %m%n
log4j.appender.MESSAGE.
log4j.category.failedMessagesLog=INFO, MESSAGE
Run Code Online (Sandbox Code Playgroud)
我正在使用我的代码中的日志记录: - /**Logger.*/
Logger logger = Logger.getLogger(MyClass.class);
Logger msgLogger = Logger.getLogger("MESSAGE");
Run Code Online (Sandbox Code Playgroud)
经过测试,我得到一个空的日志文件(messages.log).有什么建议??
我有一个存根JSON OBJECT但需要使用Mockito模拟以下内容:
HttpResponse response = defaultHttpClient.execute(postRequest);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder result = new StringBuilder();
while ((line = rd.readLine()) != null) {
result.append(line);
}
JSONObject jsonResponseObject = new JSONObject(result.toString());
Run Code Online (Sandbox Code Playgroud)
我创建了以下Mocks:
@Mock
private HttpClient mockHttpClient;
private HttpPost mockHttpPost;
private HttpResponse mockHttpResponse;
private HttpEntity mockHttpEntity;
private InputStream mockInputStream;
private InputStreamReader mockInputStreamReader;
private BufferedReader mockBufferedReader;
Run Code Online (Sandbox Code Playgroud)
并有以下when声明:
Mockito.when(mockHttpClient.execute(mockHttpPost)).thenReturn(mockHttpResponse);
Mockito.when(mockHttpResponse.getEntity()).thenReturn(mockHttpEntity);
Mockito.when(mockHttpEntity.getContent()).thenReturn(mockInputStream);
Run Code Online (Sandbox Code Playgroud)
问题:我是否需要创建所有这些'when'语句,如果是,那么我需要创建哪些其他语句才能获得存根JSON?
有什么建议吗?
谢谢
我第一次使用Mockito和PowerMock,我在以下行运行下面的代码时遇到错误:
MockitoAnnotations.initMocks(SearchTest.class);
Run Code Online (Sandbox Code Playgroud)
错误是:
java.lang.ExceptionInInitializerError
at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.<init>(ConditionalStackTraceFilter.java:17)
at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:30)
at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:19)
at org.mockito.exceptions.misusing.MockitoConfigurationException.<init>(MockitoConfigurationException.java:18)
at org.mockito.internal.configuration.ClassPathLoader.loadImplementations(ClassPathLoader.java:145)
at org.mockito.internal.configuration.ClassPathLoader.findPluginImplementation(ClassPathLoader.java:110)
at org.mockito.internal.configuration.ClassPathLoader.findPlatformMockMaker(ClassPathLoader.java:106)
at org.mockito.internal.configuration.ClassPathLoader.<clinit>(ClassPathLoader.java:59)
at org.mockito.internal.configuration.GlobalConfiguration.createConfig(GlobalConfiguration.java:38)
at org.mockito.internal.configuration.GlobalConfiguration.<init>(GlobalConfiguration.java:32)
at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:94)
Caused by: java.lang.NullPointerException
Run Code Online (Sandbox Code Playgroud)
测试类的代码是:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized.Parameters;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(Parameterized.class)
@PrepareForTest(InputStreamReader.class)
public class SearchTest {
private String preFile;
private String expectedPreFile;
private String postFile;
private String expectedpostFile; …Run Code Online (Sandbox Code Playgroud) 作为开发人员,我是单元测试的新手,并且需要编写测试用例来对以下代码进行单元测试.有人可以帮助我,并给我一些关于如何在eclipse中编写单元测试的指针.
private void handle(final DocumentEvent e) {
Document doc = e.getDocument();
try {
String text = e.getDocument().getText(0, doc.getLength());
if (text.length() >= maxMessageSize) {
try {
component.getHighlighter()
.addHighlight(maxMessageSize, text.length() + 1, painter);
} catch (BadLocationException ex) {
System.out.println(ex.getMessage());
}
} else {
component.getHighlighter().removeAllHighlights();
}
} catch (BadLocationException e1) {
System.out.println(e1.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
由于某种原因,当我运行测试用例时,我根本没有得到任何报道.我在这里做错了什么?进一步的研究表明我需要使用test.perform()方法来调用我想要测试的方法.这是正确的吗?你能建议吗?这是代码:
public class TestMaxLength {
static final int maxMessageSize = 125;
JTextPane textPane = new JTextPane();
//***EasyMock varibles****
private JTextComponent mockComponent;
private MaxLength classUnderTest;
private DocumentEvent mockEvent; …Run Code Online (Sandbox Code Playgroud) java ×9
mocking ×3
mockito ×3
json ×2
arraylist ×1
arrays ×1
eclipse ×1
generics ×1
httpclient ×1
jdbctemplate ×1
junit ×1
log4j ×1
powermock ×1
spring-ws ×1
unit-testing ×1