Unit Tests
The test suite uses WireMock to mock all external HTTP calls. No real network access is required.
Running testsโ
# All unit tests
mvn test
# Smart Health IT sandbox profile (requires network)
mvn test -Psmart
# Epic sandbox profile (requires Epic credentials)
mvn test -Pepic
Test structureโ
src/test/java/com/akhester/smartfhir/
โโโ controller/
โ โโโ SmartLaunchControllerTest.java @WebMvcTest
โ โโโ SmartCallbackControllerTest.java @WebMvcTest
โ โโโ PatientDataControllerTest.java @WebMvcTest
โโโ service/
โ โโโ SmartDiscoveryServiceTest.java WireMock
โ โโโ SmartTokenServiceTest.java WireMock
โ โโโ IdTokenValidatorTest.java RSA key generation
โโโ security/
โ โโโ PkceHelperTest.java Crypto tests
โโโ integration/
โโโ SmartHealthSandboxIT.java @ActiveProfiles("smart")
โโโ EpicSandboxIT.java @ActiveProfiles("epic")
Example: SmartCallbackControllerTestโ
@WebMvcTest(SmartCallbackController.class)
class SmartCallbackControllerTest {
@Autowired MockMvc mockMvc;
@MockBean SmartTokenService tokenService;
@MockBean IdTokenValidator idTokenValidator;
@MockBean SmartContextExtractor extractor;
@Test
void validCallback_redirectsToDashboard() throws Exception {
when(tokenService.exchangeCode(any(), any()))
.thenReturn(mockTokenResponse());
when(extractor.extract(any()))
.thenReturn(mockLaunchContext());
mockMvc.perform(get("/callback")
.param("code", "auth-code-001")
.param("state", "valid-state")
.sessionAttr("pkce", mockPkce()))
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("/dashboard"));
}
@Test
void invalidState_returns400() throws Exception {
mockMvc.perform(get("/callback")
.param("code", "auth-code-001")
.param("state", "wrong-state")
.sessionAttr("pkce", mockPkce()))
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("/launch?error=invalid_state"));
}
}
Example: WireMock for discoveryโ
@ExtendWith(WireMockExtension.class)
class SmartDiscoveryServiceTest {
@RegisterExtension
static WireMockExtension wm = WireMockExtension.newInstance().build();
@Test
void discovery_parsesSmartConfiguration() {
wm.stubFor(get("/.well-known/smart-configuration")
.willReturn(okJson(SMART_CONFIG_JSON)));
SmartConfiguration config = service.discover(wm.baseUrl());
assertThat(config.authorizationEndpoint())
.isEqualTo(wm.baseUrl() + "/oauth2/authorize");
assertThat(config.tokenEndpoint())
.isEqualTo(wm.baseUrl() + "/oauth2/token");
}
}
Next: SMART Sandbox โ