Skip to main content

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 โ†’