Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring Junit设置测试的系统属性不工作_Spring_Junit - Fatal编程技术网

Spring Junit设置测试的系统属性不工作

Spring Junit设置测试的系统属性不工作,spring,junit,Spring,Junit,我有一个问题,我认为我现在可以解决 我正在编写一些简单的测试来测试一些服务 @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public class EndpointTests { private static Logger log = Logger.getLogger(Application

我有一个问题,我认为我现在可以解决

我正在编写一些简单的测试来测试一些服务

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class EndpointTests {

    private static Logger log = Logger.getLogger(ApplicationController.class.getName());

    @Autowired
    private WebApplicationContext context;
    private MockMvc mockMvc;

    @Mock
    private ApplicationController applicationController = new     ApplicationController();
    static {
        System.setProperty("audit.enabled", "false");
    }

    @BeforeClass
    public static void setupProperties() {
        System.setProperty("audit.enabled", "false");
    }

    @Before
    public void setup() {
       MockitoAnnotations.initMocks(this);
       mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
    }

    @Test
    public void contextLoads() {}

    @Test
    public void testGetApplications() throws Exception  {
        mockMvc.perform(get("/applications/")).andExpect(status().isOk())
               .andDo(print())
               .andExpect(content().contentType(TestUtils.APPLICATION_JSON_UTF8))
               .andExpect(jsonPath("$.[0].name",is("XYZ")));
    }
长话短说,我需要在运行测试时禁用此属性。我曾尝试在静态初始值设定项和@BeforeClass中设置该属性,正如我在其他文章中看到的那样,但当它进入实际方法时,它仍然是默认的“enabled”值,测试失败。我不使用XML配置,所以我更喜欢代码/注释解决方案

有没有其他解决方法的建议?谢谢

更新

似乎每次我的集成测试运行时:

 @Test
    public void testGetApplications() throws Exception  {
        mockMvc.perform(get("/applications/")).andExpect(status().isOk())
               .andDo(print())
               .andExpect(content().contentType(TestUtils.APPLICATION_JSON_UTF8))
               .andExpect(jsonPath("$.[0].name",is("paasport-services")));
    }
它在调用mockMvc时执行我的@Configuration类

@Configuration
public class AppConfiguration 
...
...
因此,在我的测试类中设置属性值没有好处。 是否有任何方法可以介于两者之间并为我的测试设置这一属性?我不想真正创建一个单独的测试应用程序上下文,因为它只是一个属性,到目前为止,一切都运行良好。
谢谢。

我确信有一个更优雅的解决方案,但我只是在测试类的@BeforeClass中设置了一个新的系统属性

我的审计由一个方面处理,我只需检查我只在测试类中设置的属性。如果设置为true,则建议不会执行