java.lang.AssertionError:预期的响应内容:<testversion>,但为:<>

时间:2018-11-10 04:29:19

标签: spring-boot junit junit4

@RunWith(MockitoJUnitRunner.class)
public class VersionResourceImplTest {

    @Configuration
    public static class MockConfig {
        @Bean
        public Properties myProp() {
            Properties properties = new Properties();
            properties.setProperty("ra.bank.app.version", "TestVersion");
            return properties;
        }
    }

    @Value("#{myProp['ra.bank.app.version']}")
    private String applicationVersion;

    @Test
    public void testVersion() {
        Assert.assertEquals("TestVersion", applicationVersion);
    }

}

这是我的测试文件。它给出了以下错误

java.lang.AssertionError: Response content expected:<TestVersion> but was:<>
    at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54)
    at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:81)
    at org.springframework.test.web.servlet.result.ContentResultMatchers$5.match(ContentResultMatchers.java:149)
    at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:171)
    at com.db.creditrisk.rating.analysis.bank.ui.facade.v2.common.ws.VersionResourceImplTest.shouldReturnCorrectVersion(VersionResourceImplTest.java:49)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

2 个答案:

答案 0 :(得分:0)

对于属性文件中的@Value,我们使用如下所示的@Value("${app.version}") private String appVersion; 批注进行读取。

@PropertySource("classpath:application-test.properties")

所以要解决您的问题。

1。确保属性文件存在于类路径中。

@TestPropertySource("classpath:application-test.properties")

OR

@Value("#{myProp['ra.bank.app.version']}")

2。在assert语句之前打印值,以了解@Value是否真的在注入值。

3。将@Value("${ra.bank.app.version}")更改为def resampFlat(arr, reps): n = arr.shape[-1] # create an array to shift random indexes as needed shift = np.repeat(np.arange(0, arr.size, n), n).reshape(arr.shape) # get a flat view of the array arrflat = arr.ravel() # sample the array by generating random ints and shifting them appropriately return np.array([arrflat[np.random.randint(0, n, arr.shape) + shift] for i in range(reps)])

答案 1 :(得分:0)

请尝试使用以下代码,让我知道它是否有效。

 @RunWith(MockitoJUnitRunner.class)
  @ContextConfiguration(classes = VersionResourceImplTest.myProp.class)
  @TestPropertySource(properties = {
    "ra.bank.app.version=TestVersion",
})
    public class VersionResourceImplTest {

        @Value("${ra.bank.app.version}")
        private String applicationVersion;

        @Test
        public void testVersion() {
            Assert.assertEquals("TestVersion", applicationVersion);
        }

@Configuration
    public static class myProp{
        @Bean
    public static PropertySourcesPlaceholderConfigurer propertiesResolver() {
        return new PropertySourcesPlaceholderConfigurer();

    }

    }

或者您也可以使用以下方法。

@SpringBootTest
@SpringBootConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(properties = {
    "ra.bank.app.version=TestVersion"
})
public class FooTest {

  @Value("${ra.bank.app.version}")
  String applicationVersion;

  @Test
  public void testVersion() {
    Assert.assertEquals("TestVersion", applicationVersion);
 }

}