如何在单元测试中初始化ThreeTen Android backport

时间:2019-02-05 03:19:47

标签: android unit-testing threetenbp

我使用this library在我的应用中存储与日期和时间相关的数据。应用程序启动时,AndroidThreeTen首先被初始化以正常运行。所以我想问一下单元测试时如何初始化它?例如。我想使用LocalDateLocalDateTime等进行测试。

我当前的方式是这样的:

class OverviewViewModelTest {

    @Rule
    @JvmField
    val rule = InstantTaskExecutorRule()

    @Before
    fun setup() {
        AndroidThreeTen.init(Application())
    }

    //...
}

但是它抛出了这个错误:

java.lang.ExceptionInInitializerError
    at org.threeten.bp.ZoneRegion.ofId(ZoneRegion.java:143)
    at org.threeten.bp.ZoneId.of(ZoneId.java:358)
    at org.threeten.bp.ZoneId.of(ZoneId.java:286)
    at org.threeten.bp.ZoneId.systemDefault(ZoneId.java:245)
    at org.threeten.bp.Clock.systemDefaultZone(Clock.java:137)
    at org.threeten.bp.LocalDate.now(LocalDate.java:165)
Caused by: java.lang.RuntimeException: Method getAssets in android.content.ContextWrapper not mocked. See http://g.co/androidstudio/not-mocked for details.
    at android.content.ContextWrapper.getAssets(ContextWrapper.java)
    at com.jakewharton.threetenabp.AssetsZoneRulesInitializer.initializeProviders(AssetsZoneRulesInitializer.java:22)
    at org.threeten.bp.zone.ZoneRulesInitializer.initialize(ZoneRulesInitializer.java:89)
    at org.threeten.bp.zone.ZoneRulesProvider.<clinit>(ZoneRulesProvider.java:82)
    ... 32 more

那我怎样才能使这个库在单元测试中工作?

1 个答案:

答案 0 :(得分:0)

使用latest comment at the GitHub issue,我能够使用Robolectric库运行“三十” Android backport。对于Kotlin用户,请勿使用最终课程。而是使用 open 类。

在您应用的<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Raect Aiignment - PI Chart</title> <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script> <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> <!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.js"></script> --> <script src="https://unpkg.com/chart.js@2.7.2/dist/Chart.bundle.js"></script> <!-- chartkick dependency on chart.js --> <script src="https://unpkg.com/react-chartkick@0.3.0"></script> <style> .circle_style { fill: #f4fc16; stroke: #DF5C24; transition: stroke-dasharray 1s linear infinite } .svg_style { background: #f4fc16; -webkit-border-radius: 50%; -moz-border-radius: 50%; border-radius: 50%; } </style> </head> <body> <div id="root"></div> <script type="text/babel"> const svgVars = { svgWidth: 100, svgHeight: 100, defaultRadius: 25, cX: 50, cY: 50, defaultStrokeWidth: 50, // 2*circleRadius = 50 }; const circumferenceWidth = () => Math.PI * 2 * svgVars.defaultRadius; var dashWidth = (rangeSliderValue = 50) => { return ( rangeSliderValue * circumferenceWidth() / 100).toFixed(3) ; }; class Slider extends React.Component { constructor(props) { super(props); } render() { return ( <form id="slider_form" role="form"> <input type="range" min={0} max={100} defaultValue={25} step={1} onChange={this.props.doAfterSlide} /> </form> ); } } class Head extends React.Component { render() { return ( <h1>PI Chart with range slider</h1> ); } } class Chart extends React.Component { render() { let cirStyle = { strokeDasharray: `${ dashWidth() } ${ circumferenceWidth() }`, strokeWidth: svgVars.defaultStrokeWidth } return ( <svg width={ svgVars.svgWidth } height={ svgVars.svgHeight } className="svg_style"> <circle r={ svgVars.defaultRadius } cx={ svgVars.cX } cy={ svgVars.cY } style={ cirStyle } className="circle_style" /> </svg> ); } } class PieChart extends React.Component { constructor(props) { super(props); // This binding is necessary to make `this` work in the callback this.updatePiechart = this.updatePiechart.bind(this); } updatePiechart(e) { let sliderValue = e.target.value; let dw = dashWidth(sliderValue); console.log(dw); console.log("hello"); } render() { return ( <div> <Head /> <Slider doAfterSlide={this.updatePiechart} /> <Chart /> </div> ); } } /** * Render * */ ReactDOM.render( <PieChart />, document.getElementById('root') ); </script> </body> </html> 中,添加:

build.gradle

并使用此模板通过ThreeTenABP编写测试:

dependencies {
    // ...
    testImplementation('org.threeten:threetenbp:1.3.8') {
        exclude group: 'com.jakewharton.threetenabp', module: 'threetenabp'
    }
}