Spring JPA : test interface-based projection using factory
In this short article, I'll show you how you can easily test a repository method which uses interface-based projection.
Our repository:
You can use the above method this ways:
I use the
You must also provide the setters on your interface as follow:
Happy reading...
Our repository:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Repository | |
public interface MeasuringUncertaintyRepository extends JpaRepository<MeasuringUncertainty, String> { | |
<T> List<T> findByDeviceIdOrderById(String id, Class<T> clazz); | |
} |
You can use the above method this ways:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//... | |
public List<MeasuringUncertaintyMedium> findUncertaintiesMediumByDevice(String deviceId) { | |
Validate.notNull(deviceId, "The device identification should be provided"); | |
return measuringUncertaintyRepository.findByDeviceIdOrderById(deviceId, MeasuringUncertaintyMedium.class); | |
} | |
public List<MeasuringUncertaintyShort> findUncertaintiesShortByDevice(String deviceId) { | |
Validate.notNull(deviceId, "The device identification should be provided"); | |
return measuringUncertaintyRepository.findByDeviceIdOrderById(deviceId, MeasuringUncertaintyShort.class); | |
} | |
//... |
I use the
ProjectionFactory
as well as the SpelAwareProxyProjectionFactory
for testing (spring doc).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//imports... | |
import org.springframework.data.projection.ProjectionFactory; | |
import org.springframework.data.projection.SpelAwareProxyProjectionFactory; | |
@RunWith(SpringRunner.class) | |
@SpringBootTest(classes = Application.class) | |
@AutoConfigureMockMvc | |
public class MeasuringDeviceControllerTest { | |
private final ProjectionFactory factory = new SpelAwareProxyProjectionFactory(); | |
@Test | |
public void givenMediumMode_whenGetUncertaintiesByDevice_thenReturnsStatus200() throws Exception { | |
//create interface-based projection using factory | |
MeasuringUncertaintyMedium uncertainty1 = factory.createProjection(MeasuringUncertaintyMedium.class); | |
uncertainty1.setId("id1"); | |
uncertainty1.setMode(Mode.TENSION_ALT_400HZ); | |
uncertainty1.setUncertaintyUnit(UncertaintyUnit.PERCENT); | |
uncertainty1.setUncertaintyValue(new BigDecimal("0.01")); | |
uncertainty1.setUnit(MeasuringUnit.MV); | |
//... | |
} | |
} |
You must also provide the setters on your interface as follow:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface MeasuringUncertaintyMedium { | |
String getId(); | |
void setId(String id); | |
Mode getMode(); | |
void setMode(Mode mode); | |
MeasuringUnit getUnit(); | |
void setUnit(MeasuringUnit unit); | |
BigDecimal getUncertaintyValue(); | |
void setUncertaintyValue(BigDecimal value); | |
UncertaintyUnit getUncertaintyUnit(); | |
void setUncertaintyUnit(UncertaintyUnit unit); | |
} |
Happy reading...
Comments
Post a Comment