How does one unit test a Mail Session?

D

Danno

How do you unit test a javax.mail.Session since Session is final and
does not implement any interface so that makes mocking a problem.
 
A

AndrewMcDonagh

Danno said:
How do you unit test a javax.mail.Session since Session is final and
does not implement any interface so that makes mocking a problem.

As with most 3rd party libraries, you can put it behind an adapter.


interface MySession {
void setProtocolForAddress(String addresstype, String protocol);
// copy the remaining or better still, used methods from Session
...
}

public class SessionAdapter implements MySession {

private Session theRealSession = Session.getInstance();


public void setProtocolForAddress(String addresstype, String protocol){
theRealSession.setProtocolForAddress(addressType, protocol);
}

// delegate remaining Session methods to 'theRealSession'
...
}


class MockSession implement MySession {

public String lastAddressType;
public String lastProtocol

public void setProtocolForAddress(String addresstype, String protocol){
lastAddressType = addressType;
lastProtocol = protocol;
}

}


class MyMail {


void send(String message) {
MySession session = createSession();
if (imap) {
session.setProtocolForAddress(addressType, "IMAP");
} else {
session.setProtocolForAddress(addressType, "SMTP");
}
}

/** Protected so test can over ride*/
protected MySession createSession() {
return new SessionAdapter();
}

}


class MyMailTest extends Testcase {

public void testImapTransportProtocolUsed() {

MockSession mockSession = new MockSession();

MyMail mailer = new MyMail() {

protected MySession createSession() {
return mockSession;
}

};

mailer.send("Boo");

assertEquals("Wrong transport protocol", "IMAP",
mockSession.lastProtocol);
}



}
 
A

AndrewMcDonagh

AndrewMcDonagh said:
snipped...


class MyMailTest extends Testcase {

public void testImapTransportProtocolUsed() {

MockSession mockSession = new MockSession();

MyMail mailer = new MyMail() {

protected MySession createSession() {
return mockSession;
}

};

mailer.send("Boo");

assertEquals("Wrong transport protocol", "IMAP",
mockSession.lastProtocol);
}



}



I thought I'd show how we can inject the mock via the 'extract and over
ride' mechanism, a simpler way to do the same thing could be to pass the
mock as a Constructor arg.

And keep in mind, you only need to expose in the interface, those
methods that you actually need today, you dont have to do all of them.

Regards

Andrew
 
D

Danno

AndrewMcDonagh said:
As with most 3rd party libraries, you can put it behind an adapter.


interface MySession {
void setProtocolForAddress(String addresstype, String protocol);
// copy the remaining or better still, used methods from Session
...
}

public class SessionAdapter implements MySession {

private Session theRealSession = Session.getInstance();


public void setProtocolForAddress(String addresstype, String protocol){
theRealSession.setProtocolForAddress(addressType, protocol);
}

// delegate remaining Session methods to 'theRealSession'
...
}


class MockSession implement MySession {

public String lastAddressType;
public String lastProtocol

public void setProtocolForAddress(String addresstype, String protocol){
lastAddressType = addressType;
lastProtocol = protocol;
}

}


class MyMail {


void send(String message) {
MySession session = createSession();
if (imap) {
session.setProtocolForAddress(addressType, "IMAP");
} else {
session.setProtocolForAddress(addressType, "SMTP");
}
}

/** Protected so test can over ride*/
protected MySession createSession() {
return new SessionAdapter();
}

}


class MyMailTest extends Testcase {

public void testImapTransportProtocolUsed() {

MockSession mockSession = new MockSession();

MyMail mailer = new MyMail() {

protected MySession createSession() {
return mockSession;
}

};

mailer.send("Boo");

assertEquals("Wrong transport protocol", "IMAP",
mockSession.lastProtocol);
}



}

That's what I was leaning towards, and it's nice to know that that was
the good way to go.

Thanks so much.
Danno
 
A

AndrewMcDonagh

Danno said:
snipped...


That's what I was leaning towards, and it's nice to know that that was
the good way to go.

Thanks so much.
Danno


Cool.

oh one point worth keeping in mind, there's not many Junit users on this
NG, so if you do post a question and find no one has answered, do keep
in mind that there is a dedicated JUnit yahoo group available where they
are more than willing to help.

Andrew
 
D

Danno

AndrewMcDonagh said:
Cool.

oh one point worth keeping in mind, there's not many Junit users on this
NG, so if you do post a question and find no one has answered, do keep
in mind that there is a dedicated JUnit yahoo group available where they
are more than willing to help.

Andrew

Yep, I am a member of that as well. I have a Velocity unit test q
too, and I will put that on the email list.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top