Skip to content

Commit

Permalink
test: 修复Md5PasswordServiceTest的测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
damingerdai committed Nov 7, 2024
1 parent 0dbf6bb commit 7bb3d3c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@Service("md5PasswordService")
public class Md5PasswordService implements IPasswordService {

private IErrorService errorService;
private final IErrorService errorService;

/**
* 解密
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
package org.daming.hoteler.security.service.impl;

import org.daming.hoteler.base.exceptions.HotelerException;
import org.daming.hoteler.service.IErrorService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

// @SpringBootTest
@SpringBootTest(properties = "spring.main.allow-bean-definition-overriding=true")
@SpringBootTest
@ContextConfiguration(classes = Md5PasswordServiceTest.TestConfig.class)
class Md5PasswordServiceTest {

@Configuration
static class TestConfig {

@MockBean
public IErrorService errorService;

@Bean
public Md5PasswordService md5PasswordService(IErrorService errorService) {
return new Md5PasswordService(errorService);
}
}

@Autowired
private Md5PasswordService md5PasswordService;

@Autowired
private IErrorService errorService;

@BeforeEach
public void beforeEach() {
assertNotNull(md5PasswordService);
Expand All @@ -32,8 +53,11 @@ public void testEncodePassword() {
@Test
@DisplayName("Md5PasswordService解密密码单元测试")
public void testDecodePassword() {
var he = new HotelerException();
he.setMessage("md5 algorithm encrypt is not supported");
when(errorService.createHotelerSystemException(any(String.class), any(Throwable.class))).thenReturn(he);
var thrown = assertThrows(HotelerException.class, () -> md5PasswordService.decodePassword("e10adc3949ba59abbe56e057f20f883e"));
assertEquals(thrown.getMessage(), "md5 algorithm encrypt is not supported");
assertEquals(thrown.getMessage(), he.getMessage());
}

}

0 comments on commit 7bb3d3c

Please sign in to comment.