我有一个servlet:
class MyServlet extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String one = request.getParameter("one");
String two = request.getParameter("two");
addNew(one, two);
}
public void addNew(String one, String two){
// DAO called here to insert a new record
}
}
我想测试是否在不执行其主体的情况下调用'addNew'。我知道我需要一个测试双,但不知道间谍是否是最好的方法。我想这样做:
class MyServletSpy extends MyServlet
{
private boolean addNewGotCalled = false;
@Override
public void addNew(String one, Integer, two)
{
addNewGotCalled = true;
}
public boolean addNewWasCalled()
{
return this.addNewGotCalled;
}
}
这是正确的方法吗?我觉得扩展MyServlet是一个愚蠢的奇怪,似乎我允许我的测试有依赖。
答案 0 :(得分:-1)
很难理解你真正想要测试的内容但是如果你想测试你的Servlet的行为,你可以试着看看Jetty:https://webtide.com/unit-test-servlets-with-jetty/
修改强>
感谢您的澄清。在您的情况下,一种解决方案是使用addNew
来测试成功调用<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
方法。
首先你需要mockito依赖:
spy
然后您可以使用/**
* Test of {@link MyServlet} class
* @author rouliboy
*/
public class MyServletTest
{
@Test
public void testAddNewCall() throws ServletException, IOException
{
// Initialization
MyServlet myServlet = new MyServlet();
myServlet = Mockito.spy(myServlet);
final HttpServletRequest mockHttpServletRequest = Mockito.mock(HttpServletRequest.class);
final HttpServletResponse mockHttpServletResponse = Mockito.mock(HttpServletResponse.class);
Mockito.when(mockHttpServletRequest.getParameter("one")).thenReturn("oneReturn");
Mockito.when(mockHttpServletRequest.getParameter("two")).thenReturn("twoReturn");
// Test
myServlet.doPost(mockHttpServletRequest, mockHttpServletResponse);
// Verification
Mockito.verify(myServlet).addNew("oneReturn", "twoReturn");
}
}
方法测试您的Servlet:
spy
<强>说明强>
首先我们myServlet = Mockito.spy(myServlet);
使用Servlet:
spy
使用HttpServletRequest
,我们可以检查Servlet中调用的方法。
然后我们模拟HttpServletResponse
方法的doPost
和HttpServletRequest.getParameter(...)
输入。使用模拟,我们可以通过Mockito.when(mockHttpServletRequest.getParameter("one")).thenReturn("oneReturn");
Mockito.when(mockHttpServletRequest.getParameter("two")).thenReturn("twoReturn");
定义返回值的行为:
doPost
在调用addNew
方法后,我们验证是否已成功调用Mockito.verify(myServlet).addNew("oneReturn", "twoReturn");
:
#include <SpritzCipher.h>
String salt = "fkllrnjnfd";
int randNumber;
String valorConcat;
void randomico() {
//Serial.begin(9600);
randomSeed(analogRead(0)); // Starts the list of random values
randNumber = random(100); // Draw a number from 0 to 99
valorConcat = String(randNumber) + salt;
//Serial.println(valorConcat); // Sends the value of randNumber to the serial
delay(500); // espera 500 ms
}
/* Data to input */
const byte testData[12] = {randomico};
/* Test vectors */
/* Data = 'testData' hash test vectors */
const byte testVector[32] =
{ 0xff, 0x8c, 0xf2, 0x68, 0x09, 0x4c, 0x87, 0xb9,
0x5f, 0x74, 0xce, 0x6f, 0xee, 0x9d, 0x30, 0x03,
0xa5, 0xf9, 0xfe, 0x69, 0x44, 0x65, 0x3c, 0xd5,
0x0e, 0x66, 0xbf, 0x18, 0x9c, 0x63, 0xf6, 0x99
};
void testFunc(const byte ExpectedOutput[32], const byte *data, byte dataLen)
{
byte hashLen = 32; /* 256-bit */
byte digest[hashLen]; /* Output buffer */
byte digest_2[hashLen]; /* Output buffer for chunk by chunk API */
spritz_ctx hash_ctx; /* the CTX for chunk by chunk API */
unsigned int i;
/* Print input */
for (i = 0; i < dataLen; i++) {
Serial.write(data[i]);
}
Serial.println();
spritz_hash_setup(&hash_ctx);
/* For easy test: code add a byte each time */
for (i = 0; i < dataLen; i++) {
spritz_hash_update(&hash_ctx, data + i, 1);
}
spritz_hash_final(&hash_ctx, digest_2, hashLen);
spritz_hash(digest, hashLen, data, dataLen);
for (i = 0; i < sizeof(digest); i++) {
if (digest[i] < 0x10) { /* To print "0F" not "F" */
Serial.write('0');
}
Serial.print(digest[i], HEX);
}
/* Check the output */
if (spritz_compare(digest, ExpectedOutput, sizeof(digest)) || spritz_compare(digest_2, ExpectedOutput, sizeof(digest_2))) {
/* If the output is wrong "Alert" */
digitalWrite(LED_BUILTIN, HIGH); /* Turn pin LED_BUILTIN On (Most boards have this LED connected to digital pin 13) */
Serial.println("\n** WARNING: Output != Test_Vector **");
}
Serial.println();
}
void setup() {
/* Initialize serial and wait for port to open */
Serial.begin(9600);
while (!Serial) {
; /* Wait for serial port to connect. Needed for Leonardo only */
}
/* initialize digital pin LED_BUILTIN (Most boards have this LED connected to digital pin 13) as an output */
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}
void loop() {
Serial.println("[Spritz spritz_hash*() test]\n");
/* Data: arcfour */
testFunc(testVector, testData, sizeof(testData));
delay(5000); /* Wait 5s */
Serial.println();
}
但是,我认为最好完全测试你的Servlet(例如使用Jetty并模拟你的DAO),而不仅仅是测试你的addNew方法是否被调用。