我正在使用Spring Boot,我正在尝试创建一个测试。
其实是我的考试班:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class MemberServiceImplTest {
@InjectMocks
MemberServiceImpl memberService;
@Mock
MemberRepository memberRepository;
@Mock
Member member;
@Before
public void initMocks() {
MockitoAnnotations.initMocks(this);
}
@Test
public void getMemberId() {
Long memberId = 1l;
Member member = new Member();
...
Mockito.when(memberRepository.findOne(memberId)).thenReturn(member);
MemberDto memberDto = memberService.getMemberById(memberId);
...
}
在我的memberService
课程和getMemberById
方法中,我调用了findOne
课程的memberRepository
方法。如果未找到任何成员,则抛出ResourceNotFoundException
异常。
我不明白为什么会收到此错误。 我没有使用正确的注释吗?
答案 0 :(得分:0)
很难说清楚,因为我不能只复制并粘贴所有代码进行调试,但我相信你没有正确初始化模拟。尝试添加以下方法:
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
答案 1 :(得分:0)
而不是
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
使用它 @RunWith(MockitoJUnitRunner.class)
答案 2 :(得分:0)
如果Mockito找不到任何ID资源,那么你将自动抛出myApp.controller('SignInCtrl',['$scope','$cordovaSQLite','$ionicLoading','$state','$rootScope','$http',
function($scope, $cordovaSQLite,$ionicLoading,$state,$rootScope,$http) {
var CLIENT_ID = '741594492560-ipa5tg0pkls6ubn6h.apps.googleusercontent.com';
var SCOPES = ['https://mail.google.com/', 'https://www.googleapis.com/auth/gmail.send', 'https://www.googleapis.com/auth/gmail.modify', 'https://www.googleapis.com/auth/gmail.labels','https://www.googleapis.com/auth/plus.login','https://www.googleapis.com/oauth2/v1/tokeninfo'];
function handleAuthResult(authResult) {
if (authResult && !authResult.error) {
loadGmailApi();
}
}
$scope.initializeGMailInterface = function() {
gapi.auth.authorize({
client_id: CLIENT_ID,
scope: SCOPES,
immediate: true
}, handleAuthResult);
};
function loadGmailApi() {
gapi.client.load('gmail', 'v1', function() {
console.log("Loaded GMail API");
});
}
$scope.sendEmail = function() {
var content = 'HELLO';
// I have an email account on GMail. Lets call it 'theSenderEmail@gmail.com'
var sender = 'hello@hello.co.in';
// And an email account on Hotmail. Lets call it 'theReceiverEmail@gmail.com'\
// Note: I tried several 'receiver' email accounts, including one on GMail. None received the email.
var receiver = 'hello@hello.com';
var to = 'To: ' +receiver;
var from = 'From: ' +sender;
var subject = 'Subject: ' +'HELLO TEST';
var contentType = 'Content-Type: text/plain; charset=utf-8';
var mime = 'MIME-Version: 1.0';
var message = "";
message += to +"\r\n";
message += from +"\r\n";
message += subject +"\r\n";
message += contentType +"\r\n";
message += mime +"\r\n";
message += "\r\n" + content;
sendMessage(message, receiver, sender);
};
function sendMessage(message, receiver, sender) {
var headers = getClientRequestHeaders();
var path = "gmail/v1/users/me/messages/send?key=" + CLIENT_ID;
var base64EncodedEmail = btoa(message).replace(/\+/g, '-').replace(/\//g, '_');
gapi.client.request({
path: path,
method: "POST",
headers: headers,
body: {
'raw': base64EncodedEmail
}
}).then(function (response) {
});
}
var t = null;
function getClientRequestHeaders() {
if(!t) t = gapi.auth.getToken();
gapi.auth.setToken({token: t['access_token']});
var a = "Bearer " + t["access_token"];
return {
"Authorization": a,
"X-JavaScript-User-Agent": "Google APIs Explorer"
};
}
}])
。
因为在为{01}提到的所有memberId中,应该返回特定的ResourceNotFoundException
。
但实际上,当你调用方法时,Mockito.when(memberRepository.findOne(memberId)).thenReturn(member);
对象不是他们的,所以Mockito会抛出member
。
您必须使用member
。